C:\xampp\htdocs\kptv2\admin2\vendor\phroute\phroute\src\Phroute\Dispatcher.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
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php namespace Phroute\Phroute;

use 
Phroute\Phroute\Exception\HttpMethodNotAllowedException;
use 
Phroute\Phroute\Exception\HttpRouteNotFoundException;

class 
Dispatcher {

    private 
$staticRouteMap;
    private 
$variableRouteData;
    private 
$filters;
    private 
$handlerResolver;
    public 
$matchedRoute;

    
/**
     * Create a new route dispatcher.
     *
     * @param RouteDataInterface $data
     * @param HandlerResolverInterface $resolver
     */
    
public function __construct(RouteDataInterface $dataHandlerResolverInterface $resolver null)
    {
        
$this->staticRouteMap $data->getStaticRoutes();

        
$this->variableRouteData $data->getVariableRoutes();
        
        
$this->filters $data->getFilters();
        
        if (
$resolver === null)
        {
            
$this->handlerResolver = new HandlerResolver();
        }
        else
        {
            
$this->handlerResolver $resolver;
        }
    }

    
/**
     * Dispatch a route for the given HTTP Method / URI.
     *
     * @param $httpMethod
     * @param $uri
     * @return mixed|null
     */
    
public function dispatch($httpMethod$uri)
    {
        list(
$handler$filters$vars) = $this->dispatchRoute($httpMethodtrim($uri'/'));

        list(
$beforeFilter$afterFilter) = $this->parseFilters($filters);

        if((
$response $this->dispatchFilters($beforeFilter)) !== null)
        {
            return 
$response;
        }
        
        
$resolvedHandler $this->handlerResolver->resolve($handler);
        
        
$response call_user_func_array($resolvedHandler$vars);

        return 
$this->dispatchFilters($afterFilter$response);
    }

    
/**
     * Dispatch a route filter.
     *
     * @param $filters
     * @param null $response
     * @return mixed|null
     */
    
private function dispatchFilters($filters$response null)
    {
        while(
$filter array_shift($filters))
        {
            
$handler $this->handlerResolver->resolve($filter);
            
            if((
$filteredResponse call_user_func($handler$response)) !== null)
            {
                return 
$filteredResponse;
            }
        }
        
        return 
$response;
    }

    
/**
     * Normalise the array filters attached to the route and merge with any global filters.
     *
     * @param $filters
     * @return array
     */
    
private function parseFilters($filters)
    {        
        
$beforeFilter = array();
        
$afterFilter = array();
        
        if(isset(
$filters[Route::BEFORE]))
        {
            
$beforeFilter array_intersect_key($this->filtersarray_flip((array) $filters[Route::BEFORE]));
        }

        if(isset(
$filters[Route::AFTER]))
        {
            
$afterFilter array_intersect_key($this->filtersarray_flip((array) $filters[Route::AFTER]));
        }
        
        return array(
$beforeFilter$afterFilter);
    }

    
/**
     * Perform the route dispatching. Check static routes first followed by variable routes.
     *
     * @param $httpMethod
     * @param $uri
     * @throws Exception\HttpRouteNotFoundException
     */
    
private function dispatchRoute($httpMethod$uri)
    {
        if (isset(
$this->staticRouteMap[$uri]))
        {
            return 
$this->dispatchStaticRoute($httpMethod$uri);
        }
        
        return 
$this->dispatchVariableRoute($httpMethod$uri);
    }

    
/**
     * Handle the dispatching of static routes.
     *
     * @param $httpMethod
     * @param $uri
     * @return mixed
     * @throws Exception\HttpMethodNotAllowedException
     */
    
private function dispatchStaticRoute($httpMethod$uri)
    {
        
$routes $this->staticRouteMap[$uri];

        if (!isset(
$routes[$httpMethod]))
        {
            
$httpMethod $this->checkFallbacks($routes$httpMethod);
        }
        
        return 
$routes[$httpMethod];
    }

    
/**
     * Check fallback routes: HEAD for GET requests followed by the ANY attachment.
     *
     * @param $routes
     * @param $httpMethod
     * @throws Exception\HttpMethodNotAllowedException
     */
    
private function checkFallbacks($routes$httpMethod)
    {
        
$additional = array(Route::ANY);
        
        if(
$httpMethod === Route::HEAD)
        {
            
$additional[] = Route::GET;
        }
        
        foreach(
$additional as $method)
        {
            if(isset(
$routes[$method]))
            {
                return 
$method;
            }
        }
        
        
$this->matchedRoute $routes;
        
        throw new 
HttpMethodNotAllowedException('Allow: ' implode(', 'array_keys($routes)));
    }

    
/**
     * Handle the dispatching of variable routes.
     *
     * @param $httpMethod
     * @param $uri
     * @throws Exception\HttpMethodNotAllowedException
     * @throws Exception\HttpRouteNotFoundException
     */
    
private function dispatchVariableRoute($httpMethod$uri)
    {        
        foreach (
$this->variableRouteData as $data
        {            
            if (!
preg_match($data['regex'], $uri$matches))
            {
                continue;
            }

            
$count count($matches);

            while(!isset(
$data['routeMap'][$count++]));
            
            
$routes $data['routeMap'][$count 1];

            if (!isset(
$routes[$httpMethod]))
            {
                
$httpMethod $this->checkFallbacks($routes$httpMethod);
            } 

            foreach (
array_values($routes[$httpMethod][2]) as $i => $varName)
            {
                if(!isset(
$matches[$i 1]) || $matches[$i 1] === '')
                {
                    unset(
$routes[$httpMethod][2][$varName]);
                }
                else
                {
                    
$routes[$httpMethod][2][$varName] = $matches[$i 1];
                }
            }

            return 
$routes[$httpMethod];
        }

        throw new 
HttpRouteNotFoundException('Route ' $uri ' does not exist');
    }
}
x

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