|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\TwigView\Lib; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class TreeScanner |
|
7
|
|
|
* @package WyriHaximus\TwigView\Lib |
|
8
|
|
|
*/ |
|
9
|
|
|
class TreeScanner |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Return all sections (app & plugins) with an Template directory. |
|
13
|
|
|
* |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
|
|
public static function all() |
|
17
|
|
|
{ |
|
18
|
|
|
return static::deepen(RelativeScanner::all()); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Return all templates for a given plugin. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $plugin The plugin to find all templates for. |
|
25
|
|
|
* |
|
26
|
|
|
* @return mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function plugin($plugin) |
|
29
|
|
|
{ |
|
30
|
|
|
return static::deepen([ |
|
|
|
|
|
|
31
|
|
|
$plugin => RelativeScanner::plugin($plugin), |
|
32
|
|
|
])[$plugin]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Strip the absolute path of template's paths for all given sections. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $sections Sections to iterate over. |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
3 |
|
protected static function deepen($sections) |
|
43
|
|
|
{ |
|
44
|
|
|
foreach ($sections as $section => $paths) { |
|
|
|
|
|
|
45
|
|
|
$sections[$section] = static::convertToTree($paths); |
|
46
|
1 |
|
} |
|
47
|
3 |
|
return $sections; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Turn a set of paths into a tree. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $paths Paths to turn into a tree. |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
1 |
|
protected static function convertToTree(array $paths) |
|
58
|
|
|
{ |
|
59
|
|
|
foreach ($paths as $index => $path) { |
|
60
|
|
|
static::convertPathToTree($paths, $index, $path); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return $paths; |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Convert a path into a tree when it contains a directory separator. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $paths The paths to work on. |
|
70
|
|
|
* @param mixed $index Index of $path. |
|
71
|
|
|
* @param string $path Path to breakup and turn into a tree. |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
protected static function convertPathToTree(array &$paths, $index, $path) |
|
76
|
|
|
{ |
|
77
|
|
|
if (strpos($path, DIRECTORY_SEPARATOR) !== false) { |
|
78
|
|
|
$chunks = explode(DIRECTORY_SEPARATOR, $path); |
|
79
|
|
|
$paths = static::branch($paths, $chunks); |
|
80
|
|
|
unset($paths[$index]); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Create a branch for the current level and push a twig on it. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $paths Paths to append. |
|
88
|
|
|
* @param array $branches Branches to use untill only one left. |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
protected static function branch(array $paths, array $branches) |
|
93
|
|
|
{ |
|
94
|
|
|
$twig = array_shift($branches); |
|
95
|
|
|
if (count($branches) == 0) { |
|
96
|
|
|
$paths[] = $twig; |
|
97
|
|
|
return $paths; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (!isset($paths[$twig])) { |
|
101
|
|
|
$paths[$twig] = []; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$paths[$twig] = static::branch($paths[$twig], $branches); |
|
105
|
|
|
|
|
106
|
|
|
return $paths; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: