[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/router/src/Command/ -> DebugRouterCommand.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Router Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Router\Command;
  10  
  11  use Joomla\Console\Command\AbstractCommand;
  12  use Joomla\Router\RouterInterface;
  13  use Symfony\Component\Console\Helper\TableCell;
  14  use Symfony\Component\Console\Input\InputInterface;
  15  use Symfony\Component\Console\Input\InputOption;
  16  use Symfony\Component\Console\Output\OutputInterface;
  17  use Symfony\Component\Console\Style\SymfonyStyle;
  18  
  19  /**
  20   * Command listing information about the application's router.
  21   *
  22   * @since  2.0.0
  23   */
  24  class DebugRouterCommand extends AbstractCommand
  25  {
  26      /**
  27       * The default command name
  28       *
  29       * @var    string
  30       * @since  2.0.0
  31       */
  32      protected static $defaultName = 'debug:router';
  33  
  34      /**
  35       * The application router.
  36       *
  37       * @var    RouterInterface
  38       * @since  2.0.0
  39       */
  40      private $router;
  41  
  42      /**
  43       * Instantiate the command.
  44       *
  45       * @param   RouterInterface  $router  The application router.
  46       *
  47       * @since   2.0.0
  48       */
  49  	public function __construct(RouterInterface $router)
  50      {
  51          $this->router = $router;
  52  
  53          parent::__construct();
  54      }
  55  
  56      /**
  57       * Configure the command.
  58       *
  59       * @return  void
  60       *
  61       * @since   2.0.0
  62       */
  63  	protected function configure(): void
  64      {
  65          $this->setDescription("Displays information about the application's routes");
  66          $this->addOption('show-controllers', null, InputOption::VALUE_NONE, 'Show the controller for a route in the overview');
  67          $this->setHelp(<<<'EOF'
  68  The <info>%command.name%</info> command lists all of the application's routes:
  69  
  70    <info>php %command.full_name%</info>
  71  
  72  To show the controllers that handle each route, use the <info>--show-controllers</info> option:
  73  
  74    <info>php %command.full_name% --show-controllers</info>
  75  EOF
  76          );
  77      }
  78  
  79      /**
  80       * Internal function to execute the command.
  81       *
  82       * @param   InputInterface   $input   The input to inject into the command.
  83       * @param   OutputInterface  $output  The output to inject into the command.
  84       *
  85       * @return  integer  The command exit code
  86       *
  87       * @since   2.0.0
  88       */
  89  	protected function doExecute(InputInterface $input, OutputInterface $output): int
  90      {
  91          $io = new SymfonyStyle($input, $output);
  92  
  93          $showControllers = $input->getOption('show-controllers');
  94  
  95          $io->title(sprintf('%s Router Information', $this->getApplication()->getName()));
  96  
  97          if (empty($this->router->getRoutes()))
  98          {
  99              $io->warning('The router has no routes.');
 100  
 101              return 0;
 102          }
 103  
 104          $tableHeaders = [
 105              'Methods',
 106              'Pattern',
 107              'Rules',
 108          ];
 109  
 110          $tableRows = [];
 111  
 112          if ($showControllers)
 113          {
 114              $tableHeaders[] = 'Controller';
 115          }
 116  
 117          foreach ($this->router->getRoutes() as $route)
 118          {
 119              $row = [];
 120              $row[] = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
 121              $row[] = $route->getPattern();
 122  
 123              $rules = $route->getRules();
 124  
 125              if (empty($rules))
 126              {
 127                  $row[] = 'N/A';
 128              }
 129              else
 130              {
 131                  ksort($rules);
 132  
 133                  $rulesAsString = '';
 134  
 135                  foreach ($rules as $key => $value)
 136                  {
 137                      $rulesAsString .= sprintf("%s: %s\n", $key, $this->formatValue($value));
 138                  }
 139  
 140                  $row[] = new TableCell(rtrim($rulesAsString), ['rowspan' => count($rules)]);
 141              }
 142  
 143              if ($showControllers)
 144              {
 145                  $row[] = $this->formatCallable($route->getController());
 146              }
 147  
 148              $tableRows[] = $row;
 149          }
 150  
 151          $io->table($tableHeaders, $tableRows);
 152  
 153          return 0;
 154      }
 155  
 156      /**
 157       * Formats a callable resource to be displayed in the console output
 158       *
 159       * @param   callable  $callable  A callable resource to format
 160       *
 161       * @return  string
 162       *
 163       * @since   2.0.0
 164       * @throws  \ReflectionException
 165       * @note    This method is based on \Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor::formatCallable()
 166       */
 167  	private function formatCallable($callable): string
 168      {
 169          if (\is_array($callable))
 170          {
 171              if (\is_object($callable[0]))
 172              {
 173                  return sprintf('%s::%s()', \get_class($callable[0]), $callable[1]);
 174              }
 175  
 176              return sprintf('%s::%s()', $callable[0], $callable[1]);
 177          }
 178  
 179          if (\is_string($callable))
 180          {
 181              return sprintf('%s()', $callable);
 182          }
 183  
 184          if ($callable instanceof \Closure)
 185          {
 186              $r = new \ReflectionFunction($callable);
 187  
 188              if (strpos($r->name, '{closure}') !== false)
 189              {
 190                  return 'Closure()';
 191              }
 192  
 193              if ($class = $r->getClosureScopeClass())
 194              {
 195                  return sprintf('%s::%s()', $class->name, $r->name);
 196              }
 197  
 198              return $r->name . '()';
 199          }
 200  
 201          if (method_exists($callable, '__invoke'))
 202          {
 203              return sprintf('%s::__invoke()', \get_class($callable));
 204          }
 205  
 206          throw new \InvalidArgumentException('Callable is not describable.');
 207      }
 208  
 209      /**
 210       * Formats a value as string.
 211       *
 212       * @param   mixed  $value  A value to format
 213       *
 214       * @return  string
 215       *
 216       * @since   2.0.0
 217       * @note    This method is based on \Symfony\Bundle\FrameworkBundle\Console\Descriptor\Descriptor::formatValue()
 218       */
 219  	private function formatValue($value): string
 220      {
 221          if (\is_object($value))
 222          {
 223              return sprintf('object(%s)', \get_class($value));
 224          }
 225  
 226          if (\is_string($value))
 227          {
 228              return $value;
 229          }
 230  
 231          return preg_replace("/\n\s*/s", '', var_export($value, true));
 232      }
 233  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer