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\Render; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Support functions for template resolvers. |
16
|
|
|
*/ |
17
|
|
|
trait TemplateResolverTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Resolves path tries. |
21
|
|
|
* |
22
|
|
|
* The method resolves a try path collection from a collection of roots, template name, and |
23
|
|
|
* extension collection. |
24
|
|
|
* |
25
|
|
|
* @param array $paths Template directories paths. |
26
|
|
|
* @param string $name Template name. |
27
|
|
|
* @param array $extensions Supported extensions. |
28
|
|
|
* |
29
|
|
|
* @return array A collection of candidate template pathnames. |
30
|
|
|
*/ |
31
|
|
|
protected function resolve_tries(array $paths, $name, array $extensions) |
32
|
|
|
{ |
33
|
|
|
$extension = pathinfo($name, PATHINFO_EXTENSION); |
34
|
|
|
|
35
|
|
|
if ($extension && in_array('.' . $extension, $extensions)) |
36
|
|
|
{ |
37
|
|
|
$name = substr($name, 0, -strlen($extension) - 1); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$tries = []; |
41
|
|
|
$dirname = dirname($name); |
42
|
|
|
$basename = basename($name); |
43
|
|
|
|
44
|
|
|
foreach ($paths as $path) |
45
|
|
|
{ |
46
|
|
|
$parent_dir = basename(dirname($path)); |
47
|
|
|
|
48
|
|
|
foreach ($extensions as $extension) |
49
|
|
|
{ |
50
|
|
|
$filename = $name; |
51
|
|
|
|
52
|
|
|
if ($dirname && $dirname == $parent_dir) |
53
|
|
|
{ |
54
|
|
|
$filename = $basename; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$filename = $filename . $extension; |
58
|
|
|
$pathname = $path . $filename; |
59
|
|
|
|
60
|
|
|
$tries[] = $pathname; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $tries; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Resolves a template path. |
69
|
|
|
* |
70
|
|
|
* The method returns the pathname of the first file matching the path collection. The tried |
71
|
|
|
* paths are collected in `$tried`. |
72
|
|
|
* |
73
|
|
|
* @param array $tries Pathname collection, as returned by {@link resolve_tries()}. |
74
|
|
|
* @param array $tried Tried pathname collection. |
75
|
|
|
* |
76
|
|
|
* @return string|null |
77
|
|
|
*/ |
78
|
|
|
protected function resolve_path(array $tries, &$tried) |
79
|
|
|
{ |
80
|
|
|
foreach ($tries as $pathname) |
81
|
|
|
{ |
82
|
|
|
$tried[] = $pathname; |
83
|
|
|
|
84
|
|
|
if (file_exists($pathname)) |
85
|
|
|
{ |
86
|
|
|
return $pathname; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|