-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResolver.php
251 lines (218 loc) · 5.24 KB
/
Resolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace Darya\View;
use InvalidArgumentException;
/**
* Finds and instantiates views of the given implementation using the given base
* paths and file extensions.
*
* Optionally shares arguments and configuration variables with all templates
* that are resolved.
*
* @author Chris Andrew <[email protected]>
*/
class Resolver
{
/**
* View implementor to resolve.
*
* @var string
*/
protected $engine;
/**
* Paths to search for templates within.
*
* @var array
*/
protected $basePaths = array();
/**
* Template file extensions to search for.
*
* @var array
*/
protected $extensions = array();
/**
* Extra directories to search within.
*
* @var array
*/
protected $directories = array('views');
/**
* Arguments to assign to all views that are resolved.
*
* @var array
*/
protected $shared = array();
/**
* Configuration variables to set for all views that are resolved.
*
* @var array
*/
protected $config = array();
/**
* Normalise the given path.
*
* @param string $path The path to normalise
* @return string The normalised path
*/
public static function normalise($path)
{
return preg_replace('~[\\\|/]+~', '/', rtrim($path, '\/'));
}
/**
* Create a new view resolver.
*
* @param string $engine View implementor class to resolve
* @param string|array $basePath [optional] Single path or set of paths to search within
* @param string|array $extensions [optional] Template file extensions to search for
*/
public function __construct($engine, $basePath = null, $extensions = array())
{
$this->setEngine($engine);
if ($basePath) {
$this->registerBasePaths($basePath);
}
if (!empty($extensions)) {
$this->registerExtensions($extensions);
}
}
/**
* Set the engine (View implementor class) to resolve.
*
* @param string $engine
*/
public function setEngine($engine)
{
if (!class_exists($engine) || !is_subclass_of($engine, 'Darya\View\View')) {
throw new InvalidArgumentException("View engine $engine does not exist or does not implement Darya\View\View");
}
$this->engine = $engine;
}
/**
* Register a base path or set of base paths to resolve views from.
*
* @param string|array $path Single path or set of paths
*/
public function registerBasePaths($path)
{
if (is_array($path)) {
$this->basePaths = array_merge($this->basePaths, $path);
} else {
$this->basePaths[] = $path;
}
}
/**
* Register file extensions to consider when resolving template files.
*
* @param string|array $extensions
*/
public function registerExtensions($extensions)
{
foreach ((array) $extensions as $extension) {
$this->extensions[] = '.' . ltrim($extension, '.');
}
}
/**
* Register extra directory names to search within when resolving template
* files.
*
* 'views' is registered by default.
*
* @param string|array $directories
*/
public function registerDirectories($directories)
{
$directories = (array) $directories;
foreach ($directories as $key => $directory) {
$directories[$key] = ltrim($directory, '\/');
}
$this->directories = array_merge($this->directories, $directories);
}
/**
* Set arguments to assign to all resolved views.
*
* @param array $arguments
*/
public function share(array $arguments = array())
{
$this->shared = array_merge($this->shared, $arguments);
}
/**
* Set configuration variables to set for all resolved views.
*
* @param array $config
*/
public function shareConfig(array $config = array())
{
$this->config = array_merge($this->config, $config);
}
/**
* Generate file paths to attempt when resolving template files.
*
* @param string $path
* @return array
*/
public function generate($path)
{
$dirname = dirname($path);
$dir = $dirname != '.' ? $dirname : '';
$file = basename($path);
$paths = array();
foreach ($this->basePaths as $basePath) {
foreach ($this->extensions as $extension) {
$paths[] = "$basePath/$path$extension";
foreach ($this->directories as $directory) {
$paths[] = "$basePath/$dir/$directory/$file$extension";
}
}
}
return $paths;
}
/**
* Find a template file using the given path.
*
* @param string $path The template path to find
* @return string
*/
public function resolve($path)
{
$path = static::normalise($path);
if (is_file($path)) {
return $path;
}
$filePaths = $this->generate($path);
foreach ($filePaths as $filePath) {
if (is_file($filePath)) {
return $filePath;
}
}
}
/**
* Determine whether a template exists at the given path.
*
* @param string $path The template path to check
* @return bool
*/
public function exists($path)
{
return $this->resolve($path) !== null;
}
/**
* Resolve a view instance with a template at the given path, as well as
* shared arguments and config.
*
* @param string $path [optional] Template path
* @param array $arguments [optional] Arguments to assign to the View
* @return View
*/
public function create($path = null, $arguments = array())
{
$file = $this->resolve($path);
$engine = $this->engine;
/**
* @var View $view
*/
$engine = new $engine($file, array_merge($this->shared, $arguments), $this->config);
$engine->setResolver($this);
return $engine;
}
}