vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Routing/DelegatingLoader.php line 70

Open in your IDE?
  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\Routing;
  11. use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
  12. use Symfony\Component\Config\Exception\FileLoaderLoadException;
  13. use Symfony\Component\Config\Loader\DelegatingLoader as BaseDelegatingLoader;
  14. use Symfony\Component\Config\Loader\LoaderResolverInterface;
  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@symfony.com>
  22.  */
  23. class DelegatingLoader extends BaseDelegatingLoader
  24. {
  25.     protected $parser;
  26.     private $loading false;
  27.     /**
  28.      * @param ControllerNameParser    $parser   A ControllerNameParser instance
  29.      * @param LoaderResolverInterface $resolver A LoaderResolverInterface instance
  30.      */
  31.     public function __construct(ControllerNameParser $parserLoaderResolverInterface $resolver)
  32.     {
  33.         $this->parser $parser;
  34.         parent::__construct($resolver);
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function load($resource$type null)
  40.     {
  41.         if ($this->loading) {
  42.             // This can happen if a fatal error occurs in parent::load().
  43.             // Here is the scenario:
  44.             // - while routes are being loaded by parent::load() below, a fatal error
  45.             //   occurs (e.g. parse error in a controller while loading annotations);
  46.             // - PHP abruptly empties the stack trace, bypassing all catch/finally blocks;
  47.             //   it then calls the registered shutdown functions;
  48.             // - the ErrorHandler catches the fatal error and re-injects it for rendering
  49.             //   thanks to HttpKernel->terminateWithException() (that calls handleException());
  50.             // - at this stage, if we try to load the routes again, we must prevent
  51.             //   the fatal error from occurring a second time,
  52.             //   otherwise the PHP process would be killed immediately;
  53.             // - while rendering the exception page, the router can be required
  54.             //   (by e.g. the web profiler that needs to generate an URL);
  55.             // - this handles the case and prevents the second fatal error
  56.             //   by triggering an exception beforehand.
  57.             throw new FileLoaderLoadException($resourcenullnullnull$type);
  58.         }
  59.         $this->loading true;
  60.         try {
  61.             $collection parent::load($resource$type);
  62.         } finally {
  63.             $this->loading false;
  64.         }
  65.         foreach ($collection->all() as $route) {
  66.             if (!\is_string($controller $route->getDefault('_controller')) || !$controller) {
  67.                 continue;
  68.             }
  69.             try {
  70.                 $controller $this->parser->parse($controller);
  71.             } catch (\InvalidArgumentException $e) {
  72.                 // unable to optimize unknown notation
  73.             }
  74.             $route->setDefault('_controller'$controller);
  75.         }
  76.         return $collection;
  77.     }
  78. }