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