1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ICanBoogie\Routing; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\HTTP\Request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The class defines options that can be used to define a route as well as means to normalize and |
18
|
|
|
* validate this definition. |
19
|
|
|
*/ |
20
|
|
|
class RouteDefinition |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Pattern of the route. |
24
|
|
|
*/ |
25
|
|
|
const PATTERN = 'pattern'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A controller class name (with an optional action) or a callable. |
29
|
|
|
*/ |
30
|
|
|
const CONTROLLER = 'controller'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The controller action. |
34
|
|
|
*/ |
35
|
|
|
const ACTION = 'action'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* An identifier. |
39
|
|
|
*/ |
40
|
|
|
const ID = 'id'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* A redirection target. |
44
|
|
|
*/ |
45
|
|
|
const LOCATION = 'location'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Request method(s) accepted by the route. |
49
|
|
|
*/ |
50
|
|
|
const VIA = 'via'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Route constructor, a class name for now. |
54
|
|
|
*/ |
55
|
|
|
const CONSTRUCTOR = 'class'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Normalizes a route definition. |
59
|
|
|
* |
60
|
|
|
* @param array $definition |
61
|
|
|
*/ |
62
|
|
|
static public function normalize(array &$definition) |
63
|
|
|
{ |
64
|
|
|
if (isset($definition[self::CONTROLLER])) |
65
|
|
|
{ |
66
|
|
|
$controller = $definition[self::CONTROLLER]; |
67
|
|
|
|
68
|
|
|
if (is_string($controller) && strpos($controller, RouteMaker::CONTROLLER_ACTION_SEPARATOR)) |
69
|
|
|
{ |
70
|
|
|
list($controller, $action) = explode(RouteMaker::CONTROLLER_ACTION_SEPARATOR, $controller); |
71
|
|
|
|
72
|
|
|
$definition[self::CONTROLLER] = $controller; |
73
|
|
|
$definition[self::ACTION] = $action; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (empty($definition[self::VIA])) |
78
|
|
|
{ |
79
|
|
|
$definition[self::VIA] = Request::METHOD_ANY; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Ensures that a route definition has an identifier and generates one if required. |
85
|
|
|
* |
86
|
|
|
* @param array $definition |
87
|
|
|
* |
88
|
|
|
* @return string The route identifier. |
89
|
|
|
*/ |
90
|
|
|
static public function ensure_has_id(array &$definition) |
91
|
|
|
{ |
92
|
|
|
if (empty($definition[self::ID])) |
93
|
|
|
{ |
94
|
|
|
$definition[self::ID] = self::generate_anonymous_id(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $definition[self::ID]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
static private $anonymous_id_count; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Generates an anonymous route identifier. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
static private function generate_anonymous_id() |
108
|
|
|
{ |
109
|
|
|
return 'anonymous_route_' . ++self::$anonymous_id_count; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Asserts that a route definition is valid. |
114
|
|
|
* |
115
|
|
|
* @param array $definition |
116
|
|
|
* |
117
|
|
|
* @throws PatternNotDefined when the pattern is not defined |
118
|
|
|
* @throws ControllerNotDefined when both controller and location are not defined. |
119
|
|
|
*/ |
120
|
|
|
static public function assert_is_valid(array $definition) |
121
|
|
|
{ |
122
|
|
View Code Duplication |
if (empty($definition[self::PATTERN])) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
throw new PatternNotDefined(\ICanBoogie\format("Pattern is not defined: !route", [ |
125
|
|
|
|
126
|
|
|
'route' => $definition |
127
|
|
|
|
128
|
|
|
])); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
if (empty($definition[self::CONTROLLER]) && empty($definition[self::LOCATION])) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
throw new ControllerNotDefined(\ICanBoogie\format("Controller is not defined: !route", [ |
134
|
|
|
|
135
|
|
|
'route' => $definition |
136
|
|
|
|
137
|
|
|
])); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* No instance should be created from this class. |
143
|
|
|
* |
144
|
|
|
* @codeCoverageIgnore |
145
|
|
|
*/ |
146
|
|
|
private function __construct() |
147
|
|
|
{ |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.