[Routing] added new schemes and methods options to the annotation loader

This commit is contained in:
Fabien Potencier 2013-01-14 17:47:30 +01:00
parent 508299400d
commit 65eca8a3d9
4 changed files with 44 additions and 3 deletions

View File

@ -26,6 +26,8 @@ class Route
private $options; private $options;
private $defaults; private $defaults;
private $hostname; private $hostname;
private $methods;
private $schemes;
/** /**
* Constructor. * Constructor.
@ -37,6 +39,8 @@ class Route
$this->requirements = array(); $this->requirements = array();
$this->options = array(); $this->options = array();
$this->defaults = array(); $this->defaults = array();
$this->methods = array();
$this->schemes = array();
if (isset($data['value'])) { if (isset($data['value'])) {
$data['path'] = $data['value']; $data['path'] = $data['value'];
@ -127,4 +131,24 @@ class Route
{ {
return $this->defaults; return $this->defaults;
} }
public function setSchemes($schemes)
{
$this->schemes = is_array($schemes) ? $schemes : array($schemes);
}
public function getSchemes()
{
return $this->schemes;
}
public function setMethods($methods)
{
$this->methods = is_array($methods) ? $methods : array($methods);
}
public function getMethods()
{
return $this->methods;
}
} }

View File

@ -29,8 +29,9 @@ use Symfony\Component\Config\Loader\LoaderResolverInterface;
* and on each method. * and on each method.
* *
* The @Route annotation main value is the route path. The annotation also * The @Route annotation main value is the route path. The annotation also
* recognizes three parameters: requirements, options, and name. The name parameter * recognizes several parameters: requirements, options, defaults, schemes,
* is mandatory. Here is an example of how you should be able to use it: * methods, hostname, and name. The name parameter is mandatory.
* Here is an example of how you should be able to use it:
* *
* /** * /**
* * @Route("/Blog") * * @Route("/Blog")
@ -112,6 +113,8 @@ abstract class AnnotationClassLoader implements LoaderInterface
'requirements' => array(), 'requirements' => array(),
'options' => array(), 'options' => array(),
'defaults' => array(), 'defaults' => array(),
'schemes' => array(),
'methods' => array(),
'hostname' => '', 'hostname' => '',
); );
@ -140,6 +143,14 @@ abstract class AnnotationClassLoader implements LoaderInterface
$globals['defaults'] = $annot->getDefaults(); $globals['defaults'] = $annot->getDefaults();
} }
if (null !== $annot->getSchemes()) {
$globals['schemes'] = $annot->getSchemes();
}
if (null !== $annot->getMethods()) {
$globals['methods'] = $annot->getMethods();
}
if (null !== $annot->getHostname()) { if (null !== $annot->getHostname()) {
$globals['hostname'] = $annot->getHostname(); $globals['hostname'] = $annot->getHostname();
} }
@ -175,13 +186,15 @@ abstract class AnnotationClassLoader implements LoaderInterface
} }
$requirements = array_replace($globals['requirements'], $annot->getRequirements()); $requirements = array_replace($globals['requirements'], $annot->getRequirements());
$options = array_replace($globals['options'], $annot->getOptions()); $options = array_replace($globals['options'], $annot->getOptions());
$schemes = array_replace($globals['schemes'], $annot->getSchemes());
$methods = array_replace($globals['methods'], $annot->getMethods());
$hostname = $annot->getHostname(); $hostname = $annot->getHostname();
if (null === $hostname) { if (null === $hostname) {
$hostname = $globals['hostname']; $hostname = $globals['hostname'];
} }
$route = new Route($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $hostname); $route = new Route($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $hostname, $schemes, $methods);
$this->configureRoute($route, $class, $method, $annot); $this->configureRoute($route, $class, $method, $annot);

View File

@ -41,6 +41,8 @@ class RouteTest extends \PHPUnit_Framework_TestCase
array('options', array('compiler_class' => 'RouteCompiler'), 'getOptions'), array('options', array('compiler_class' => 'RouteCompiler'), 'getOptions'),
array('name', 'blog_index', 'getName'), array('name', 'blog_index', 'getName'),
array('defaults', array('_controller' => 'MyBlogBundle:Blog:index'), 'getDefaults'), array('defaults', array('_controller' => 'MyBlogBundle:Blog:index'), 'getDefaults'),
array('schemes', array('https'), 'getSchemes'),
array('methods', array('GET', 'POST'), 'getMethods'),
array('hostname', array('{locale}.example.com'), 'getHostname') array('hostname', array('{locale}.example.com'), 'getHostname')
); );
} }

View File

@ -93,6 +93,8 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
'requirements' => array(), 'requirements' => array(),
'options' => array(), 'options' => array(),
'defaults' => array(), 'defaults' => array(),
'schemes' => array(),
'methods' => array(),
), $routeDatas); ), $routeDatas);
$this->reader $this->reader