1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Plates\Template; |
4
|
|
|
|
5
|
|
|
use League\Plates; |
6
|
|
|
|
7
|
|
|
function absoluteResolveName($file_exists = 'file_exists') { |
8
|
|
|
return function(ResolveNameArgs $args, $next) use ($file_exists) { |
9
|
|
|
if ($file_exists($args->name)) { |
10
|
|
|
return $args->name; |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
return $next($args); |
14
|
|
|
}; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** appends an extension to the name */ |
18
|
|
|
function extResolveName($ext = 'phtml') { |
19
|
|
|
$full_ext = '.' . $ext; |
20
|
|
|
$ext_len = strlen($full_ext); |
21
|
|
|
return function(ResolveNameArgs $args, $next) use ($full_ext, $ext_len) { |
22
|
|
|
// ext is already there, just skip |
23
|
|
|
if (strrpos($args->name, $full_ext) === strlen($args->name) - $ext_len) { |
24
|
|
|
return $next($args); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $next($args->withName($args->name . $full_ext)); |
28
|
|
|
}; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
function prefixResolveName($prefix) { |
32
|
|
|
return function(ResolveNameArgs $args, $next) use ($prefix) { |
33
|
|
|
if (strpos($args->name, '/') === 0) { |
34
|
|
|
return $next($args); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $next($args->withName( |
38
|
|
|
Plates\Util\joinPath([$prefix, $args->name]) |
39
|
|
|
)); |
40
|
|
|
}; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** If the template context stores a current directory and */ |
44
|
|
|
function relativeResolveName() { |
45
|
|
|
return function(ResolveNameArgs $args, $next) { |
46
|
|
|
$is_relative = ( |
47
|
|
|
strpos($args->name, './') === 0 |
48
|
|
|
|| strpos($args->name, '../') === 0 |
49
|
|
|
) && isset($args->context['current_directory']); |
50
|
|
|
|
51
|
|
|
if (!$is_relative) { |
52
|
|
|
return $next($args); // nothing to do |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $next($args->withName( |
56
|
|
|
Plates\Util\joinPath([$args->context['current_directory'], $args->name]) |
57
|
|
|
)); |
58
|
|
|
}; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** Just return the name as is to be rendered. Expects at this point for the name to be fully built. */ |
62
|
|
|
function idResolveName() { |
63
|
|
|
return function(ResolveNameArgs $args) { |
64
|
|
|
return $args->name; |
65
|
|
|
}; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function platesResolveName(array $config = []) { |
69
|
|
|
return Plates\Util\stackGroup(array_filter([ |
70
|
|
|
absoluteResolveName(), |
71
|
|
|
relativeResolveName(), |
72
|
|
|
isset($config['ext']) ? extResolveName($config['ext']) : extResolveName(), |
73
|
|
|
isset($config['base_dir']) ? prefixResolveName($config['base_dir']) : null, |
74
|
|
|
idResolveName(), |
75
|
|
|
])); |
76
|
|
|
} |
77
|
|
|
|