1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Routing\Attributes; |
15
|
|
|
|
16
|
|
|
use Attribute; |
17
|
|
|
use Valkyrja\Routing\Exceptions\InvalidRoutePath; |
18
|
|
|
use Valkyrja\Routing\Message; |
|
|
|
|
19
|
|
|
use Valkyrja\Routing\Models\Parameter; |
|
|
|
|
20
|
|
|
use Valkyrja\Routing\Models\Route as Model; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Attribute Route. |
24
|
|
|
* |
25
|
|
|
* @author Melech Mizrachi |
26
|
|
|
*/ |
27
|
|
|
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)] |
28
|
|
|
class Route extends Model |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param non-empty-string $path The path |
|
|
|
|
32
|
|
|
* @param Parameter[]|null $parameters The parameters |
33
|
|
|
* @param class-string<Message>[]|null $messages The messages |
34
|
|
|
* |
35
|
|
|
* @throws InvalidRoutePath |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
string $path, |
39
|
|
|
string|null $name = null, |
40
|
|
|
array|null $methods = null, |
41
|
|
|
array|null $parameters = null, |
42
|
|
|
array|null $middleware = null, |
43
|
|
|
array|null $messages = null, |
44
|
|
|
bool|null $secure = null, |
45
|
|
|
string|null $to = null, |
46
|
|
|
int|null $code = null, |
47
|
|
|
) { |
48
|
|
|
$this->path = $path; |
49
|
|
|
|
50
|
|
|
/** @psalm-suppress TypeDoesNotContainType Not everyone will use Psalm :) */ |
51
|
|
|
if ($path === '') { |
52
|
|
|
throw new InvalidRoutePath("Path must be a non-empty-string."); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($name !== null && $name !== '') { |
56
|
|
|
$this->name = $name; |
57
|
|
|
} else { |
58
|
|
|
$this->name = $path; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($methods !== null) { |
62
|
|
|
$this->methods = $methods; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($parameters !== null) { |
66
|
|
|
$this->setParameters($parameters); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($secure !== null) { |
70
|
|
|
$this->secure = $secure; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($to !== '') { |
74
|
|
|
$this->setTo($to); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->setMiddleware($middleware); |
78
|
|
|
$this->setCode($code); |
79
|
|
|
$this->setMessages($messages); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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: