How to read yaml with php

0

Hello, I'm 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 only the list of the server team. 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 server's config.yml file.

This is the config.yml file that defines the status of the players:

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

What I was thinking was to create a "reader" in php, which read the yaml code and the positions of the players 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 each staff member's name to appear 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, "Administrator" and not "Administrators", for example:

Home is an administrator:

  

Administrator

     
  • JamesMarch
  •   

House have two or more administrators:

  

Administrator is

     
  • JamesMarch
  •   
  • TheCountess _
  •   

I also wanted to be able to appear only the three positions in 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 hierarchy posts to appear, for example:

Right:

  

Administrators

     
  • JamesMarch

  •   
  • TheCountess _

  •   

Moderator

     
  • mumiant _
  •   

Helpers

     
  • nana34piu

  •   
  • Small20

  •   

Wrong:

  

Helpers

     
  • nana34piu

  •   
  • Small20

  •   

Administrators

     
  • JamesMarch

  •   
  • TheCountess _

  •   

Moderator

     
  • mumiant _
  •   
    
asked by anonymous 01.07.2017 / 20:21

1 answer

2

Installing Yml Readers

If your server allows you to install something you can install / compile link , download link and then compile:

$ ./configure
$ make
# make install

And then install via PECL , you can download here link

Symfony

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

composer require symfony/yaml

Or add in your file composer.json o symfony/yaml and then execute:

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 look something like this:

  

Note that I had to rearrange the array in a structure to be able to mount the 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>
    
01.07.2017 / 20:36