Passed
Push — master ( 05b7e1...957c9d )
by Бабичев
03:54
created

src/Router/Router.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Exceptions;
6
use Bavix\Slice\Slice;
7
use Psr\Cache\CacheItemPoolInterface;
8
9
class Router
10
{
11
12
    /**
13
     * @var array
14
     */
15
    protected $classMap = [
16
        'configure' => Configure::class
17
    ];
18
19
    /**
20
     * @var Slice
21
     */
22
    protected $slice;
23
24
    /**
25
     * @var Slice
26
     */
27
    protected $configureSlice;
28
29
    /**
30
     * @var Route[]
31
     */
32
    protected $routes;
33
34
    /**
35
     * @var Configure
36
     */
37
    protected $configure;
38
39
    /**
40
     * @var string
41
     */
42
    protected $method;
43
44
    /**
45
     * @var string
46
     */
47
    protected $protocol;
48
49
    /**
50
     * @var string
51
     */
52
    protected $host;
53
54
    /**
55
     * @var string
56
     */
57
    protected $path;
58
59
    /**
60
     * @var CacheItemPoolInterface
61
     */
62
    protected $pool;
63
64
    /**
65
     * Router constructor.
66
     *
67
     * @param array|\Traversable|Slice $data
68
     * @param CacheItemPoolInterface   $pool
69
     */
70
    public function __construct($data, CacheItemPoolInterface $pool = null)
71
    {
72
        $this->slice    = Slice::from($data);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Bavix\Slice\Slice::from($data) of type object<self> is incompatible with the declared type object<Bavix\Slice\Slice> of property $slice.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
        $this->pool     = $pool;
74
        $this->method   = method();
75
        $this->protocol = protocol();
76
        $this->host     = host();
77
        $this->path     = path();
78
    }
79
80
    /**
81
     * @return Configure
82
     */
83
    protected function configure()
84
    {
85
        if (!$this->configure)
86
        {
87
            $class = $this->classMap['configure'];
88
89
            $this->configure = new $class($this->slice, $this->pool);
90
        }
91
92
        return $this->configure;
93
    }
94
95
    /**
96
     * @return Route
97
     *
98
     * @throws Exceptions\NotFound\Data
99
     */
100
    public function getCurrentRoute()
101
    {
102
        return $this->getRoute($this->path);
103
    }
104
105
    /**
106
     * @param string $path
107
     * @param string $host
108
     * @param string $protocol
109
     *
110
     * @return Route
111
     * @throws Exceptions\NotFound\Data
112
     */
113
    public function getRoute($path, $host = null, $protocol = null)
114
    {
115
        $uri = ($protocol ?? $this->protocol) . '://' . ($host ?? $this->host) . $path;
116
117
        return $this->find($uri);
118
    }
119
120
    /**
121
     * @return Slice
122
     */
123
    protected function configureSlice()
124
    {
125
        if (!$this->configureSlice)
126
        {
127
            $this->configureSlice = $this->configure()->data();
128
        }
129
130
        return $this->configureSlice;
131
    }
132
133
    /**
134
     * @return Route[]
135
     */
136
    public function routes()
137
    {
138
        if (empty($this->routes))
139
        {
140
            $this->routes = $this->configureSlice()->asArray();
141
        }
142
143
        return $this->routes;
144
    }
145
146
    /**
147
     * @param string $path
148
     *
149
     * @return Route
150
     *
151
     * @throws Exceptions\NotFound\Path
152
     */
153
    public function route($path)
154
    {
155
        $route = $this->configureSlice()->atData($path);
156
157
        if (empty($route))
158
        {
159
            throw new Exceptions\NotFound\Path('Route `' . $path . '` not found');
160
        }
161
162
        return $route;
163
    }
164
165
    /**
166
     * @param $uri
167
     *
168
     * @return Route
169
     *
170
     * @throws Exceptions\NotFound\Page
171
     */
172
    protected function find($uri)
173
    {
174
        /**
175
         * @var Route $route
176
         */
177
        foreach ($this->routes() as $key => $route)
178
        {
179
            if ($route->test($uri, $this->method))
180
            {
181
                return $route;
182
            }
183
        }
184
185
        throw new Exceptions\NotFound\Page('Page `' . $uri . '` not found', 404);
186
    }
187
188
}
189