|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
namespace Sensio\Bundle\FrameworkExtraBundle\Routing; |
|
|
4 |
|
|
|
5 |
use Symfony\Component\Routing\Loader\AnnotationClassLoader; |
|
|
6 |
use Symfony\Component\Routing\Route; |
|
|
7 |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\AnnotationReader as ConfigurationAnnotationReader; |
|
|
8 |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
|
9 |
|
|
|
10 |
/* |
|
|
11 |
* This file is part of the Symfony framework. |
|
|
12 |
* |
|
|
13 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
14 |
* |
|
|
15 |
* This source file is subject to the MIT license that is bundled |
|
|
16 |
* with this source code in the file LICENSE. |
|
|
17 |
*/ |
|
|
18 |
|
|
|
19 |
/** |
|
|
20 |
* AnnotatedRouteControllerLoader is an implementation of AnnotationClassLoader |
|
|
21 |
* that sets the '_controller' default based on the class and method names. |
|
|
22 |
* |
|
|
23 |
* It also parse the @Method annotation. |
|
|
24 |
* |
|
|
25 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
26 |
*/ |
|
|
27 |
class AnnotatedRouteControllerLoader extends AnnotationClassLoader |
|
|
28 |
{ |
|
|
29 |
/** |
|
|
30 |
* Configures the _controller default parameter and eventually the _method |
|
|
31 |
* requirement of a given Route instance. |
|
|
32 |
* |
|
|
33 |
* @param Route $route A Route instance |
|
|
34 |
* @param ReflectionClass $class A ReflectionClass instance |
|
|
35 |
* @param ReflectionMethod $method A ReflectionClass method |
|
|
36 |
*/ |
|
|
37 |
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot) |
|
|
38 |
{ |
|
|
39 |
// controller |
|
|
40 |
$classAnnot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass); |
|
|
41 |
if ($classAnnot && $service = $classAnnot->getService()) { |
|
|
42 |
$route->setDefault('_controller', $service.':'.$method->getName()); |
|
|
43 |
} else { |
|
|
44 |
$route->setDefault('_controller', $class->getName().'::'.$method->getName()); |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
// requirements (@Method) |
|
|
48 |
foreach ($this->reader->getMethodAnnotations($method) as $configuration) { |
|
|
49 |
if ($configuration instanceof Method) { |
|
|
50 |
$route->setRequirement('_method', implode('|', $configuration->getMethods())); |
|
|
51 |
} |
|
|
52 |
} |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
/** |
|
|
56 |
* Makes the default route name more sane by removing common keywords. |
|
|
57 |
* |
|
|
58 |
* @param ReflectionClass $class A ReflectionClass instance |
|
|
59 |
* @param ReflectionMethod $method A ReflectionMethod instance |
|
|
60 |
* @return string |
|
|
61 |
*/ |
|
|
62 |
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method) |
|
|
63 |
{ |
|
|
64 |
$routeName = parent::getDefaultRouteName($class, $method); |
|
|
65 |
|
|
|
66 |
return preg_replace(array( |
|
|
67 |
'/(bundle|controller)_/', |
|
|
68 |
'/action(_\d+)?$/', |
|
|
69 |
'/__/' |
|
|
70 |
), array( |
|
|
71 |
'_', |
|
|
72 |
'\\1', |
|
|
73 |
'_' |
|
|
74 |
), $routeName); |
|
|
75 |
} |
|
|
76 |
} |