[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

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


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1