1 | <?php |
||
20 | class Router |
||
21 | { |
||
22 | /** |
||
23 | * @var RouteCollection|Route[] |
||
24 | */ |
||
25 | protected $routes; |
||
26 | |||
27 | /** |
||
28 | * @var |
||
29 | */ |
||
30 | protected $routeParser; |
||
31 | |||
32 | /** |
||
33 | * @var Dispatcher |
||
34 | */ |
||
35 | protected $dispatcher; |
||
36 | |||
37 | /** |
||
38 | * 缓存路由文件,默认不缓存 |
||
39 | * |
||
40 | * @var string|False |
||
41 | */ |
||
42 | protected $cacheFile = false; |
||
43 | |||
44 | public function __construct(RouteCollection $routes, RouteParser $parser = null) |
||
49 | |||
50 | /** |
||
51 | * 设置路由集合 |
||
52 | * |
||
53 | * @param RouteCollection $routes |
||
54 | */ |
||
55 | public function setRoutes($routes): void |
||
59 | |||
60 | /** |
||
61 | * 返回路由集合 |
||
62 | * |
||
63 | * @return RouteCollection |
||
64 | */ |
||
65 | public function getRoutes(): RouteCollection |
||
69 | |||
70 | /** |
||
71 | * 搜索路由 |
||
72 | * |
||
73 | * @param string $name |
||
74 | * @return Route |
||
75 | */ |
||
76 | public function searchRoute($name) |
||
80 | |||
81 | /** |
||
82 | * 调度请求 |
||
83 | * |
||
84 | * @param ServerRequestInterface $request |
||
85 | * @return array |
||
86 | */ |
||
87 | public function dispatch(ServerRequestInterface $request) |
||
95 | |||
96 | /** |
||
97 | * 设置自定义路由调度器 |
||
98 | * |
||
99 | * @param Dispatcher $dispatcher |
||
100 | */ |
||
101 | public function setDispatcher(Dispatcher $dispatcher) |
||
105 | |||
106 | /** |
||
107 | * 获取路由调度器 |
||
108 | * |
||
109 | * @return Dispatcher |
||
110 | */ |
||
111 | public function getDispatcher() |
||
118 | |||
119 | /** |
||
120 | * 设置路由缓存文件,为空表示禁用路由缓存 |
||
121 | * |
||
122 | * @param string|false $cacheFile |
||
123 | * |
||
124 | * @return static |
||
125 | * |
||
126 | * @throws \InvalidArgumentException If cacheFile is not a string or not false |
||
127 | * @throws \RuntimeException If cacheFile directory is not writable |
||
128 | */ |
||
129 | public function setCacheFile($cacheFile) |
||
147 | |||
148 | /** |
||
149 | * 创建路由调度 |
||
150 | * |
||
151 | * @return Dispatcher |
||
152 | */ |
||
153 | protected function createDispatcher() |
||
172 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: