vendor/symfony/framework-bundle/Routing/DelegatingLoader.php line 67

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\Component\Config\Exception\LoaderLoadException;
  12. use Symfony\Component\Config\Loader\DelegatingLoader as BaseDelegatingLoader;
  13. use Symfony\Component\Config\Loader\LoaderResolverInterface;
  14. /**
  15.  * DelegatingLoader delegates route loading to other loaders using a loader resolver.
  16.  *
  17.  * This implementation resolves the _controller attribute from the short notation
  18.  * to the fully-qualified form (from a:b:c to class::method).
  19.  *
  20.  * @author Fabien Potencier <fabien@symfony.com>
  21.  *
  22.  * @final
  23.  */
  24. class DelegatingLoader extends BaseDelegatingLoader
  25. {
  26.     private $loading false;
  27.     private $defaultOptions;
  28.     public function __construct(LoaderResolverInterface $resolver, array $defaultOptions = [])
  29.     {
  30.         $this->defaultOptions $defaultOptions;
  31.         parent::__construct($resolver);
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function load($resourcestring $type null)
  37.     {
  38.         if ($this->loading) {
  39.             // This can happen if a fatal error occurs in parent::load().
  40.             // Here is the scenario:
  41.             // - while routes are being loaded by parent::load() below, a fatal error
  42.             //   occurs (e.g. parse error in a controller while loading annotations);
  43.             // - PHP abruptly empties the stack trace, bypassing all catch/finally blocks;
  44.             //   it then calls the registered shutdown functions;
  45.             // - the ErrorHandler catches the fatal error and re-injects it for rendering
  46.             //   thanks to HttpKernel->terminateWithException() (that calls handleException());
  47.             // - at this stage, if we try to load the routes again, we must prevent
  48.             //   the fatal error from occurring a second time,
  49.             //   otherwise the PHP process would be killed immediately;
  50.             // - while rendering the exception page, the router can be required
  51.             //   (by e.g. the web profiler that needs to generate an URL);
  52.             // - this handles the case and prevents the second fatal error
  53.             //   by triggering an exception beforehand.
  54.             throw new LoaderLoadException($resourcenullnullnull$type);
  55.         }
  56.         $this->loading true;
  57.         try {
  58.             $collection parent::load($resource$type);
  59.         } finally {
  60.             $this->loading false;
  61.         }
  62.         foreach ($collection->all() as $route) {
  63.             if ($this->defaultOptions) {
  64.                 $route->setOptions($route->getOptions() + $this->defaultOptions);
  65.             }
  66.             if (!\is_string($controller $route->getDefault('_controller'))) {
  67.                 continue;
  68.             }
  69.             if (false !== strpos($controller'::')) {
  70.                 continue;
  71.             }
  72.             $route->setDefault('_controller'$controller);
  73.         }
  74.         return $collection;
  75.     }
  76. }