1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Igni\Http\Router; |
4
|
|
|
|
5
|
|
|
use Igni\Http\Exception\GenericHttpException;; |
6
|
|
|
use Igni\Http\Route; |
|
|
|
|
7
|
|
|
use Igni\Http\Router as RouterInterface; |
8
|
|
|
use Symfony\Component\Routing\Route as SymfonyRoute; |
9
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException as SymfonyMethodNotAllowedException; |
10
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
11
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
12
|
|
|
use Symfony\Component\Routing\RequestContext; |
13
|
|
|
use Symfony\Component\Routing\RouteCollection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Wrapper for package symfony/router |
17
|
|
|
* |
18
|
|
|
* @package Igni\Http\Router |
19
|
|
|
*/ |
20
|
|
|
class Router implements RouterInterface |
21
|
|
|
{ |
22
|
|
|
/** @var RouteCollection */ |
23
|
|
|
protected $routeCollection; |
24
|
|
|
|
25
|
|
|
/** @var Route[] */ |
26
|
|
|
protected $routes = []; |
27
|
|
|
|
28
|
19 |
|
public function __construct() |
29
|
|
|
{ |
30
|
19 |
|
$this->routeCollection = new RouteCollection(); |
31
|
19 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Registers new route. |
35
|
|
|
* |
36
|
|
|
* @param \Igni\Http\Router\Route $route |
37
|
|
|
*/ |
38
|
14 |
|
public function addRoute(Route $route): void |
39
|
|
|
{ |
40
|
14 |
|
$name = $route->getName(); |
|
|
|
|
41
|
|
|
|
42
|
14 |
|
$baseRoute = new SymfonyRoute($route->getPath()); |
43
|
14 |
|
$baseRoute->setMethods($route->getMethods()); |
44
|
|
|
|
45
|
14 |
|
$this->routeCollection->add($name, $baseRoute); |
46
|
14 |
|
$this->routes[$name] = $route; |
47
|
14 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Finds route matching clients request. |
51
|
|
|
* |
52
|
|
|
* @param string $method request method. |
53
|
|
|
* @param string $path request path. |
54
|
|
|
* @return Route |
55
|
|
|
*/ |
56
|
15 |
|
public function findRoute(string $method, string $path): Route |
57
|
|
|
{ |
58
|
15 |
|
$matcher = new UrlMatcher($this->routeCollection, new RequestContext('/', $method)); |
59
|
|
|
try { |
60
|
15 |
|
$route = $matcher->match($path); |
61
|
4 |
|
} catch (ResourceNotFoundException $exception) { |
62
|
3 |
|
throw GenericHttpException::invalidUri($path, $method); |
63
|
1 |
|
} catch (SymfonyMethodNotAllowedException $exception) { |
64
|
1 |
|
throw GenericHttpException::methodNotAllowed($path, $method, $exception->getAllowedMethods()); |
65
|
|
|
} |
66
|
|
|
|
67
|
11 |
|
$routeName = $route['_route']; |
68
|
11 |
|
unset($route['_route']); |
69
|
|
|
|
70
|
11 |
|
return $this->routes[$routeName]->withAttributes($route); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
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: