1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Plates\Extension\Path; |
4
|
|
|
|
5
|
|
|
use League\Plates; |
6
|
|
|
|
7
|
|
|
function resolvePathCompose(callable $resolve_path) { |
8
|
|
|
return function(Plates\Template $template) use ($resolve_path) { |
9
|
24 |
|
return $template->with('path', $resolve_path(ResolvePathArgs::fromTemplate($template, $resolve_path))); |
10
|
24 |
|
}; |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
function normalizeNameCompose(callable $normalize_name) { |
14
|
|
|
return function(Plates\Template $template) use ($normalize_name) { |
15
|
32 |
|
return $template->with( |
16
|
32 |
|
'normalized_name', |
17
|
32 |
|
Plates\Util\isPath($template->name) ? $normalize_name($template->get('path')) : $template->name |
18
|
|
|
); |
19
|
32 |
|
}; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function stripExtNormalizeName() { |
23
|
|
|
return function($name) { |
24
|
24 |
|
$ext = pathinfo($name, PATHINFO_EXTENSION); |
25
|
24 |
|
if (!$ext) { |
26
|
4 |
|
return $name; |
27
|
|
|
} |
28
|
|
|
|
29
|
20 |
|
return substr($name, 0, (strlen($ext) + 1) * -1); // +1 for the leading `.` |
30
|
32 |
|
}; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
function stripPrefixNormalizeName(array $prefixes) { |
34
|
32 |
|
$prefixes = array_filter($prefixes); |
35
|
|
|
return function($name) use ($prefixes) { |
36
|
24 |
|
foreach ($prefixes as $prefix) { |
37
|
20 |
|
if (strpos($name, $prefix . '/') === 0) { |
38
|
20 |
|
return substr($name, strlen($prefix) + 1); // +1 for the trailing `/` |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
8 |
|
return $name; |
43
|
32 |
|
}; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** appends an extension to the name */ |
47
|
|
|
|
48
|
|
|
function extResolvePath($ext = 'phtml') { |
49
|
32 |
|
$full_ext = '.' . $ext; |
50
|
32 |
|
$ext_len = strlen($full_ext); |
51
|
|
|
return function(ResolvePathArgs $args, $next) use ($full_ext, $ext_len) { |
52
|
|
|
// ext is already there, just skip |
53
|
32 |
|
if (strrpos($args->path, $full_ext) === strlen($args->path) - $ext_len) { |
54
|
8 |
|
return $next($args); |
55
|
|
|
} |
56
|
|
|
|
57
|
28 |
|
return $next($args->withPath($args->path . $full_ext)); |
58
|
32 |
|
}; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
function prefixResolvePath(array $prefixes, $file_exists = 'file_exists') { |
62
|
|
|
return function(ResolvePathArgs $args, $next) use ($prefixes, $file_exists) { |
63
|
28 |
|
if (!$prefixes) { |
|
|
|
|
64
|
|
|
return $next($args); |
65
|
|
|
} |
66
|
|
|
|
67
|
28 |
|
foreach ($prefixes as $cur_prefix) { |
68
|
28 |
|
$path = Plates\Util\isAbsolutePath($args->path) |
69
|
16 |
|
? $next($args) |
70
|
20 |
|
: $next($args->withPath( |
71
|
28 |
|
Plates\Util\joinPath([$cur_prefix, $args->path]) |
72
|
|
|
)); |
73
|
|
|
|
74
|
|
|
// we have a match, let's return |
75
|
28 |
|
if ($file_exists($path)) { |
76
|
28 |
|
return $path; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// at this point, we need to try the next prefix, but before we do, let's strip the prefix |
80
|
|
|
// if there is one since this might a be a relative path |
81
|
4 |
|
$stripped_args = null; |
82
|
4 |
|
foreach ($prefixes as $prefix) { |
83
|
4 |
|
if (strpos($path, $prefix) === 0) { |
84
|
4 |
|
$stripped_args = $args->withPath(substr($path, strlen($prefix))); // remove the prefix |
85
|
4 |
|
break; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// could not strip the prefix, so there's not point in continuing on |
90
|
4 |
|
if (!$stripped_args) { |
91
|
|
|
return $path; |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
$args = $stripped_args; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// at this point, none of the paths resolved into a valid path, let's just return the last one |
98
|
4 |
|
return $path; |
99
|
28 |
|
}; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** Figures out the path based off of the parent templates current path */ |
103
|
|
|
function relativeResolvePath() { |
104
|
|
|
return function(ResolvePathArgs $args, $next) { |
105
|
32 |
|
$is_relative = Plates\Util\isRelativePath($args->path) && $args->template->parent; |
106
|
|
|
|
107
|
32 |
|
if (!$is_relative) { |
108
|
28 |
|
return $next($args); // nothing to do |
109
|
|
|
} |
110
|
|
|
|
111
|
12 |
|
$current_directory = dirname($args->template->parent()->get('path')); |
112
|
12 |
|
return $next($args->withPath( |
113
|
12 |
|
Plates\Util\joinPath([$current_directory, $args->path]) |
114
|
|
|
)); |
115
|
32 |
|
}; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
function idResolvePath() { |
119
|
|
|
return function(ResolvePathArgs $args, $next) { |
120
|
52 |
|
return $args->path; |
121
|
52 |
|
}; |
122
|
|
|
} |
123
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.