RouterApacheDumperCommand.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\Output;
  16. use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
  17. /**
  18. * RouterApacheDumperCommand.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class RouterApacheDumperCommand extends ContainerAwareCommand
  23. {
  24. /**
  25. * @see Command
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setDefinition(array(
  31. new InputArgument('script_name', InputArgument::OPTIONAL, 'The script name of the application\'s front controller.'),
  32. new InputOption('base-uri', null, InputOption::VALUE_REQUIRED, 'The base URI'),
  33. ))
  34. ->setName('router:dump-apache')
  35. ->setDescription('Dumps all routes as Apache rewrite rules')
  36. ->setHelp(<<<EOF
  37. The <info>router:dump-apache</info> dumps all routes as Apache rewrite rules.
  38. These can then be used with the ApacheUrlMatcher to use Apache for route
  39. matching.
  40. <info>router:dump-apache</info>
  41. EOF
  42. )
  43. ;
  44. }
  45. /**
  46. * @see Command
  47. */
  48. protected function execute(InputInterface $input, OutputInterface $output)
  49. {
  50. $router = $this->getContainer()->get('router');
  51. $dumpOptions = array();
  52. if ($input->getArgument('script_name')) {
  53. $dumpOptions['script_name'] = $input->getArgument('script_name');
  54. }
  55. if ($input->getOption('base-uri')) {
  56. $dumpOptions['base_uri'] = $input->getOption('base-uri');
  57. }
  58. $dumper = new ApacheMatcherDumper($router->getRouteCollection());
  59. $output->writeln($dumper->dump($dumpOptions), OutputInterface::OUTPUT_RAW);
  60. }
  61. }