|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\walkway; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This class represents the root of an independent collection of Routes. |
|
9
|
|
|
* |
|
10
|
|
|
* @see Route |
|
11
|
|
|
*/ |
|
12
|
|
|
class Module extends Route |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* A map of regular expression pattern substitutions to apply to every |
|
16
|
|
|
* pattern encountered, as a means fo pre-processing patterns. Provides a |
|
17
|
|
|
* useful means of adding your own custom patterns for convenient reuse. |
|
18
|
|
|
* |
|
19
|
|
|
* @var Closure[] map where full regular expression => substitution closure |
|
20
|
|
|
* |
|
21
|
|
|
* @see $symbols |
|
22
|
|
|
* @see preparePattern() |
|
23
|
|
|
* @see init() |
|
24
|
|
|
*/ |
|
25
|
|
|
public $substitutions = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Symbols used by the built-in standard substitution pattern, which provides |
|
29
|
|
|
* a convenient short-hand syntax for placeholder tokens. The built-in standard |
|
30
|
|
|
* symbols are: |
|
31
|
|
|
* |
|
32
|
|
|
* <pre> |
|
33
|
|
|
* 'int' => '\d+' |
|
34
|
|
|
* 'slug' => '[a-z0-9-]+' |
|
35
|
|
|
* </pre> |
|
36
|
|
|
* |
|
37
|
|
|
* Which provides support for simplified named routes, such as: |
|
38
|
|
|
* |
|
39
|
|
|
* <pre> |
|
40
|
|
|
* 'user/<user_id:int>' |
|
41
|
|
|
* 'tags/<tag:slug>' |
|
42
|
|
|
* </pre> |
|
43
|
|
|
* |
|
44
|
|
|
* For which the resulting patterns would be: |
|
45
|
|
|
* |
|
46
|
|
|
* <pre> |
|
47
|
|
|
* 'user/(?<user_id:\d+>)' |
|
48
|
|
|
* 'tags/(?<slug:[a-z0-9-]>)' |
|
49
|
|
|
* </pre> |
|
50
|
|
|
* |
|
51
|
|
|
* @var string[] map where symbol name => partial regular expression |
|
52
|
|
|
* |
|
53
|
|
|
* @see init() |
|
54
|
|
|
*/ |
|
55
|
|
|
public $symbols = array(); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Closure|null event-handler for diagnostic messages |
|
59
|
|
|
*/ |
|
60
|
|
|
public $onLog = null; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var InvokerInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
public $invoker; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param InvokerInterface|null $invoker optional custom invoker |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function __construct(InvokerInterface $invoker = null) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->module = $this; |
|
73
|
1 |
|
$this->invoker = $invoker ?: $this->createDefaultInvoker(); |
|
74
|
|
|
|
|
75
|
1 |
|
$this->init(); |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Prepares a regular expression pattern by applying the patterns and callbacks |
|
80
|
|
|
* defined by {@link $substitutions} to it. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $pattern unprocessed pattern |
|
83
|
|
|
* |
|
84
|
|
|
* @return string pre-processed pattern |
|
85
|
|
|
* |
|
86
|
|
|
* @throws RoutingException if the regular expression fails to execute |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function preparePattern($pattern) |
|
89
|
|
|
{ |
|
90
|
1 |
|
foreach ($this->substitutions as $subpattern => $fn) { |
|
91
|
1 |
|
$pattern = @preg_replace_callback($subpattern, $fn, $pattern); |
|
92
|
|
|
|
|
93
|
1 |
|
if ($pattern === null) { |
|
94
|
1 |
|
throw new RoutingException("invalid substitution pattern: {$subpattern}"); |
|
95
|
|
|
} |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return $pattern; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Initialize the Module upon construction - override as needed. |
|
103
|
|
|
*/ |
|
104
|
1 |
|
protected function init() |
|
105
|
|
|
{ |
|
106
|
1 |
|
$module = $this; |
|
107
|
|
|
|
|
108
|
1 |
|
$this->vars['route'] = $this; |
|
109
|
1 |
|
$this->vars['module'] = $this; |
|
110
|
|
|
|
|
111
|
|
|
// define a default pattern-substitution with support for some common symbols: |
|
112
|
|
|
|
|
113
|
1 |
|
$this->substitutions['/(?<!\(\?)<([^\:]+)\:([^>]+)>/'] = function ($matches) use ($module) { |
|
114
|
1 |
|
if (isset($module->symbols[$matches[2]])) { |
|
115
|
1 |
|
$matches[2] = $module->symbols[$matches[2]]; |
|
116
|
1 |
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
return "(?<{$matches[1]}>{$matches[2]})"; |
|
119
|
|
|
}; |
|
120
|
|
|
|
|
121
|
|
|
// define common symbols for the default pattern-substitution: |
|
122
|
|
|
|
|
123
|
1 |
|
$this->symbols = array( |
|
|
|
|
|
|
124
|
1 |
|
'int' => '\d+', |
|
125
|
1 |
|
'slug' => '[a-z0-9-]+', |
|
126
|
|
|
); |
|
127
|
1 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return InvokerInterface |
|
131
|
|
|
*/ |
|
132
|
1 |
|
protected function createDefaultInvoker() |
|
133
|
|
|
{ |
|
134
|
1 |
|
return new Invoker(); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..