"Fossies" - the Fresh Open Source Software Archive

Member "Symfony/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/AbstractConfigCommand.php" (30 Mar 2020, 4778 Bytes) of package /linux/www/Symfony_Standard_Vendors_3.4.39.tgz:


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 "AbstractConfigCommand.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 /*
    4  * This file is part of the Symfony package.
    5  *
    6  * (c) Fabien Potencier <fabien@symfony.com>
    7  *
    8  * For the full copyright and license information, please view the LICENSE
    9  * file that was distributed with this source code.
   10  */
   11 
   12 namespace Symfony\Bundle\FrameworkBundle\Command;
   13 
   14 use Symfony\Component\Config\Definition\ConfigurationInterface;
   15 use Symfony\Component\Console\Exception\LogicException;
   16 use Symfony\Component\Console\Helper\Table;
   17 use Symfony\Component\Console\Output\OutputInterface;
   18 use Symfony\Component\Console\Style\StyleInterface;
   19 use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
   20 
   21 /**
   22  * A console command for dumping available configuration reference.
   23  *
   24  * @author Kevin Bond <kevinbond@gmail.com>
   25  * @author Wouter J <waldio.webdesign@gmail.com>
   26  * @author Grégoire Pineau <lyrixx@lyrixx.info>
   27  */
   28 abstract class AbstractConfigCommand extends ContainerDebugCommand
   29 {
   30     /**
   31      * @param OutputInterface|StyleInterface $output
   32      */
   33     protected function listBundles($output)
   34     {
   35         $title = 'Available registered bundles with their extension alias if available';
   36         $headers = ['Bundle name', 'Extension alias'];
   37         $rows = [];
   38 
   39         $bundles = $this->getApplication()->getKernel()->getBundles();
   40         usort($bundles, function ($bundleA, $bundleB) {
   41             return strcmp($bundleA->getName(), $bundleB->getName());
   42         });
   43 
   44         foreach ($bundles as $bundle) {
   45             $extension = $bundle->getContainerExtension();
   46             $rows[] = [$bundle->getName(), $extension ? $extension->getAlias() : ''];
   47         }
   48 
   49         if ($output instanceof StyleInterface) {
   50             $output->title($title);
   51             $output->table($headers, $rows);
   52         } else {
   53             $output->writeln($title);
   54             $table = new Table($output);
   55             $table->setHeaders($headers)->setRows($rows)->render();
   56         }
   57     }
   58 
   59     /**
   60      * @return ExtensionInterface
   61      */
   62     protected function findExtension($name)
   63     {
   64         $bundles = $this->initializeBundles();
   65         $minScore = INF;
   66 
   67         foreach ($bundles as $bundle) {
   68             if ($name === $bundle->getName()) {
   69                 if (!$bundle->getContainerExtension()) {
   70                     throw new \LogicException(sprintf('Bundle "%s" does not have a container extension.', $name));
   71                 }
   72 
   73                 return $bundle->getContainerExtension();
   74             }
   75 
   76             $distance = levenshtein($name, $bundle->getName());
   77 
   78             if ($distance < $minScore) {
   79                 $guess = $bundle->getName();
   80                 $minScore = $distance;
   81             }
   82 
   83             $extension = $bundle->getContainerExtension();
   84 
   85             if ($extension) {
   86                 if ($name === $extension->getAlias()) {
   87                     return $extension;
   88                 }
   89 
   90                 $distance = levenshtein($name, $extension->getAlias());
   91 
   92                 if ($distance < $minScore) {
   93                     $guess = $extension->getAlias();
   94                     $minScore = $distance;
   95                 }
   96             }
   97         }
   98 
   99         if ('Bundle' !== substr($name, -6)) {
  100             $message = sprintf('No extensions with configuration available for "%s".', $name);
  101         } else {
  102             $message = sprintf('No extension with alias "%s" is enabled.', $name);
  103         }
  104 
  105         if (isset($guess) && $minScore < 3) {
  106             $message .= sprintf("\n\nDid you mean \"%s\"?", $guess);
  107         }
  108 
  109         throw new LogicException($message);
  110     }
  111 
  112     public function validateConfiguration(ExtensionInterface $extension, $configuration)
  113     {
  114         if (!$configuration) {
  115             throw new \LogicException(sprintf('The extension with alias "%s" does not have its getConfiguration() method setup.', $extension->getAlias()));
  116         }
  117 
  118         if (!$configuration instanceof ConfigurationInterface) {
  119             throw new \LogicException(sprintf('Configuration class "%s" should implement ConfigurationInterface in order to be dumpable.', \get_class($configuration)));
  120         }
  121     }
  122 
  123     private function initializeBundles()
  124     {
  125         // Re-build bundle manually to initialize DI extensions that can be extended by other bundles in their build() method
  126         // as this method is not called when the container is loaded from the cache.
  127         $container = $this->getContainerBuilder();
  128         $bundles = $this->getApplication()->getKernel()->getBundles();
  129         foreach ($bundles as $bundle) {
  130             if ($extension = $bundle->getContainerExtension()) {
  131                 $container->registerExtension($extension);
  132             }
  133         }
  134 
  135         foreach ($bundles as $bundle) {
  136             $bundle->build($container);
  137         }
  138 
  139         return $bundles;
  140     }
  141 }