|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Venta\Routing; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Psr\Http\Message\UriInterface; |
|
9
|
|
|
use Venta\Contracts\Http\Request; |
|
10
|
|
|
use Venta\Contracts\Routing\Route; |
|
|
|
|
|
|
11
|
|
|
use Venta\Contracts\Routing\RouteCollection; |
|
|
|
|
|
|
12
|
|
|
use Venta\Contracts\Routing\UrlGenerator as UrlGeneratorContract; |
|
13
|
|
|
use Venta\Routing\Exception\RouteNotFoundException; |
|
14
|
|
|
|
|
15
|
|
|
class UrlGenerator implements UrlGeneratorContract |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ServerRequestInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $request; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var RouteCollection |
|
24
|
|
|
*/ |
|
25
|
|
|
private $routes; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var UriInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $uri; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* UrlGenerator constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param Request $request |
|
36
|
|
|
* @param RouteCollection $routes |
|
37
|
|
|
* @param UriInterface $uri |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Request $request, RouteCollection $routes, UriInterface $uri) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->request = $request; |
|
42
|
|
|
$this->routes = $routes; |
|
|
|
|
|
|
43
|
|
|
$this->uri = $uri; |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritDoc |
|
48
|
|
|
*/ |
|
49
|
|
View Code Duplication |
public function toCurrent(array $variables = [], array $query = []): UriInterface |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$route = $this->request->getRoute(); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
if ($route === null) { |
|
54
|
|
|
throw new RouteNotFoundException( |
|
55
|
|
|
sprintf('Unable to generate an URL for current.') |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->buildRouteUri($route, $variables, $query); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritDoc |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function toRoute(string $routeName, array $variables = [], array $query = []): UriInterface |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$route = $this->routes->findByName($routeName); |
|
68
|
|
|
|
|
69
|
|
|
if ($route === null) { |
|
70
|
|
|
throw new RouteNotFoundException( |
|
71
|
|
|
sprintf('Unable to generate an URL for the named route "%s" as such route does not exist.', $routeName) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->buildRouteUri($route, $variables, $query); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Builds URI for provided route instance. |
|
80
|
|
|
* |
|
81
|
|
|
* @param Route $route |
|
82
|
|
|
* @param array $variables |
|
83
|
|
|
* @param array $query |
|
84
|
|
|
* @return UriInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
private function buildRouteUri(Route $route, array $variables = [], array $query = []): UriInterface |
|
87
|
|
|
{ |
|
88
|
|
|
$uri = $this->uri |
|
89
|
|
|
->withScheme($route->getScheme() ?: $this->request->getUri()->getScheme()) |
|
90
|
|
|
->withHost($route->getHost() ?: $this->request->getUri()->getHost()) |
|
91
|
|
|
->withPath($route->compilePath($variables)); |
|
92
|
|
|
|
|
93
|
|
|
if ($query) { |
|
|
|
|
|
|
94
|
|
|
$uri = $uri->withQuery(http_build_query($query)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $uri; |
|
98
|
|
|
} |
|
99
|
|
|
} |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: