vendor/symfony-cmf/routing/src/ProviderBasedGenerator.php line 58

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony CMF package.
  4.  *
  5.  * (c) Symfony CMF
  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\Cmf\Component\Routing;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Routing\Exception\RouteNotFoundException;
  13. use Symfony\Component\Routing\Generator\UrlGenerator;
  14. use Symfony\Component\Routing\RequestContext;
  15. use Symfony\Component\Routing\Route as SymfonyRoute;
  16. /**
  17.  * A Generator that uses a RouteProvider rather than a RouteCollection.
  18.  *
  19.  * @author Larry Garfield
  20.  */
  21. class ProviderBasedGenerator extends UrlGenerator implements VersatileGeneratorInterface
  22. {
  23.     /**
  24.      * The route provider for this generator.
  25.      *
  26.      * @var RouteProviderInterface
  27.      */
  28.     protected $provider;
  29.     /**
  30.      * @param LoggerInterface $logger
  31.      */
  32.     public function __construct(RouteProviderInterface $providerLoggerInterface $logger null)
  33.     {
  34.         $this->provider $provider;
  35.         $this->logger $logger;
  36.         $this->context = new RequestContext();
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      *
  41.      * The CMF routing system used to allow to pass route objects as $name to generate the route.
  42.      * Since Symfony 5.0, the UrlGeneratorInterface declares $name as string. We widen the contract
  43.      * for BC but deprecate passing non-strings.
  44.      * Instead, Pass the RouteObjectInterface::OBJECT_BASED_ROUTE_NAME as route name and the object
  45.      * in the parameters with key RouteObjectInterface::ROUTE_OBJECT.
  46.      *
  47.      * @param mixed $name
  48.      */
  49.     public function generate($name$parameters = [], $referenceType self::ABSOLUTE_PATH)
  50.     {
  51.         if (is_object($name)) {
  52.             @trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`'E_USER_DEPRECATED);
  53.         }
  54.         if ($name instanceof SymfonyRoute) {
  55.             $route $name;
  56.         } elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
  57.             && array_key_exists(RouteObjectInterface::ROUTE_OBJECT$parameters)
  58.             && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute
  59.         ) {
  60.             $route $parameters[RouteObjectInterface::ROUTE_OBJECT];
  61.             unset($parameters[RouteObjectInterface::ROUTE_OBJECT]);
  62.         } elseif (null === $route $this->provider->getRouteByName($name)) {
  63.             throw new RouteNotFoundException(sprintf('Route "%s" does not exist.'$name));
  64.         }
  65.         // the Route has a cache of its own and is not recompiled as long as it does not get modified
  66.         $compiledRoute $route->compile();
  67.         $hostTokens $compiledRoute->getHostTokens();
  68.         $debug_message $this->getRouteDebugMessage($name);
  69.         return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters$debug_message$referenceType$hostTokens);
  70.     }
  71.     /**
  72.      * Support a route object and any string as route name.
  73.      *
  74.      * {@inheritdoc}
  75.      */
  76.     public function supports($name)
  77.     {
  78.         return is_string($name) || $name instanceof SymfonyRoute;
  79.     }
  80.     /**
  81.      * {@inheritdoc}
  82.      */
  83.     public function getRouteDebugMessage($name, array $parameters = [])
  84.     {
  85.         if (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
  86.             && array_key_exists(RouteObjectInterface::ROUTE_OBJECT$parameters)
  87.         ) {
  88.             $routeObject $parameters[RouteObjectInterface::ROUTE_OBJECT];
  89.             if ($routeObject instanceof RouteObjectInterface) {
  90.                 return 'Route with key '.$routeObject->getRouteKey();
  91.             }
  92.             if ($routeObject instanceof SymfonyRoute) {
  93.                 return 'Route with path '.$routeObject->getPath();
  94.             }
  95.             if (is_object($routeObject)) {
  96.                 return get_class($routeObject);
  97.             }
  98.             return 'Null route';
  99.         }
  100.         if (is_scalar($name)) {
  101.             return $name;
  102.         }
  103.         // legacy
  104.         if (is_array($name)) {
  105.             return serialize($name);
  106.         }
  107.         if ($name instanceof RouteObjectInterface) {
  108.             return 'Route with key '.$name->getRouteKey();
  109.         }
  110.         if ($name instanceof SymfonyRoute) {
  111.             return 'Route with path '.$name->getPath();
  112.         }
  113.         if (is_object($name)) {
  114.             return get_class($name);
  115.         }
  116.         return 'Null route';
  117.     }
  118. }