How to read yaml with php

Asked

Viewed 535 times

0

Hello, I am opening a Minecraft server (java game) and I came across the following need:

For each Minecraft server it is necessary to build an informative website. When building the site, I created a simple page with just the server team list. The team consists of "Helpers", "Moderators" and "Administrators". To assign one of these roles to a player, you need to enter a command within the game, which will then be validated in the config.yml file of the server.

This is the config.yml file that defines player status:

users:
      JamesMarch:
        group:
        - Admin
      TheCountess_:
        group:
        - Admin
      mumiant_:
        group:
        - Moderador
      Pequena20:
        group:
        - Ajudante
      nana34piu:
        group:
        - Ajudante

What I was thinking was to create a php "reader", who would read the yaml code and the player positions on my site as follows:

<html>
    <head>
        <title>Alderaan Minecraft Server</title>
    </head>
    <body>
        <h1>Staff</h1>
            <h2>Administradores</h2>
                <p>JamesMarch</p>
                <p>TheCountess_</p>
            <h2>Moderador</h2>
                <p>mumiant_</p>
            <h2>Ajudantes</h2>
                <p>Pequena20</p>
                <p>nana34piu</p>
    </body>
</html>

I would also like the name of each member to appear in each staff paragraph, in alphabetical order, for example:

Administrators

  • Jamesmarch
  • Thecountess_

and not

Administrators

  • Thecountess_
  • Jamesmarch

I would also like, for example, if there is only one Administrator, appear "Administrator" and not "Administrators", example:

House there is an administrator:

Administrator

  • Jamesmarch

House there are two or more administrators:

Administratores

  • Jamesmarch
  • Thecountess_

I wish it was possible too appear only the three posts on the web site, since the site has more than three positions, I liked that only the positions of "Administrator", "Moderator" and "Helper" appear on the page.

Finally, I wanted the ranks to appear by the hierarchy, example:

Right:

Administrators

  • Jamesmarch

  • Thecountess_

Moderator

  • mumiant_

Helpers

  • nana34piu

  • Small20

Wrong:

Helpers

  • nana34piu

  • Small20

Administrators

  • Jamesmarch

  • Thecountess_

Moderator

  • mumiant_

1 answer

2

Installing readers of Yml

If your server allows you to install something you can install/compile the http://pyyaml.org/wiki/LibYAML, download http://pyyaml.org/download/libyaml/yaml-0.1.7.tar.gz and then Compile:

$ ./configure
$ make
# make install

And then install via PECL, can download here http://pecl.php.net/package/yaml

Symfony

You can install the Symfony package using composer, perform this in your project:

composer require symfony/yaml

Or add to your file composer.json the symfony/yaml and then perform:

composer update

To use do this:

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;

try {
    $value = Yaml::parse(file_get_contents('arquivo.yml'));
} catch (ParseException $e) {
    printf("Impossivel decodificar o YAML: %s", $e->getMessage());
}

Reading the question yml

Your . yml seems to be in an invalid format, I suppose the correct one would be this:

users:
    JamesMarch:
        group:
        - Admin
    TheCountess_:
        group:
        - Admin
    mumiant_:
        group:
        - Moderador
    Pequena20:
        group:
        - Ajudante
    nana34piu:
        group:
        - Ajudante

The result should be more or less this:

Note that I had to rearrange the array in a structure to be able to mount HTML more easily, using the variable $porcargo

<?php

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;

$conteudo = file_get_contents('config.yml');

include 'vendor/autoload.php';

$porcargo = array();

try {
    $ymlParsed = Yaml::parse($conteudo);
    foreach ($ymlParsed['users'] as $nome => $cargos) {
        $grupo = $cargos['group'];
        $j = count($grupo);

        for ($i = 0; $i < $j; $i++) {
            if (!isset($porcargo[ $grupo[$i] ])) {
                $porcargo[ $grupo[$i] ] = array();
            }

            $porcargo[ $grupo[$i] ][] = $nome;
        }
    }
} catch (ParseException $e) {
    die(sprintf("Unable to parse the YAML string: %s", $e->getMessage()));
}
?>
<html>
    <head>
        <title>Alderaan Minecraft Server</title>
    </head>
    <body>
        <h1>Staff</h1>
            <?php foreach ($porcargo as $cargo => $usuarios): ?>

            <h2><?php echo $cargo; ?></h2>

                <?php for ($i = 0; $i < count($usuarios); $i++): ?>

                    <p><?php echo $usuarios[$i]; ?></p>

                <?php endfor; ?>

            <?php endforeach; ?>
    </body>
</html>
  • yaml-parse-file is the structured form of Yaml::parse correct?

  • @Everson I believe that Yaml::parse is a symfony package independent of LibYAML and http://pecl.php.net/package/yaml, for a look at the source-code: https://github.com/symfony/yaml/blob/master/Parser.php#L88

Browser other questions tagged

You are not signed in. Login or sign up in order to post.