"Fossies" - the Fresh Open Source Software Archive

Member "contao-4.4.48/core-bundle/src/DependencyInjection/Compiler/AddPackagesPass.php" (2 Apr 2020, 1841 Bytes) of package /linux/www/contao-4.4.48.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AddPackagesPass.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 4.8.8_vs_4.9.0.

    1 <?php
    2 
    3 /*
    4  * This file is part of Contao.
    5  *
    6  * (c) Leo Feyer
    7  *
    8  * @license LGPL-3.0-or-later
    9  */
   10 
   11 namespace Contao\CoreBundle\DependencyInjection\Compiler;
   12 
   13 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
   14 use Symfony\Component\DependencyInjection\ContainerBuilder;
   15 
   16 /**
   17  * Adds the composer packages and versions to the container.
   18  *
   19  * @author Andreas Schempp <https://github.com/aschempp>
   20  * @author Leo Feyer <https://github.com/leofeyer>
   21  */
   22 class AddPackagesPass implements CompilerPassInterface
   23 {
   24     /**
   25      * @var string
   26      */
   27     private $jsonFile;
   28 
   29     /**
   30      * Constructor.
   31      *
   32      * @param string $jsonFile
   33      */
   34     public function __construct($jsonFile)
   35     {
   36         $this->jsonFile = $jsonFile;
   37     }
   38 
   39     /**
   40      * {@inheritdoc}
   41      */
   42     public function process(ContainerBuilder $container)
   43     {
   44         $packages = [];
   45 
   46         if (is_file($this->jsonFile)) {
   47             $json = json_decode(file_get_contents($this->jsonFile), true);
   48 
   49             if (null !== $json) {
   50                 $packages = $this->getVersions($json);
   51             }
   52         }
   53 
   54         $container->setParameter('kernel.packages', $packages);
   55     }
   56 
   57     /**
   58      * Extracts the version numbers from the JSON data.
   59      *
   60      * @param array $json
   61      *
   62      * @return array
   63      */
   64     private function getVersions(array $json)
   65     {
   66         $packages = [];
   67 
   68         foreach ($json as $package) {
   69             if (isset($package['version'])) {
   70                 if (isset($package['extra']['branch-alias'][$package['version']])) {
   71                     $packages[$package['name']] = $package['extra']['branch-alias'][$package['version']];
   72                 } else {
   73                     $packages[$package['name']] = $package['version'];
   74                 }
   75             }
   76         }
   77 
   78         return $packages;
   79     }
   80 }