|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSRestBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Routing\Loader; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\RestBundle\Routing\Loader\Reader\RestControllerReader; |
|
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser; |
|
16
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
|
17
|
|
|
use Symfony\Component\Config\Loader\Loader; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* RestRouteLoader REST-enabled controller router loader. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
|
25
|
|
|
* @author Bulat Shakirzyanov <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class RestRouteLoader extends Loader |
|
28
|
|
|
{ |
|
29
|
|
|
protected $container; |
|
30
|
|
|
protected $controllerParser; |
|
31
|
|
|
protected $controllerReader; |
|
32
|
|
|
protected $defaultFormat; |
|
33
|
|
|
protected $locator; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Initializes loader. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ContainerInterface $container |
|
39
|
|
|
* @param FileLocatorInterface $locator |
|
40
|
|
|
* @param ControllerNameParser $controllerParser |
|
41
|
|
|
* @param RestControllerReader $controllerReader |
|
42
|
|
|
* @param string $defaultFormat |
|
43
|
|
|
*/ |
|
44
|
39 |
|
public function __construct( |
|
45
|
|
|
ContainerInterface $container, |
|
46
|
|
|
FileLocatorInterface $locator, |
|
47
|
|
|
ControllerNameParser $controllerParser, |
|
48
|
|
|
RestControllerReader $controllerReader, $defaultFormat = 'html' |
|
49
|
|
|
) { |
|
50
|
39 |
|
$this->container = $container; |
|
51
|
39 |
|
$this->locator = $locator; |
|
52
|
39 |
|
$this->controllerParser = $controllerParser; |
|
53
|
39 |
|
$this->controllerReader = $controllerReader; |
|
54
|
39 |
|
$this->defaultFormat = $defaultFormat; |
|
55
|
39 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns controller reader. |
|
59
|
|
|
* |
|
60
|
|
|
* @return RestControllerReader |
|
61
|
|
|
*/ |
|
62
|
23 |
|
public function getControllerReader() |
|
63
|
1 |
|
{ |
|
64
|
23 |
|
return $this->controllerReader; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
24 |
|
public function load($controller, $type = null) |
|
71
|
|
|
{ |
|
72
|
24 |
|
list($prefix, $class) = $this->getControllerLocator($controller); |
|
73
|
|
|
|
|
74
|
24 |
|
$collection = $this->controllerReader->read(new \ReflectionClass($class)); |
|
75
|
24 |
|
$collection->prependRouteControllersWithPrefix($prefix); |
|
76
|
24 |
|
$collection->setDefaultFormat($this->defaultFormat); |
|
77
|
|
|
|
|
78
|
24 |
|
return $collection; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
9 |
|
public function supports($resource, $type = null) |
|
85
|
|
|
{ |
|
86
|
9 |
|
return is_string($resource) |
|
87
|
9 |
|
&& 'rest' === $type |
|
88
|
9 |
|
&& !in_array(pathinfo($resource, PATHINFO_EXTENSION), ['xml', 'yml'] |
|
89
|
9 |
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns controller locator by it's id. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $controller |
|
96
|
|
|
* |
|
97
|
|
|
* @throws \InvalidArgumentException |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
24 |
|
private function getControllerLocator($controller) |
|
102
|
|
|
{ |
|
103
|
24 |
|
$class = null; |
|
104
|
24 |
|
$prefix = null; |
|
105
|
|
|
|
|
106
|
24 |
|
if (0 === strpos($controller, '@')) { |
|
107
|
|
|
$file = $this->locator->locate($controller); |
|
108
|
|
|
$controllerClass = $this->findClass($file); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
if (false === $controllerClass) { |
|
111
|
|
|
throw new \InvalidArgumentException(sprintf('Can\'t find class for controller "%s"', $controller)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$controller = $controllerClass; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
24 |
|
if ($this->container->has($controller)) { |
|
118
|
|
|
// service_id |
|
119
|
1 |
|
$prefix = $controller.':'; |
|
120
|
1 |
|
if (method_exists($this->container, 'enterScope')) { |
|
121
|
1 |
|
$this->container->enterScope('request'); |
|
|
|
|
|
|
122
|
1 |
|
$this->container->set('request', new Request()); |
|
123
|
1 |
|
} |
|
124
|
1 |
|
$class = get_class($this->container->get($controller)); |
|
125
|
1 |
|
if (method_exists($this->container, 'enterScope')) { |
|
126
|
1 |
|
$this->container->leaveScope('request'); |
|
|
|
|
|
|
127
|
1 |
|
} |
|
128
|
24 |
|
} elseif (class_exists($controller)) { |
|
129
|
|
|
// full class name |
|
130
|
24 |
|
$class = $controller; |
|
131
|
24 |
|
$prefix = $class.'::'; |
|
132
|
24 |
|
} elseif (false !== strpos($controller, ':')) { |
|
133
|
|
|
// bundle:controller notation |
|
134
|
|
|
try { |
|
135
|
|
|
$notation = $this->controllerParser->parse($controller.':method'); |
|
136
|
|
|
list($class) = explode('::', $notation); |
|
137
|
|
|
$prefix = $class.'::'; |
|
138
|
|
|
} catch (\Exception $e) { |
|
139
|
|
|
throw new \InvalidArgumentException( |
|
140
|
|
|
sprintf('Can\'t locate "%s" controller.', $controller) |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
24 |
|
if (empty($class)) { |
|
146
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
147
|
|
|
'Class could not be determined for Controller identified by "%s".', $controller |
|
148
|
|
|
)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
24 |
|
return [$prefix, $class]; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Returns the full class name for the first class in the file. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $file A PHP file path |
|
158
|
|
|
* |
|
159
|
|
|
* @return string|false Full class name if found, false otherwise |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function findClass($file) |
|
162
|
|
|
{ |
|
163
|
|
|
$class = false; |
|
164
|
|
|
$namespace = false; |
|
165
|
|
|
$tokens = token_get_all(file_get_contents($file)); |
|
166
|
|
|
for ($i = 0, $count = count($tokens); $i < $count; ++$i) { |
|
167
|
|
|
$token = $tokens[$i]; |
|
168
|
|
|
|
|
169
|
|
|
if (!is_array($token)) { |
|
170
|
|
|
continue; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (true === $class && T_STRING === $token[0]) { |
|
174
|
|
|
return $namespace.'\\'.$token[1]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if (true === $namespace && T_STRING === $token[0]) { |
|
178
|
|
|
$namespace = ''; |
|
179
|
|
|
do { |
|
180
|
|
|
$namespace .= $token[1]; |
|
181
|
|
|
$token = $tokens[++$i]; |
|
182
|
|
|
} while ($i < $count && is_array($token) && in_array($token[0], [T_NS_SEPARATOR, T_STRING])); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (T_CLASS === $token[0]) { |
|
186
|
|
|
$class = true; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if (T_NAMESPACE === $token[0]) { |
|
190
|
|
|
$namespace = true; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return false; |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.