vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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\Console\Input\InputArgument;
       
    15 use Symfony\Component\Console\Input\InputOption;
       
    16 use Symfony\Component\Console\Input\InputInterface;
       
    17 use Symfony\Component\Console\Output\OutputInterface;
       
    18 use Symfony\Component\Console\Output\Output;
       
    19 use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
       
    20 
       
    21 /**
       
    22  * A console command for retrieving information about routes
       
    23  *
       
    24  * @author Fabien Potencier <fabien@symfony.com>
       
    25  */
       
    26 class RouterDebugCommand extends ContainerAwareCommand
       
    27 {
       
    28     /**
       
    29      * @see Command
       
    30      */
       
    31     protected function configure()
       
    32     {
       
    33         $this
       
    34             ->setDefinition(array(
       
    35                 new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
       
    36             ))
       
    37             ->setName('router:debug')
       
    38             ->setDescription('Displays current routes for an application')
       
    39             ->setHelp(<<<EOF
       
    40 The <info>router:debug</info> displays the configured routes:
       
    41 
       
    42   <info>router:debug</info>
       
    43 EOF
       
    44             )
       
    45         ;
       
    46     }
       
    47 
       
    48     /**
       
    49      * @see Command
       
    50      */
       
    51     protected function execute(InputInterface $input, OutputInterface $output)
       
    52     {
       
    53         $router = $this->getContainer()->get('router');
       
    54 
       
    55         $routes = array();
       
    56         foreach ($router->getRouteCollection()->all() as $name => $route) {
       
    57             $routes[$name] = $route->compile();
       
    58         }
       
    59 
       
    60         if ($input->getArgument('name')) {
       
    61             $this->outputRoute($output, $routes, $input->getArgument('name'));
       
    62         } else {
       
    63             $this->outputRoutes($output, $routes);
       
    64         }
       
    65     }
       
    66 
       
    67     protected function outputRoutes(OutputInterface $output, $routes)
       
    68     {
       
    69         $output->writeln($this->getHelper('formatter')->formatSection('router', 'Current routes'));
       
    70 
       
    71         $maxName = 4;
       
    72         $maxMethod = 6;
       
    73         foreach ($routes as $name => $route) {
       
    74             $requirements = $route->getRequirements();
       
    75             $method = isset($requirements['_method']) ? strtoupper(is_array($requirements['_method']) ? implode(', ', $requirements['_method']) : $requirements['_method']) : 'ANY';
       
    76 
       
    77             if (strlen($name) > $maxName) {
       
    78                 $maxName = strlen($name);
       
    79             }
       
    80 
       
    81             if (strlen($method) > $maxMethod) {
       
    82                 $maxMethod = strlen($method);
       
    83             }
       
    84         }
       
    85         $format  = '%-'.$maxName.'s %-'.$maxMethod.'s %s';
       
    86 
       
    87         // displays the generated routes
       
    88         $format1  = '%-'.($maxName + 19).'s %-'.($maxMethod + 19).'s %s';
       
    89         $output->writeln(sprintf($format1, '<comment>Name</comment>', '<comment>Method</comment>', '<comment>Pattern</comment>'));
       
    90         foreach ($routes as $name => $route) {
       
    91             $requirements = $route->getRequirements();
       
    92             $method = isset($requirements['_method']) ? strtoupper(is_array($requirements['_method']) ? implode(', ', $requirements['_method']) : $requirements['_method']) : 'ANY';
       
    93             $output->writeln(sprintf($format, $name, $method, $route->getPattern()));
       
    94         }
       
    95     }
       
    96 
       
    97     /**
       
    98      * @throws \InvalidArgumentException When route does not exist
       
    99      */
       
   100     protected function outputRoute(OutputInterface $output, $routes, $name)
       
   101     {
       
   102         $output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));
       
   103 
       
   104         if (!isset($routes[$name])) {
       
   105             throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
       
   106         }
       
   107 
       
   108         $route = $routes[$name];
       
   109         $output->writeln(sprintf('<comment>Name</comment>         %s', $name));
       
   110         $output->writeln(sprintf('<comment>Pattern</comment>      %s', $route->getPattern()));
       
   111         $output->writeln(sprintf('<comment>Class</comment>        %s', get_class($route)));
       
   112 
       
   113         $defaults = '';
       
   114         $d = $route->getDefaults();
       
   115         ksort($d);
       
   116         foreach ($d as $name => $value) {
       
   117             $defaults .= ($defaults ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
       
   118         }
       
   119         $output->writeln(sprintf('<comment>Defaults</comment>     %s', $defaults));
       
   120 
       
   121         $requirements = '';
       
   122         $r = $route->getRequirements();
       
   123         ksort($r);
       
   124         foreach ($r as $name => $value) {
       
   125             $requirements .= ($requirements ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
       
   126         }
       
   127         $output->writeln(sprintf('<comment>Requirements</comment> %s', $requirements));
       
   128 
       
   129         $options = '';
       
   130         $o = $route->getOptions();
       
   131         ksort($o);
       
   132         foreach ($o as $name => $value) {
       
   133             $options .= ($options ? "\n".str_repeat(' ', 13) : '').$name.': '.$this->formatValue($value);
       
   134         }
       
   135         $output->writeln(sprintf('<comment>Options</comment>      %s', $options));
       
   136         $output->write('<comment>Regex</comment>        ');
       
   137         $output->writeln(preg_replace('/^             /', '', preg_replace('/^/m', '             ', $route->getRegex())), OutputInterface::OUTPUT_RAW);
       
   138     }
       
   139 
       
   140     protected function formatValue($value)
       
   141     {
       
   142         if (is_object($value)) {
       
   143             return sprintf('object(%s)', get_class($value));
       
   144         }
       
   145 
       
   146         if (is_string($value)) {
       
   147             return $value;
       
   148         }
       
   149 
       
   150         return preg_replace("/\n\s*/s", '', var_export($value, true));
       
   151     }
       
   152 }