PluginProbe
WPIDE – File Manager & Code Editor / 3.5.10
WPIDE – File Manager & Code Editor v3.5.10
3.5.10 3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 All 56 releases
wpide / vendor / nikic / fast-route / src / RouteParser / Std.php

Std.php in WPIDE – File Manager & Code Editor 3.5.10, at vendor/nikic/fast-route/src/RouteParser/Std.php

88 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FastRoute\RouteParser;
4
5 use FastRoute\BadRouteException;
6 use FastRoute\RouteParser;
7
8 /**
9 * Parses route strings of the following form:
10 *
11 * "/user/{name}[/{id:[0-9]+}]"
12 */
13 class Std implements RouteParser
14 {
15 const VARIABLE_REGEX = <<<'REGEX'
16 \{
17 \s* ([a-zA-Z_][a-zA-Z0-9_-]*) \s*
18 (?:
19 : \s* ([^{}]*(?:\{(?-1)\}[^{}]*)*)
20 )?
21 \}
22 REGEX;
23 const DEFAULT_DISPATCH_REGEX = '[^/]+';
24
25 public function parse($route)
26 {
27 $routeWithoutClosingOptionals = rtrim($route, ']');
28 $numOptionals = strlen($route) - strlen($routeWithoutClosingOptionals);
29
30 // Split on [ while skipping placeholders
31 $segments = preg_split('~' . self::VARIABLE_REGEX . '(*SKIP)(*F) | \[~x', $routeWithoutClosingOptionals);
32 if ($numOptionals !== count($segments) - 1) {
33 // If there are any ] in the middle of the route, throw a more specific error message
34 if (preg_match('~' . self::VARIABLE_REGEX . '(*SKIP)(*F) | \]~x', $routeWithoutClosingOptionals)) {
35 throw new BadRouteException('Optional segments can only occur at the end of a route');
36 }
37 throw new BadRouteException("Number of opening '[' and closing ']' does not match");
38 }
39
40 $currentRoute = '';
41 $routeDatas = [];
42 foreach ($segments as $n => $segment) {
43 if ($segment === '' && $n !== 0) {
44 throw new BadRouteException('Empty optional part');
45 }
46
47 $currentRoute .= $segment;
48 $routeDatas[] = $this->parsePlaceholders($currentRoute);
49 }
50 return $routeDatas;
51 }
52
53 /**
54 * Parses a route string that does not contain optional segments.
55 *
56 * @param string
57 * @return mixed[]
58 */
59 private function parsePlaceholders($route)
60 {
61 if (!preg_match_all(
62 '~' . self::VARIABLE_REGEX . '~x', $route, $matches,
63 PREG_OFFSET_CAPTURE | PREG_SET_ORDER
64 )) {
65 return [$route];
66 }
67
68 $offset = 0;
69 $routeData = [];
70 foreach ($matches as $set) {
71 if ($set[0][1] > $offset) {
72 $routeData[] = substr($route, $offset, $set[0][1] - $offset);
73 }
74 $routeData[] = [
75 $set[1][0],
76 isset($set[2]) ? trim($set[2][0]) : self::DEFAULT_DISPATCH_REGEX
77 ];
78 $offset = $set[0][1] + strlen($set[0][0]);
79 }
80
81 if ($offset !== strlen($route)) {
82 $routeData[] = substr($route, $offset);
83 }
84
85 return $routeData;
86 }
87 }
88