|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BlitzPHP\Annotations\Http; |
|
13
|
|
|
|
|
14
|
|
|
use BlitzPHP\Annotations\BaseAnnotation; |
|
15
|
|
|
use mindplay\annotations\AnnotationException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Annotation pour le mappage des requêtes Web sur les méthodes dans les ccontrôleurs |
|
19
|
|
|
* avec des signatures de méthode flexibles. |
|
20
|
|
|
* |
|
21
|
|
|
* @usage('class'=>true, 'method'=> true, 'inherited'=>true) |
|
22
|
|
|
*/ |
|
23
|
|
|
class RequestMappingAnnotation extends BaseAnnotation |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Méthodes autorisés |
|
27
|
|
|
*/ |
|
28
|
|
|
private const VALID_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'TRACE']; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string[] |
|
32
|
|
|
* |
|
33
|
|
|
* Les méthodes de requête HTTP à mapper, en limitant le mappage principal : |
|
34
|
|
|
* GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE. |
|
35
|
|
|
* <p><b>Pris en charge au niveau du contrôleur ainsi qu'au niveau de la méthode !</b> |
|
36
|
|
|
* Lorsqu'il est utilisé au niveau du contrôleur, tous les mappages au niveau de la méthode héritent de ce |
|
37
|
|
|
* Restriction de la méthode HTTP. |
|
38
|
|
|
*/ |
|
39
|
|
|
public $method; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
* |
|
44
|
|
|
* Les URI de mappage de chemin (par exemple, {@code "/profile"}). |
|
45
|
|
|
* <p>Les modèles de chemin de style Ant sont également pris en charge (par exemple, {@code "/profile/**"}). |
|
46
|
|
|
* Au niveau de la méthode, les chemins relatifs (par exemple, {@code "edit"}) sont pris en charge |
|
47
|
|
|
* dans le mappage principal exprimé au niveau du contôleur. |
|
48
|
|
|
* <p><b>Pris en charge au niveau du contrôleur ainsi qu'au niveau de la méthode !</b> |
|
49
|
|
|
* Lorsqu'ils sont utilisés au niveau du contrôleur, tous les mappages au niveau de la méthode héritent |
|
50
|
|
|
* ce mappage primaire, en le rétrécissant pour une méthode de gestionnaire spécifique. |
|
51
|
|
|
*/ |
|
52
|
|
|
public $path; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Initialisation de l'annotation. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function initAnnotation(array $properties) |
|
58
|
|
|
{ |
|
59
|
|
|
if (isset($properties[0])) { |
|
60
|
|
|
if (isset($properties[1])) { |
|
61
|
|
|
$this->method = (array) $properties[0]; |
|
62
|
|
|
|
|
63
|
|
|
if (! is_string($properties[1])) { |
|
64
|
|
|
throw new AnnotationException('RequestMappingAnnotation requires a string as path property'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->path = $properties[1]; |
|
68
|
|
|
|
|
69
|
|
|
unset($properties[1]); |
|
70
|
|
|
} else { |
|
71
|
|
|
if (is_string($properties[0])) { |
|
72
|
|
|
$this->path = $properties[0]; |
|
73
|
|
|
} elseif (is_array($properties[0])) { |
|
74
|
|
|
$this->method = $properties[0]; |
|
75
|
|
|
} else { |
|
76
|
|
|
throw new AnnotationException('Invalid type for RequestMappingAnnotation properties'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
unset($properties[0]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
parent::initAnnotation($properties); |
|
84
|
|
|
|
|
85
|
|
|
if ($this->method === ['*']) { |
|
86
|
|
|
$this->method = null; |
|
87
|
|
|
} |
|
88
|
|
|
if (! empty($this->method)) { |
|
89
|
|
|
$this->method = array_map('strtoupper', $this->method); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($this->method as $method) { |
|
92
|
|
|
if (! in_array($method, self::VALID_METHODS, true)) { |
|
93
|
|
|
throw new AnnotationException('`' . $method . '` is not a valid method for RequestMappingAnnotation'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|