Test Setup Failed
Push — master ( 0415ed...38e597 )
by Php Easy Api
03:46
created

Route::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 18
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Router;
4
5
use Resta\Support\Str;
6
use Resta\Support\Utils;
7
use Resta\Foundation\PathManager\StaticPathList;
8
9
class Route extends RouteHttpManager
10
{
11
    // get route acccessible property
12
    use RouteAccessiblePropertyTrait;
13
14
    /**
15
     * @var array $endpoints
16
     */
17
    protected static $endpoints = [];
18
19
    /**
20
     * @var array $routes
21
     */
22
    protected static $routes = [];
23
24
    /**
25
     * @var array $paths
26
     */
27
    protected static $paths = [];
28
29
    /**
30
     * @var array $mappers
31
     */
32
    protected static $mappers = [];
33
34
    /**
35
     * @var null|string $namespace
36
     */
37
    protected static $namespace;
38
39
    /**
40
     * get route checkArrayEqual
41
     *
42
     * @param $patterns
43
     * @param $urlRoute
44
     * @return int|string
45
     */
46
    public static function checkArrayEqual($patterns,$urlRoute)
47
    {
48
        // calculates the equality difference between
49
        // the route pattern and the urlRoute value.
50
        foreach ($patterns as $key=>$pattern){
51
52
            if(Utils::isArrayEqual($pattern,$urlRoute)){
53
                return $key;
54
            }
55
        }
56
57
        // if the difference is not equal,
58
        // null is returned.
59
        return null;
60
    }
61
62
    /**
63
     * get route controller class
64
     *
65
     * @return string
66
     */
67
    public static function getRouteControllerClass()
68
    {
69
        $route = self::getRouteResolve();
70
71
        if(isset($route['class'])){
72
            return $route['class'];
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * get route controller class
80
     *
81
     * @return string
82
     */
83
    public static function getRouteControllerMethod()
84
    {
85
        $route = self::getRouteResolve();
86
87
        if(isset($route['method'])){
88
            return $route['method'];
89
        }
90
91
        return null;
92
    }
93
94
    /**
95
     * get route controller namespace
96
     *
97
     * @return string
98
     */
99
    public static function getRouteControllerNamespace()
100
    {
101
        $route = self::getRouteResolve();
102
103
        if(isset($route['controller'],$route['namespace'])){
104
            return $route['controller'].'/'.$route['namespace'];
105
        }
106
107
        return null;
108
    }
109
110
    /**
111
     * get route getRouteResolve
112
     *
113
     * @return array
114
     */
115
    public static function getRouteResolve()
116
    {
117
        // get routes data and the resolving pattern
118
        // Both are interrelated.
119
        $routes = self::getRoutes();
120
        $patternResolve = app()->resolve(RouteMatching::class,['route'=>new self()])->getPatternResolve();
121
122
        // we set the route variables for the route assistant.
123
        self::updateRouteParameters($patternResolve);
124
125
        //if routes data is available in pattern resolve.
126
        if(isset($routes['data'][$patternResolve])){
127
128
            // if the incoming http value is
129
            // the same as the real request method, the data is processed.
130
            if($routes['data'][$patternResolve]['http'] == strtolower(httpMethod)){
131
132
                // we are set the solved pattern to a variable.
133
                $resolve = $routes['data'][$patternResolve];
134
135
                return [
136
                    'class'         => $resolve['class'],
137
                    'method'        => $resolve['method'],
138
                    'controller'    => $resolve['controller'],
139
                    'namespace'     => $resolve['namespace'],
140
                ];
141
            }
142
        }
143
144
        return [];
145
    }
146
147
    /**
148
     * update route parameters
149
     *
150
     * @param $patternResolvedKey
151
     * @return mixed|void
152
     */
153
    private static function updateRouteParameters($patternResolvedKey)
154
    {
155
        $list = [];
156
157
        if(isset(static::$routes['pattern'][$patternResolvedKey])){
158
159
            $routeParameters = static::$routes['pattern'][$patternResolvedKey];
160
            $route = route();
161
162
            foreach($routeParameters as $key=>$param){
163
164
                $param = Str::replaceWordArray(['{','}','?'],'',$param);
165
166
                if(isset($route[$key])){
167
                    $list[$param] = $route[$key];
168
                }
169
            }
170
        }
171
172
        app()->register('routeParams',$list);
173
    }
174
175
    /**
176
     * route handle for route application
177
     *
178
     * @return void|mixed
179
     */
180
    public function handle()
181
    {
182
        // we will record the path data for the route.
183
        // We set the routeMapper variables and the route path.
184
        self::setPath(function(){
185
186
            // we are sending
187
            // the controller and routes.php path.
188
            return [
189
                'controllerPath'    => path()->controller(),
190
                'routePath'         => path()->route(),
191
            ];
192
        });
193
194
        // in the paths data,
195
        // we run the route mapper values ​​and the route files one by one.
196
        foreach (self::$paths as $mapper=>$controller){
197
            core()->fileSystem->callFile($mapper);
198
        }
199
    }
200
201
    /**
202
     * get route setPath method
203
     *
204
     * @param $path
205
     */
206
    public static function setPath(callable $callback)
207
    {
208
        $routeDefinitor = call_user_func($callback);
209
210
        if(isset($routeDefinitor['controllerPath']) && isset($routeDefinitor['routePath'])){
211
212
            //the route paths to be saved to the mappers static property.
213
            static::$mappers['routePaths'][] = $routeDefinitor['routePath'];
214
            static::$mappers['controllerNamespaces'][] = Utils::getNamespace($routeDefinitor['controllerPath']);
215
216
            // if there is endpoint,
217
            // then only that endpoint is transferred into the path
218
            if(defined('endpoint')){
219
220
                $routeName      = endpoint.'Route.php';
221
                $routeMapper    = $routeDefinitor['routePath'].''.DIRECTORY_SEPARATOR.''.$routeName;
222
223
                if(file_exists($routeMapper) && !isset(static::$paths[$routeMapper])){
224
                    static::$paths[$routeMapper] = $routeDefinitor['controllerPath'];
225
                }
226
            }
227
            else{
228
229
                // if there is no endpoint,
230
                // all files in the path of the route are transferred to path.
231
                $allFilesInThatRoutePath = Utils::glob($routeDefinitor['routePath']);
232
233
                foreach ($allFilesInThatRoutePath as $item){
234
                    static::$paths[$item] = $routeDefinitor['controllerPath'];
235
                }
236
            }
237
        }
238
    }
239
240
    /**
241
     * get route setRoute method
242
     *
243
     * @param $params
244
     * @param $function
245
     * @param null $controller
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $controller is correct as it would always require null to be passed?
Loading history...
246
     */
247
    public static function setRoute($params,$function,$controller=null)
248
    {
249
        [$pattern,$route]   = $params;
250
        [$class,$method]    = explode("@",$route);
251
252
        $patternList = array_values(
253
            array_filter(explode("/",$pattern),'strlen')
254
        );
255
256
        static::$routes['pattern'][] = $patternList;
257
        static::$routes['data'][] = [
258
            'method'        => $method,
259
            'class'         => $class,
260
            'http'          => $function,
261
            'controller'    => $controller,
262
            'namespace'     => static::$namespace,
263
            'endpoint'      => strtolower(str_replace(StaticPathList::$controllerBundleName,'',static::$namespace))
264
        ];
265
    }
266
267
    /**
268
     * is route variable regex pattern
269
     *
270
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
271
     * @return bool
272
     */
273
    public static function isMatchVaribleRegexPattern($value=null)
274
    {
275
        // determines if the variable that can be used
276
        // in the route file meets the regex rule.
277
        return (preg_match('@\{(.*?)\}@is',$value)) ? true : false;
278
    }
279
280
281
    /**
282
     * is optional route variable regex pattern
283
     *
284
     * @param null $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
285
     * @return bool
286
     */
287
    public static function isOptionalVaribleRegexPattern($value=null)
288
    {
289
        // determines if the variable that can be used
290
        // in the route file meets the regex rule.
291
        return preg_match('@\{[a-z]+\?\}@is',$value) ? true : false;
292
    }
293
294
295
}