Test Setup Failed
Push — master ( 5545ed...387d88 )
by Php Easy Api
04:23
created

Route::updateRouteParameters()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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