C:\xampp\htdocs\kptv2\admin2\vendor\phroute\phroute\src\Phroute\RouteParser.php


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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php namespace Phroute\Phroute;

use 
Phroute\Phroute\Exception\BadRouteException;
/**
 * Parses routes of the following form:
 *
 * "/user/{name}/{id:[0-9]+}?"
 */
class RouteParser {

    
/**
     * Search through the given route looking for dynamic portions.
     *
     * Using ~ as the regex delimiter.
     *
     * We start by looking for a literal '{' character followed by any amount of whitespace.
     * The next portion inside the parentheses looks for a parameter name containing alphanumeric characters or underscore.
     *
     * After this we look for the ':\d+' and ':[0-9]+' style portion ending with a closing '}' character.
     *
     * Finally we look for an optional '?' which is used to signify an optional route.
     */
    
const VARIABLE_REGEX 
"~\{
    \s* ([a-zA-Z0-9_]*) \s*
    (?:
        : \s* ([^{]+(?:\{.*?\})?)
    )?
\}\??~x"
;

    
/**
     * The default parameter character restriction (One or more characters that is not a '/').
     */
    
const DEFAULT_DISPATCH_REGEX '[^/]+';

    private 
$parts;

    private 
$reverseParts;
    
    private 
$partsCounter;
    
    private 
$variables;
    
    private 
$regexOffset;

    
/**
     * Handy parameter type restrictions.
     *
     * @var array
     */
    
private $regexShortcuts = array(
        
':i}'  => ':[0-9]+}',
    
':a}'  => ':[0-9A-Za-z]+}',
    
':h}'  => ':[0-9A-Fa-f]+}',
        
':c}'  => ':[a-zA-Z0-9+_\-\.]+}'
    
);

    
/**
     * Parse a route returning the correct data format to pass to the dispatch engine.
     *
     * @param $route
     * @return array
     */
    
public function parse($route)
    {
        
$this->reset();
        
        
$route strtr($route$this->regexShortcuts);
        
        if (!
$matches $this->extractVariableRouteParts($route))
        {
            
$reverse = array(
                
'variable'  => false,
                
'value'     => $route
            
);

            return array(array(
$route), array($reverse));
        }

        foreach (
$matches as $set) {

            
$this->staticParts($route$set[0][1]);
                        
            
$this->validateVariable($set[1][0]);

            
$regexPart = (isset($set[2]) ? trim($set[2][0]) : self::DEFAULT_DISPATCH_REGEX);
            
            
$this->regexOffset $set[0][1] + strlen($set[0][0]);

            
$match '(' $regexPart ')';

            
$isOptional substr($set[0][0], -1) === '?';
            
            if(
$isOptional)
            {
                
$match $this->makeOptional($match);
            }

            
$this->reverseParts[$this->partsCounter] = array(
                
'variable'  => true,
                
'optional'  => $isOptional,
                
'name'      => $set[1][0]
            );

            
$this->parts[$this->partsCounter++] = $match;
        }

        
$this->staticParts($routestrlen($route));

        return array(array(
implode(''$this->parts), $this->variables), array_values($this->reverseParts));
    }

    
/**
     * Reset the parser ready for the next route.
     */
    
private function reset()
    {
        
$this->parts = array();
        
        
$this->reverseParts = array();
    
        
$this->partsCounter 0;

        
$this->variables = array();

        
$this->regexOffset 0;
    }

    
/**
     * Return any variable route portions from the given route.
     *
     * @param $route
     * @return mixed
     */
    
private function extractVariableRouteParts($route)
    {
        if(
preg_match_all(self::VARIABLE_REGEX$route$matchesPREG_OFFSET_CAPTURE PREG_SET_ORDER))
        {
            return 
$matches;
        }
    }

    
/**
     * @param $route
     * @param $nextOffset
     */
    
private function staticParts($route$nextOffset)
    {
        
$static preg_split('~(/)~u'substr($route$this->regexOffset$nextOffset $this->regexOffset), 0PREG_SPLIT_DELIM_CAPTURE);

        foreach(
$static as $staticPart)
        {
            if(
$staticPart)
            {
                
$quotedPart $this->quote($staticPart);

                
$this->parts[$this->partsCounter] = $quotedPart;

                
$this->reverseParts[$this->partsCounter] = array(
                    
'variable'  => false,
                    
'value'     => $staticPart
                
);
                
                
$this->partsCounter++;
            }
        }
    }

    
/**
     * @param $varName
     */
    
private function validateVariable($varName)
    {
        if (isset(
$this->variables[$varName]))
        {
            throw new 
BadRouteException("Cannot use the same placeholder '$varName' twice");
        }

        
$this->variables[$varName] = $varName;
    }

    
/**
     * @param $match
     * @return string
     */
    
private function makeOptional($match)
    {
        
$previous $this->partsCounter 1;
        
        if(isset(
$this->parts[$previous]) && $this->parts[$previous] === '/')
        {
            
$this->partsCounter--;
            
$match '(?:/' $match ')';
        }

        return 
$match '?';
    }

    
/**
     * @param $part
     * @return string
     */
    
private function quote($part)
    {
        return 
preg_quote($part'~');
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586