DelegatingLoader.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Routing;
  11. use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
  12. use Symfony\Component\Config\Loader\DelegatingLoader as BaseDelegatingLoader;
  13. use Symfony\Component\Config\Loader\LoaderResolverInterface;
  14. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  15. /**
  16. * DelegatingLoader delegates route loading to other loaders using a loader resolver.
  17. *
  18. * This implementation resolves the _controller attribute from the short notation
  19. * to the fully-qualified form (from a:b:c to class:method).
  20. *
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. */
  23. class DelegatingLoader extends BaseDelegatingLoader
  24. {
  25. protected $parser;
  26. protected $logger;
  27. /**
  28. * Constructor.
  29. *
  30. * @param ControllerNameParser $parser A ControllerNameParser instance
  31. * @param LoggerInterface $logger A LoggerInterface instance
  32. * @param LoaderResolverInterface $resolver A LoaderResolverInterface instance
  33. */
  34. public function __construct(ControllerNameParser $parser, LoggerInterface $logger = null, LoaderResolverInterface $resolver)
  35. {
  36. $this->parser = $parser;
  37. $this->logger = $logger;
  38. parent::__construct($resolver);
  39. }
  40. /**
  41. * Loads a resource.
  42. *
  43. * @param mixed $resource A resource
  44. * @param string $type The resource type
  45. *
  46. * @return RouteCollection A RouteCollection instance
  47. */
  48. public function load($resource, $type = null)
  49. {
  50. $collection = parent::load($resource, $type);
  51. foreach ($collection->all() as $name => $route) {
  52. if ($controller = $route->getDefault('_controller')) {
  53. try {
  54. $controller = $this->parser->parse($controller);
  55. } catch (\Exception $e) {
  56. // unable to optimize unknown notation
  57. }
  58. $route->setDefault('_controller', $controller);
  59. }
  60. }
  61. return $collection;
  62. }
  63. }