comparison vendor/symfony/routing/Matcher/Dumper/PhpMatcherDumper.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\Routing\Matcher\Dumper; 12 namespace Symfony\Component\Routing\Matcher\Dumper;
13 13
14 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
15 use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
14 use Symfony\Component\Routing\Route; 16 use Symfony\Component\Routing\Route;
15 use Symfony\Component\Routing\RouteCollection; 17 use Symfony\Component\Routing\RouteCollection;
16 use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
17 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
18 18
19 /** 19 /**
20 * PhpMatcherDumper creates a PHP class able to match URLs for a given set of routes. 20 * PhpMatcherDumper creates a PHP class able to match URLs for a given set of routes.
21 * 21 *
22 * @author Fabien Potencier <fabien@symfony.com> 22 * @author Fabien Potencier <fabien@symfony.com>
28 private $expressionLanguage; 28 private $expressionLanguage;
29 29
30 /** 30 /**
31 * @var ExpressionFunctionProviderInterface[] 31 * @var ExpressionFunctionProviderInterface[]
32 */ 32 */
33 private $expressionLanguageProviders = array(); 33 private $expressionLanguageProviders = [];
34 34
35 /** 35 /**
36 * Dumps a set of routes to a PHP class. 36 * Dumps a set of routes to a PHP class.
37 * 37 *
38 * Available options: 38 * Available options:
42 * 42 *
43 * @param array $options An array of options 43 * @param array $options An array of options
44 * 44 *
45 * @return string A PHP class representing the matcher class 45 * @return string A PHP class representing the matcher class
46 */ 46 */
47 public function dump(array $options = array()) 47 public function dump(array $options = [])
48 { 48 {
49 $options = array_replace(array( 49 $options = array_replace([
50 'class' => 'ProjectUrlMatcher', 50 'class' => 'ProjectUrlMatcher',
51 'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher', 51 'base_class' => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
52 ), $options); 52 ], $options);
53 53
54 // trailing slash support is only enabled if we know how to redirect the user 54 // trailing slash support is only enabled if we know how to redirect the user
55 $interfaces = class_implements($options['base_class']); 55 $interfaces = class_implements($options['base_class']);
56 $supportsRedirections = isset($interfaces['Symfony\\Component\\Routing\\Matcher\\RedirectableUrlMatcherInterface']); 56 $supportsRedirections = isset($interfaces['Symfony\\Component\\Routing\\Matcher\\RedirectableUrlMatcherInterface']);
57 57
96 $code = rtrim($this->compileRoutes($this->getRoutes(), $supportsRedirections), "\n"); 96 $code = rtrim($this->compileRoutes($this->getRoutes(), $supportsRedirections), "\n");
97 97
98 return <<<EOF 98 return <<<EOF
99 public function match(\$rawPathinfo) 99 public function match(\$rawPathinfo)
100 { 100 {
101 \$allow = array(); 101 \$allow = [];
102 \$pathinfo = rawurldecode(\$rawPathinfo); 102 \$pathinfo = rawurldecode(\$rawPathinfo);
103 \$trimmedPathinfo = rtrim(\$pathinfo, '/'); 103 \$trimmedPathinfo = rtrim(\$pathinfo, '/');
104 \$context = \$this->context; 104 \$context = \$this->context;
105 \$request = \$this->request ?: \$this->createRequest(\$pathinfo); 105 \$request = \$this->request ?: \$this->createRequest(\$pathinfo);
106 \$requestMethod = \$canonicalMethod = \$context->getMethod(); 106 \$requestMethod = \$canonicalMethod = \$context->getMethod();
228 */ 228 */
229 private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null) 229 private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null)
230 { 230 {
231 $code = ''; 231 $code = '';
232 $compiledRoute = $route->compile(); 232 $compiledRoute = $route->compile();
233 $conditions = array(); 233 $conditions = [];
234 $hasTrailingSlash = false; 234 $hasTrailingSlash = false;
235 $matches = false; 235 $matches = false;
236 $hostMatches = false; 236 $hostMatches = false;
237 $methods = $route->getMethods(); 237 $methods = $route->getMethods();
238 238
239 $supportsTrailingSlash = $supportsRedirections && (!$methods || in_array('GET', $methods)); 239 $supportsTrailingSlash = $supportsRedirections && (!$methods || \in_array('GET', $methods));
240 $regex = $compiledRoute->getRegex(); 240 $regex = $compiledRoute->getRegex();
241 241
242 if (!count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#'.('u' === substr($regex, -1) ? 'u' : ''), $regex, $m)) { 242 if (!\count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#'.('u' === substr($regex, -1) ? 'u' : ''), $regex, $m)) {
243 if ($supportsTrailingSlash && '/' === substr($m['url'], -1)) { 243 if ($supportsTrailingSlash && '/' === substr($m['url'], -1)) {
244 $conditions[] = sprintf('%s === $trimmedPathinfo', var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true)); 244 $conditions[] = sprintf('%s === $trimmedPathinfo', var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true));
245 $hasTrailingSlash = true; 245 $hasTrailingSlash = true;
246 } else { 246 } else {
247 $conditions[] = sprintf('%s === $pathinfo', var_export(str_replace('\\', '', $m['url']), true)); 247 $conditions[] = sprintf('%s === $pathinfo', var_export(str_replace('\\', '', $m['url']), true));
263 if ($compiledRoute->getHostVariables()) { 263 if ($compiledRoute->getHostVariables()) {
264 $hostMatches = true; 264 $hostMatches = true;
265 } 265 }
266 266
267 if ($route->getCondition()) { 267 if ($route->getCondition()) {
268 $conditions[] = $this->getExpressionLanguage()->compile($route->getCondition(), array('context', 'request')); 268 $conditions[] = $this->getExpressionLanguage()->compile($route->getCondition(), ['context', 'request']);
269 } 269 }
270 270
271 $conditions = implode(' && ', $conditions); 271 $conditions = implode(' && ', $conditions);
272 272
273 $code .= <<<EOF 273 $code .= <<<EOF
277 EOF; 277 EOF;
278 278
279 $gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name); 279 $gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
280 280
281 // the offset where the return value is appended below, with indendation 281 // the offset where the return value is appended below, with indendation
282 $retOffset = 12 + strlen($code); 282 $retOffset = 12 + \strlen($code);
283 283
284 // optimize parameters array 284 // optimize parameters array
285 if ($matches || $hostMatches) { 285 if ($matches || $hostMatches) {
286 $vars = array(); 286 $vars = [];
287 if ($hostMatches) { 287 if ($hostMatches) {
288 $vars[] = '$hostMatches'; 288 $vars[] = '$hostMatches';
289 } 289 }
290 if ($matches) { 290 if ($matches) {
291 $vars[] = '$matches'; 291 $vars[] = '$matches';
292 } 292 }
293 $vars[] = "array('_route' => '$name')"; 293 $vars[] = "['_route' => '$name']";
294 294
295 $code .= sprintf( 295 $code .= sprintf(
296 " \$ret = \$this->mergeDefaults(array_replace(%s), %s);\n", 296 " \$ret = \$this->mergeDefaults(array_replace(%s), %s);\n",
297 implode(', ', $vars), 297 implode(', ', $vars),
298 str_replace("\n", '', var_export($route->getDefaults(), true)) 298 str_replace("\n", '', var_export($route->getDefaults(), true))
299 ); 299 );
300 } elseif ($route->getDefaults()) { 300 } elseif ($route->getDefaults()) {
301 $code .= sprintf(" \$ret = %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true))); 301 $code .= sprintf(" \$ret = %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), ['_route' => $name]), true)));
302 } else { 302 } else {
303 $code .= sprintf(" \$ret = array('_route' => '%s');\n", $name); 303 $code .= sprintf(" \$ret = ['_route' => '%s'];\n", $name);
304 } 304 }
305 305
306 if ($hasTrailingSlash) { 306 if ($hasTrailingSlash) {
307 $code .= <<<EOF 307 $code .= <<<EOF
308 if ('/' === substr(\$pathinfo, -1)) { 308 if ('/' === substr(\$pathinfo, -1)) {
316 316
317 EOF; 317 EOF;
318 } 318 }
319 319
320 if ($methods) { 320 if ($methods) {
321 $methodVariable = in_array('GET', $methods) ? '$canonicalMethod' : '$requestMethod'; 321 $methodVariable = \in_array('GET', $methods) ? '$canonicalMethod' : '$requestMethod';
322 $methods = implode("', '", $methods); 322 $methods = implode("', '", $methods);
323 } 323 }
324 324
325 if ($schemes = $route->getSchemes()) { 325 if ($schemes = $route->getSchemes()) {
326 if (!$supportsRedirections) { 326 if (!$supportsRedirections) {
329 $schemes = str_replace("\n", '', var_export(array_flip($schemes), true)); 329 $schemes = str_replace("\n", '', var_export(array_flip($schemes), true));
330 if ($methods) { 330 if ($methods) {
331 $code .= <<<EOF 331 $code .= <<<EOF
332 \$requiredSchemes = $schemes; 332 \$requiredSchemes = $schemes;
333 \$hasRequiredScheme = isset(\$requiredSchemes[\$context->getScheme()]); 333 \$hasRequiredScheme = isset(\$requiredSchemes[\$context->getScheme()]);
334 if (!in_array($methodVariable, array('$methods'))) { 334 if (!in_array($methodVariable, ['$methods'])) {
335 if (\$hasRequiredScheme) { 335 if (\$hasRequiredScheme) {
336 \$allow = array_merge(\$allow, array('$methods')); 336 \$allow = array_merge(\$allow, ['$methods']);
337 } 337 }
338 goto $gotoname; 338 goto $gotoname;
339 } 339 }
340 if (!\$hasRequiredScheme) { 340 if (!\$hasRequiredScheme) {
341 if ('GET' !== \$canonicalMethod) { 341 if ('GET' !== \$canonicalMethod) {
361 361
362 EOF; 362 EOF;
363 } 363 }
364 } elseif ($methods) { 364 } elseif ($methods) {
365 $code .= <<<EOF 365 $code .= <<<EOF
366 if (!in_array($methodVariable, array('$methods'))) { 366 if (!in_array($methodVariable, ['$methods'])) {
367 \$allow = array_merge(\$allow, array('$methods')); 367 \$allow = array_merge(\$allow, ['$methods']);
368 goto $gotoname; 368 goto $gotoname;
369 } 369 }
370 370
371 371
372 EOF; 372 EOF;