RouterApacheDumperCommand.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
  17. use Symfony\Component\Routing\RouterInterface;
  18. /**
  19. * RouterApacheDumperCommand.
  20. *
  21. * @deprecated since version 2.5, to be removed in 3.0.
  22. * The performance gains are minimal and it's very hard to replicate
  23. * the behavior of PHP implementation.
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. */
  27. class RouterApacheDumperCommand extends ContainerAwareCommand
  28. {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function isEnabled()
  33. {
  34. if (!$this->getContainer()->has('router')) {
  35. return false;
  36. }
  37. $router = $this->getContainer()->get('router');
  38. if (!$router instanceof RouterInterface) {
  39. return false;
  40. }
  41. return parent::isEnabled();
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function configure()
  47. {
  48. $this
  49. ->setName('router:dump-apache')
  50. ->setDefinition(array(
  51. new InputArgument('script_name', InputArgument::OPTIONAL, 'The script name of the application\'s front controller'),
  52. new InputOption('base-uri', null, InputOption::VALUE_REQUIRED, 'The base URI'),
  53. ))
  54. ->setDescription('[DEPRECATED] Dumps all routes as Apache rewrite rules')
  55. ->setHelp(<<<'EOF'
  56. The <info>%command.name%</info> dumps all routes as Apache rewrite rules.
  57. These can then be used with the ApacheUrlMatcher to use Apache for route
  58. matching.
  59. <info>php %command.full_name%</info>
  60. EOF
  61. )
  62. ;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. protected function execute(InputInterface $input, OutputInterface $output)
  68. {
  69. $io = new SymfonyStyle($input, $output);
  70. $io->title('Router Apache Dumper');
  71. $io->caution('The router:dump-apache command is deprecated since version 2.5 and will be removed in 3.0.');
  72. $router = $this->getContainer()->get('router');
  73. $dumpOptions = array();
  74. if ($input->getArgument('script_name')) {
  75. $dumpOptions['script_name'] = $input->getArgument('script_name');
  76. }
  77. if ($input->getOption('base-uri')) {
  78. $dumpOptions['base_uri'] = $input->getOption('base-uri');
  79. }
  80. $dumper = new ApacheMatcherDumper($router->getRouteCollection());
  81. $io->writeln($dumper->dump($dumpOptions), OutputInterface::OUTPUT_RAW);
  82. }
  83. }