2015-08-17 17:00:26 -07:00
< ? php
/*
* This file is part of the Symfony CMF package .
*
2016-10-06 15:16:20 -07:00
* ( c ) 2011 - 2015 Symfony CMF
2015-08-17 17:00:26 -07:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Cmf\Component\Routing ;
use Symfony\Component\Routing\Generator\UrlGenerator ;
use Symfony\Component\Routing\Route as SymfonyRoute ;
use Symfony\Component\Routing\RequestContext ;
use Symfony\Component\Routing\Exception\RouteNotFoundException ;
use Psr\Log\LoggerInterface ;
/**
2016-10-06 15:16:20 -07:00
* A Generator that uses a RouteProvider rather than a RouteCollection .
2015-08-17 17:00:26 -07:00
*
* @ author Larry Garfield
*/
class ProviderBasedGenerator extends UrlGenerator implements VersatileGeneratorInterface
{
/**
* The route provider for this generator .
*
* @ var RouteProviderInterface
*/
protected $provider ;
/**
* @ param RouteProviderInterface $provider
* @ param LoggerInterface $logger
*/
public function __construct ( RouteProviderInterface $provider , LoggerInterface $logger = null )
{
$this -> provider = $provider ;
$this -> logger = $logger ;
$this -> context = new RequestContext ();
}
/**
2016-10-06 15:16:20 -07:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
2016-10-06 15:16:20 -07:00
public function generate ( $name , $parameters = array (), $referenceType = self :: ABSOLUTE_PATH )
2015-08-17 17:00:26 -07:00
{
if ( $name instanceof SymfonyRoute ) {
$route = $name ;
2016-10-06 15:16:20 -07:00
} elseif ( null === $route = $this -> provider -> getRouteByName ( $name )) {
2015-08-17 17:00:26 -07:00
throw new RouteNotFoundException ( sprintf ( 'Route "%s" does not exist.' , $name ));
}
// the Route has a cache of its own and is not recompiled as long as it does not get modified
$compiledRoute = $route -> compile ();
$hostTokens = $compiledRoute -> getHostTokens ();
$debug_message = $this -> getRouteDebugMessage ( $name );
2016-10-06 15:16:20 -07:00
return $this -> doGenerate ( $compiledRoute -> getVariables (), $route -> getDefaults (), $route -> getRequirements (), $compiledRoute -> getTokens (), $parameters , $debug_message , $referenceType , $hostTokens );
2015-08-17 17:00:26 -07:00
}
/**
2016-10-06 15:16:20 -07:00
* Support a route object and any string as route name .
2015-08-17 17:00:26 -07:00
*
2016-10-06 15:16:20 -07:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function supports ( $name )
{
return is_string ( $name ) || $name instanceof SymfonyRoute ;
}
/**
2016-10-06 15:16:20 -07:00
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
public function getRouteDebugMessage ( $name , array $parameters = array ())
{
if ( is_scalar ( $name )) {
return $name ;
}
if ( is_array ( $name )) {
return serialize ( $name );
}
if ( $name instanceof RouteObjectInterface ) {
2016-10-06 15:16:20 -07:00
return 'Route with key ' . $name -> getRouteKey ();
2015-08-17 17:00:26 -07:00
}
if ( $name instanceof SymfonyRoute ) {
2016-10-06 15:16:20 -07:00
return 'Route with path ' . $name -> getPath ();
2015-08-17 17:00:26 -07:00
}
return get_class ( $name );
}
}