Test Setup Failed
Push — master ( ce8a6d...dcec01 )
by Php Easy Api
04:52 queued 10s
created

Route::getRouteResolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 0
dl 0
loc 30
rs 9.8666
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 namespace
80
     *
81
     * @return string
82
     */
83
    public static function getRouteControllerNamespace()
84
    {
85
        $route = self::getRouteResolve();
86
87
        if(isset($route['controller'],$route['namespace'])){
88
            return $route['controller'].'/'.$route['namespace'];
89
        }
90
91
        return null;
92
    }
93
94
    /**
95
     * get route getRouteResolve
96
     *
97
     * @return array
98
     */
99
    public static function getRouteResolve()
100
    {
101
        // get routes data and the resolving pattern
102
        // Both are interrelated.
103
        $routes = self::getRoutes();
104
        $patternResolve = app()->resolve(RouteMatching::class,['route'=>new self()])->getPatternResolve();
105
106
        // we set the route variables for the route assistant.
107
        self::updateRouteParameters($patternResolve);
108
109
        //if routes data is available in pattern resolve.
110
        if(isset($routes['data'][$patternResolve])){
111
112
            // if the incoming http value is
113
            // the same as the real request method, the data is processed.
114
            if($routes['data'][$patternResolve]['http'] == strtolower(httpMethod)){
115
116
                // we are set the solved pattern to a variable.
117
                $resolve = $routes['data'][$patternResolve];
118
119
                return [
120
                    'class'         => $resolve['class'],
121
                    'method'        => $resolve['method'],
122
                    'controller'    => $resolve['controller'],
123
                    'namespace'     => $resolve['namespace'],
124
                ];
125
            }
126
        }
127
128
        return [];
129
    }
130
131
    /**
132
     * update route parameters
133
     *
134
     * @param $patternResolvedKey
135
     * @return mixed|void
136
     */
137
    private static function updateRouteParameters($patternResolvedKey)
138
    {
139
        $list = [];
140
141
        if(isset(static::$routes['pattern'][$patternResolvedKey])){
142
143
            $routeParameters = static::$routes['pattern'][$patternResolvedKey];
144
            $route = route();
145
146
            foreach($routeParameters as $key=>$param){
147
148
                $param = Str::replaceWordArray(['{','}','?'],'',$param);
149
150
                if(isset($route[$key])){
151
                    $list[$param] = $route[$key];
152
                }
153
            }
154
        }
155
156
        app()->register('routeParams',$list);
157
    }
158
159
    /**
160
     * route handle for route application
161
     *
162
     * @return void|mixed
163
     */
164
    public function handle()
165
    {
166
        // we will record the path data for the route.
167
        // We set the routeMapper variables and the route path.
168
        self::setPath(function(){
169
170
            // we are sending
171
            // the controller and routes.php path.
172
            return [
173
                'controllerPath'    => path()->controller(),
174
                'routePath'         => path()->route(),
175
            ];
176
        });
177
178
        // in the paths data,
179
        // we run the route mapper values ​​and the route files one by one.
180
        foreach (self::$paths as $mapper=>$controller){
181
            core()->fileSystem->callFile($mapper);
182
        }
183
    }
184
185
    /**
186
     * get route setPath method
187
     *
188
     * @param $path
189
     */
190
    public static function setPath(callable $callback)
191
    {
192
        $routeDefinitor = call_user_func($callback);
193
194
        if(isset($routeDefinitor['controllerPath']) && isset($routeDefinitor['routePath'])){
195
196
            //the route paths to be saved to the mappers static property.
197
            static::$mappers['routePaths'][] = $routeDefinitor['routePath'];
198
            static::$mappers['controllerNamespaces'][] = Utils::getNamespace($routeDefinitor['controllerPath']);
199
200
            // if there is endpoint,
201
            // then only that endpoint is transferred into the path
202
            if(defined('endpoint')){
203
204
                $routeName      = endpoint.'Route.php';
205
                $routeMapper    = $routeDefinitor['routePath'].''.DIRECTORY_SEPARATOR.''.$routeName;
206
207
                if(file_exists($routeMapper) && !isset(static::$paths[$routeMapper])){
208
                    static::$paths[$routeMapper] = $routeDefinitor['controllerPath'];
209
                }
210
            }
211
            else{
212
213
                // if there is no endpoint,
214
                // all files in the path of the route are transferred to path.
215
                $allFilesInThatRoutePath = Utils::glob($routeDefinitor['routePath']);
216
217
                foreach ($allFilesInThatRoutePath as $item){
218
                    static::$paths[$item] = $routeDefinitor['controllerPath'];
219
                }
220
            }
221
        }
222
    }
223
224
    /**
225
     * get route setRoute method
226
     *
227
     * @param $params
228
     * @param $function
229
     * @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...
230
     */
231
    public static function setRoute($params,$function,$controller=null)
232
    {
233
        [$pattern,$route]   = $params;
234
        [$class,$method]    = explode("@",$route);
235
236
        $patternList = array_values(
237
            array_filter(explode("/",$pattern),'strlen')
238
        );
239
240
        static::$routes['pattern'][] = $patternList;
241
        static::$routes['data'][] = [
242
            'method'        => $method,
243
            'class'         => $class,
244
            'http'          => $function,
245
            'controller'    => $controller,
246
            'namespace'     => static::$namespace,
247
            'endpoint'      => strtolower(str_replace(StaticPathList::$controllerBundleName,'',static::$namespace))
248
        ];
249
    }
250
251
    /**
252
     * is route variable regex pattern
253
     *
254
     * @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...
255
     * @return bool
256
     */
257
    public static function isMatchVaribleRegexPattern($value=null)
258
    {
259
        // determines if the variable that can be used
260
        // in the route file meets the regex rule.
261
        return (preg_match('@\{(.*?)\}@is',$value)) ? true : false;
262
    }
263
264
265
    /**
266
     * is optional route variable regex pattern
267
     *
268
     * @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...
269
     * @return bool
270
     */
271
    public static function isOptionalVaribleRegexPattern($value=null)
272
    {
273
        // determines if the variable that can be used
274
        // in the route file meets the regex rule.
275
        return preg_match('@\{[a-z]+\?\}@is',$value) ? true : false;
276
    }
277
278
279
}