[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/console/Symfony/Component/Console/Descriptor/ -> ApplicationDescription.php (source)

   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\Component\Console\Descriptor;
  13  
  14  use Symfony\Component\Console\Application;
  15  use Symfony\Component\Console\Command\Command;
  16  
  17  /**
  18   * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  19   */
  20  class ApplicationDescription
  21  {
  22      const GLOBAL_NAMESPACE = '_global';
  23  
  24      /**
  25       * @var Application
  26       */
  27      private $application;
  28  
  29      /**
  30       * @var null|string
  31       */
  32      private $namespace;
  33  
  34      /**
  35       * @var array
  36       */
  37      private $namespaces;
  38  
  39      /**
  40       * @var Command[]
  41       */
  42      private $commands;
  43  
  44      /**
  45       * @var Command[]
  46       */
  47      private $aliases;
  48  
  49      /**
  50       * Constructor.
  51       *
  52       * @param Application $application
  53       * @param string|null $namespace
  54       */
  55      public function __construct(Application $application, $namespace = null)
  56      {
  57          $this->application = $application;
  58          $this->namespace = $namespace;
  59      }
  60  
  61      /**
  62       * @return array
  63       */
  64      public function getNamespaces()
  65      {
  66          if (null === $this->namespaces) {
  67              $this->inspectApplication();
  68          }
  69  
  70          return $this->namespaces;
  71      }
  72  
  73      /**
  74       * @return Command[]
  75       */
  76      public function getCommands()
  77      {
  78          if (null === $this->commands) {
  79              $this->inspectApplication();
  80          }
  81  
  82          return $this->commands;
  83      }
  84  
  85      /**
  86       * @param string $name
  87       *
  88       * @return Command
  89       *
  90       * @throws \InvalidArgumentException
  91       */
  92      public function getCommand($name)
  93      {
  94          if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
  95              throw new \InvalidArgumentException(sprintf('Command %s does not exist.', $name));
  96          }
  97  
  98          return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];
  99      }
 100  
 101      private function inspectApplication()
 102      {
 103          $this->commands = array();
 104          $this->namespaces = array();
 105  
 106          $all = $this->application->all($this->namespace ? $this->application->findNamespace($this->namespace) : null);
 107          foreach ($this->sortCommands($all) as $namespace => $commands) {
 108              $names = array();
 109  
 110              /** @var Command $command */
 111              foreach ($commands as $name => $command) {
 112                  if (!$command->getName()) {
 113                      continue;
 114                  }
 115  
 116                  if ($command->getName() === $name) {
 117                      $this->commands[$name] = $command;
 118                  } else {
 119                      $this->aliases[$name] = $command;
 120                  }
 121  
 122                  $names[] = $name;
 123              }
 124  
 125              $this->namespaces[$namespace] = array('id' => $namespace, 'commands' => $names);
 126          }
 127      }
 128  
 129      /**
 130       * @param array $commands
 131       *
 132       * @return array
 133       */
 134      private function sortCommands(array $commands)
 135      {
 136          $namespacedCommands = array();
 137          $globalCommands = array();
 138          foreach ($commands as $name => $command) {
 139              $key = $this->application->extractNamespace($name, 1);
 140              if (!$key) {
 141                  $globalCommands['_global'][$name] = $command;
 142              } else {
 143                  $namespacedCommands[$key][$name] = $command;
 144              }
 145          }
 146          ksort($namespacedCommands);
 147          $namespacedCommands = array_merge($globalCommands, $namespacedCommands);
 148  
 149          foreach ($namespacedCommands as &$commandsSet) {
 150              ksort($commandsSet);
 151          }
 152          // unset reference to keep scope clear
 153          unset($commandsSet);
 154  
 155          return $namespacedCommands;
 156      }
 157  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1