-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathAnnotatedRouteControllerLoader.php
More file actions
169 lines (145 loc) · 6.31 KB
/
AnnotatedRouteControllerLoader.php
File metadata and controls
169 lines (145 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace BeSimple\I18nRoutingBundle\Routing\Loader;
use BeSimple\I18nRoutingBundle\Routing\Annotation\I18nRoute;
use BeSimple\I18nRoutingBundle\Routing\Exception\MissingLocaleException;
use BeSimple\I18nRoutingBundle\Routing\Exception\MissingRouteLocaleException;
use BeSimple\I18nRoutingBundle\Routing\RouteGenerator\I18nRouteGenerator;
use BeSimple\I18nRoutingBundle\Routing\RouteGenerator\RouteGeneratorInterface;
use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* This class is partially a copy of @see \Sensio\Bundle\FrameworkExtraBundle\Routing\AnnotatedRouteControllerLoader.
*/
class AnnotatedRouteControllerLoader extends AnnotationClassLoader
{
/**
* @var RouteGeneratorInterface
*/
private $routeGenerator;
public function __construct(Reader $reader, RouteGeneratorInterface $routeGenerator = null)
{
parent::__construct($reader);
$this->routeGenerator = $routeGenerator ?: new I18nRouteGenerator();
$this->setRouteAnnotationClass('Symfony\\Component\\Routing\\Annotation\\Route');
}
protected function addRoute(RouteCollection $collection, $annot, $globals, \ReflectionClass $class, \ReflectionMethod $method)
{
if (!$annot instanceof I18nRoute) {
parent::addRoute($collection, $annot, $globals, $class, $method);
return;
}
$name = $annot->getName();
if (null === $name) {
$name = $this->getDefaultRouteName($class, $method);
}
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
foreach ($method->getParameters() as $param) {
if (!isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
$defaults[$param->getName()] = $param->getDefaultValue();
}
}
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
$options = array_replace($globals['options'], $annot->getOptions());
$schemes = array_merge($globals['schemes'], $annot->getSchemes());
$methods = array_merge($globals['methods'], $annot->getMethods());
$host = $annot->getHost();
if (null === $host) {
$host = $globals['host'];
}
$condition = $annot->getCondition();
if (null === $condition && isset($globals['condition'])) {
$condition = $globals['condition'];
}
$path = '';
$localesWithPaths = $annot->getLocales();
if (is_scalar($localesWithPaths)) {
$routePath = $localesWithPaths;
if (!is_array($globals['locales'])) {
// This is a normal route
$path = $globals['locales'].$localesWithPaths;
$localesWithPaths = null;
} else {
// Global contains the locales
$localesWithPaths = array();
foreach ($globals['locales'] as $locale => $localePath) {
$localesWithPaths[$locale] = $localePath.$routePath;
}
}
} elseif (is_array($localesWithPaths) && !empty($globals['locales'])) {
if (!is_array($globals['locales'])) {
// Global is a normal prefix
foreach ($localesWithPaths as $locale => $localePath) {
$localesWithPaths[$locale] = $globals['locales'].$localePath;
}
} else {
foreach ($localesWithPaths as $locale => $localePath) {
if (!isset($globals['locales'][$locale])) {
throw new MissingLocaleException(sprintf('Locale "%s" for controller %s::%s is expected to be part of the global configuration at class level.', $locale, $class->getName(), $method->getName()));
}
$localesWithPaths[$locale] = $globals['locales'][$locale].$localePath;
}
}
} elseif (!is_array($localesWithPaths)) {
throw new MissingRouteLocaleException(sprintf('Missing locales for controller %s::%s', $class->getName(), $method->getName()));
}
$route = $this->createRoute($path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
$this->configureRoute($route, $class, $method, $annot);
if (null === $localesWithPaths) {
// Standard route
$collection->add($name, $route);
return;
}
$collection->addCollection(
$this->routeGenerator->generateRoutes($name, $localesWithPaths, $route)
);
}
/**
* @inheritdoc
*/
protected function getGlobals(\ReflectionClass $class)
{
$globals = array(
'locales' => '',
);
if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
if ($annot instanceof I18nRoute && null !== $annot->getLocales()) {
$globals['locales'] = $annot->getLocales();
}
}
return array_merge(parent::getGlobals($class), $globals);
}
/**
* Configures the _controller default parameter of a given Route instance.
*
* @param Route $route A route instance
* @param \ReflectionClass $class A ReflectionClass instance
* @param \ReflectionMethod $method A ReflectionClass method
* @param mixed $annot The annotation class instance
*
* @throws \LogicException When the service option is specified on a method
*/
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
{
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
}
/**
* @inheritdoc
*
* @see \Sensio\Bundle\FrameworkExtraBundle\Routing\AnnotatedRouteControllerLoader::getDefaultRouteName
*/
protected function getDefaultRouteName(\ReflectionClass $class, \ReflectionMethod $method)
{
$routeName = parent::getDefaultRouteName($class, $method);
return preg_replace(array(
'/(bundle|controller)_/',
'/action(_\d+)?$/',
'/__/',
), array(
'_',
'\\1',
'_',
), $routeName);
}
}