|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Plates\Extension\Path; |
|
4
|
|
|
|
|
5
|
|
|
use League\Plates; |
|
6
|
|
|
|
|
7
|
|
|
function absoluteResolvePath($file_exists = 'file_exists') { |
|
8
|
|
|
return function(ResolvePathArgs $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 extResolvePath($ext = 'phtml') { |
|
19
|
|
|
$full_ext = '.' . $ext; |
|
20
|
|
|
$ext_len = strlen($full_ext); |
|
21
|
|
|
return function(ResolvePathArgs $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 prefixResolvePath($prefix) { |
|
32
|
|
|
return function(ResolvePathArgs $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 relativeResolvePath() { |
|
45
|
|
|
return function(ResolvePathArgs $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 idResolvePath() { |
|
63
|
|
|
return function(ResolvePathArgs $args) { |
|
64
|
|
|
return $args->name; |
|
65
|
|
|
}; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
function platesResolvePath(array $config = []) { |
|
69
|
|
|
return Plates\Util\stackGroup(array_filter([ |
|
70
|
|
|
absoluteResolvePath(), |
|
71
|
|
|
relativeResolvePath(), |
|
72
|
|
|
isset($config['ext']) ? extResolvePath($config['ext']) : extResolvePath(), |
|
73
|
|
|
isset($config['base_dir']) ? prefixResolvePath($config['base_dir']) : null, |
|
74
|
|
|
idResolvePath(), |
|
75
|
|
|
])); |
|
76
|
|
|
} |
|
77
|
|
|
|