This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: VITALYIEGOROV |
||
5 | * Date: 26.10.15 |
||
6 | * Time: 10:50 |
||
7 | */ |
||
8 | namespace samsonphp\router; |
||
9 | |||
10 | use \samsonframework\routing\RouteCollection; |
||
11 | use \samsonframework\routing\Route; |
||
12 | use \samsonphp\event\Event; |
||
13 | |||
14 | /** |
||
15 | * This class is needed to generate routes for old SamsonPHP modules |
||
16 | * @package samsonphp\router |
||
17 | */ |
||
18 | class GenericRouteGenerator |
||
19 | { |
||
20 | /** Default controller name */ |
||
21 | const CTR_BASE = '__base'; |
||
22 | const CTR_CACHE_BASE = '__cache_base'; |
||
23 | |||
24 | /** Universal controller name */ |
||
25 | const CTR_UNI = '__handler'; |
||
26 | const CTR_CACHE_UNI = '__cache_handler'; |
||
27 | |||
28 | /** Post controller name */ |
||
29 | const CTR_POST = '__post'; |
||
30 | const CTR_CACHE_POST = '__cache_post'; |
||
31 | |||
32 | /** Put controller name */ |
||
33 | const CTR_PUT = '__put'; |
||
34 | const CTR_CACHE_PUT = '__cache_put'; |
||
35 | |||
36 | /** Delete controller name */ |
||
37 | const CTR_DELETE = '__delete'; |
||
38 | const CTR_CACHE_DELETE = '__cache_delete'; |
||
39 | |||
40 | /** Delete controller name */ |
||
41 | const CTR_UPDATE = '__update'; |
||
42 | const CTR_CACHE_UPDATE = '__cache_update'; |
||
43 | |||
44 | /** Controllers naming conventions */ |
||
45 | |||
46 | /** Procedural controller prefix */ |
||
47 | const PROC_PREFIX = '_'; |
||
48 | /** OOP controller prefix */ |
||
49 | const OBJ_PREFIX = '__'; |
||
50 | /** AJAX controller prefix */ |
||
51 | const ASYNC_PREFIX = 'async_'; |
||
52 | /** CACHE controller prefix */ |
||
53 | const CACHE_PREFIX = 'cache_'; |
||
54 | //[PHPCOMPRESSOR(remove,start)] |
||
55 | |||
56 | /** @var RouteCollection Generated routes collection */ |
||
57 | protected $routes; |
||
58 | |||
59 | /** @var Object[] Collection of SamsonPHP modules */ |
||
60 | protected $modules; |
||
61 | |||
62 | /** |
||
63 | * @return RouteCollection Generated routes collection |
||
64 | */ |
||
65 | public function &routes() |
||
66 | { |
||
67 | return $this->routes; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * GenericRouteGenerator constructor. |
||
72 | * @param Module[] $modules |
||
73 | */ |
||
74 | public function __construct(array & $modules) |
||
75 | { |
||
76 | $this->routes = new RouteCollection(); |
||
77 | $this->modules = &$modules; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Load all SamsonPHP web-application routes. |
||
82 | * |
||
83 | * @return RouteCollection Collection of web-application routes |
||
84 | */ |
||
85 | public function &generate() |
||
86 | { |
||
87 | foreach ($this->modules as &$module) { |
||
88 | // Generate generic routes |
||
89 | $moduleRoutes = $this->createGenericRoutes($module); |
||
90 | |||
91 | // Try to get module routes using interface method |
||
92 | $moduleRoutes = method_exists($module, 'routes') |
||
93 | ? $module->routes($moduleRoutes) : $moduleRoutes; |
||
94 | |||
95 | $this->routes = $this->routes->merge($moduleRoutes); |
||
96 | } |
||
97 | |||
98 | return $this->routes; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Class method signature parameters. |
||
103 | * |
||
104 | * @param mixed $object Object |
||
105 | * @param string $method Method name |
||
106 | * @return \ReflectionParameter[] Method parameters |
||
107 | */ |
||
108 | protected function getMethodParameters($object, $method) |
||
109 | { |
||
110 | // Analyze callback arguments |
||
111 | $reflectionMethod = new \ReflectionMethod($object, $method); |
||
112 | |||
113 | return $reflectionMethod->getParameters(); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Convert class method signature into route pattern with parameters. |
||
118 | * |
||
119 | * @param mixed $object Object |
||
120 | * @param string $method Method name |
||
121 | * @return string Pattern string with parameters placeholders |
||
122 | */ |
||
123 | protected function buildMethodParameters($object, $method) |
||
124 | { |
||
125 | $pattern = array(); |
||
126 | |||
127 | // Analyze callback arguments |
||
128 | foreach ($this->getMethodParameters($object, $method) as $parameter) { |
||
129 | // Build pattern markers |
||
130 | $pattern[] = '{' . $parameter->getName() . '}'; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
131 | //trace($parameter->getDefaultValue(),1); |
||
132 | } |
||
133 | |||
134 | return implode('/', $pattern); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param $module |
||
139 | * @param $prefix |
||
140 | * @param $method |
||
141 | * @param string $action |
||
142 | * @param string $async |
||
143 | * @param string $cache |
||
144 | * @return RouteCollection |
||
145 | * @throws \samsonframework\routing\exception\IdentifierDuplication |
||
146 | */ |
||
147 | protected function getParametrizedRoutes($module, $prefix, $method, $action = '', $async = '', $cache = '') |
||
0 ignored issues
–
show
|
|||
148 | { |
||
149 | $routes = new RouteCollection(); |
||
150 | |||
151 | // Iterate method parameters list to find NOT optional parameters |
||
152 | $parameters = array(); |
||
153 | $optionalParameters = array(); |
||
154 | foreach ($this->getMethodParameters($module, $method) as $parameter) { |
||
155 | if (!$parameter->isOptional()) { |
||
156 | // Append parameter to collection |
||
157 | $parameters[] = '{' . $parameter->getName() . '}'; |
||
0 ignored issues
–
show
![]() |
|||
158 | } else { |
||
159 | $optionalParameters[] = $parameter->getName(); |
||
0 ignored issues
–
show
![]() |
|||
160 | } |
||
161 | } |
||
162 | |||
163 | // Build controller action pattern |
||
164 | $pattern = $prefix . '/'; |
||
165 | // Add controller action if passed |
||
166 | $pattern = isset($action{0}) ? $pattern . $action . '/' : $pattern; |
||
167 | // Add needed parameters |
||
168 | $pattern .= implode('/', $parameters); |
||
169 | |||
170 | $optionalPattern = $pattern.'/'; |
||
171 | |||
172 | // Iterate all optional parameters |
||
173 | foreach ($optionalParameters as $parameter) { |
||
174 | // Add optional parameter as now we consider it needed |
||
175 | $optionalPattern .= '{' . $parameter . '}/'; |
||
176 | |||
177 | // Add SamsonPHP specific async method |
||
178 | View Code Duplication | foreach (array(Route::METHOD_GET, Route::METHOD_POST) as $httpMethod) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
179 | // Add route for this controller action |
||
180 | $routes->add( |
||
181 | new Route( |
||
182 | $optionalPattern, |
||
183 | $module->id.'#'.$method, // Route callback |
||
184 | $module->id . '_' . $httpMethod . '_' . $method.'_'.$parameter, // Route identifier |
||
185 | $async . $httpMethod // Prepend async prefix to method if found |
||
186 | ) |
||
187 | ); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | // Add SamsonPHP without optional parameters |
||
192 | View Code Duplication | foreach (array(Route::METHOD_GET, Route::METHOD_POST) as $httpMethod) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
193 | // Add route for this controller action |
||
194 | $routes->add( |
||
195 | new Route( |
||
196 | $pattern, |
||
197 | $module->id.'#'.$method, // Route callback |
||
198 | $module->id . '_' . $httpMethod . '_' . $method, // Route identifier |
||
199 | $async . $httpMethod // Prepend async prefix to method if found |
||
200 | ) |
||
201 | ); |
||
202 | } |
||
203 | |||
204 | return $routes; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Generate old-fashioned routes collection. |
||
209 | * |
||
210 | * @param Object $module |
||
211 | * @return RouteCollection |
||
212 | */ |
||
213 | protected function createGenericRoutes(&$module) |
||
214 | { |
||
215 | /** @var RouteCollection $routes */ |
||
216 | $routes = new RouteCollection(); |
||
217 | /** @var callable $universalCallback */ |
||
218 | $universalCallback = null; |
||
219 | $universalRoutes = new RouteCollection(); |
||
220 | /** @var Route $baseRoute */ |
||
221 | $baseRoute = null; |
||
222 | |||
223 | $prefix = '/' . $module->id; |
||
224 | |||
225 | Event::fire('samsonphp.router.create.module.routes', array($module, & $prefix)); |
||
226 | |||
227 | //trace('!!!!!!!!!!!!!!!! - '.$prefix); |
||
228 | |||
229 | // Iterate class methods |
||
230 | foreach (get_class_methods($module) as $method) { |
||
231 | // Try to find standard controllers |
||
232 | switch (strtolower($method)) { |
||
233 | case self::CTR_UNI: // Add generic controller action |
||
234 | case self::CTR_CACHE_UNI: |
||
235 | $universalCallback = $module->id.'#'.$method; |
||
236 | $universalRoutes->merge($this->getParametrizedRoutes($module, $prefix, $method)); |
||
237 | break; |
||
238 | View Code Duplication | case self::CTR_BASE: // Add base controller action |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
239 | $baseRoute = new Route($prefix . '/', $module->id.'#'.$method, $module->id . self::CTR_BASE); |
||
240 | break; |
||
241 | View Code Duplication | case self::CTR_CACHE_BASE: |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
242 | $baseRoute = new Route($prefix . '/', $module->id.'#'.$method, $module->id . self::CTR_CACHE_BASE); |
||
243 | break; |
||
244 | case self::CTR_POST:// not implemented |
||
245 | case self::CTR_PUT:// not implemented |
||
246 | case self::CTR_DELETE:// not implemented |
||
247 | break; |
||
248 | |||
249 | // Ignore magic methods |
||
250 | case '__call': |
||
251 | case '__wakeup': |
||
252 | case '__sleep': |
||
253 | case '__construct': |
||
254 | case '__destruct': |
||
255 | case '__set': |
||
256 | case '__get': |
||
257 | break; |
||
258 | |||
259 | // This is not special controller action |
||
260 | default: |
||
261 | // Match controller action OOP pattern |
||
262 | if (preg_match('/^' . self::OBJ_PREFIX . '(?<async_>async_)?(?<cache_>cache_)?(?<action>.+)/i', $method, $matches)) { |
||
263 | $routes->merge( |
||
264 | $this->getParametrizedRoutes( |
||
265 | $module, |
||
266 | $prefix, |
||
267 | $method, |
||
268 | $matches['action'], |
||
269 | $matches[self::ASYNC_PREFIX], |
||
270 | $matches['cache_'] |
||
271 | ) |
||
272 | ); |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 | |||
277 | // Add universal route |
||
278 | $routes->merge($universalRoutes); |
||
279 | |||
280 | // Add base controller action |
||
281 | if (isset($baseRoute)) { |
||
282 | $routes->add($baseRoute); |
||
283 | // If we have not found base controller action but we have universal action |
||
284 | } elseif (isset($universalCallback)) { |
||
285 | // Bind its pattern to universal controller callback |
||
286 | $routes->add(new Route($prefix . '/', $universalCallback, $module->id . self::CTR_BASE)); |
||
287 | } |
||
288 | |||
289 | return $routes; |
||
290 | } |
||
291 | //[PHPCOMPRESSOR(remove,end)] |
||
292 | } |
||
293 |