|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\TwigView\Lib; |
|
4
|
|
|
|
|
5
|
|
|
use Cake\Core\App; |
|
6
|
|
|
use Cake\Core\Plugin; |
|
7
|
|
|
use WyriHaximus\TwigView\View\TwigView; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Scanner |
|
11
|
|
|
* @package WyriHaximus\TwigView\Lib |
|
12
|
|
|
*/ |
|
13
|
|
|
class Scanner |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Return all sections (app & plugins) with an Template directory. |
|
17
|
|
|
* |
|
18
|
|
|
* @return array |
|
19
|
|
|
*/ |
|
20
|
|
|
// @codingStandardsIgnoreStart |
|
21
|
5 |
|
public static function all() |
|
22
|
|
|
{ |
|
23
|
|
|
// @codingStandardsIgnoreEnd |
|
24
|
5 |
|
$sections = []; |
|
25
|
|
|
|
|
26
|
5 |
|
foreach (App::path('Template') as $path) { |
|
27
|
3 |
|
if (is_dir($path)) { |
|
28
|
3 |
|
$sections['APP'] = isset($sections['APP']) ? $sections['APP'] : []; |
|
29
|
3 |
|
$sections['APP'] = array_merge($sections['APP'], static::iteratePath($path)); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
5 |
|
foreach (static::pluginsWithTemplates() as $plugin) { |
|
34
|
5 |
|
foreach (App::path('Template', $plugin) as $path) { |
|
35
|
5 |
|
if (is_dir($path)) { |
|
36
|
5 |
|
$sections[$plugin] = isset($sections[$plugin]) ? $sections[$plugin] : []; |
|
37
|
5 |
|
$sections[$plugin] = array_merge($sections[$plugin], static::iteratePath($path)); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
5 |
|
return static::clearEmptySections($sections); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Check sections a remove the ones without anything in them. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $sections Sections to check. |
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
5 |
|
protected static function clearEmptySections(array $sections) |
|
53
|
|
|
{ |
|
54
|
|
|
array_walk($sections, function ($templates, $index) use (&$sections) { |
|
55
|
5 |
|
if (count($templates) == 0) { |
|
56
|
|
|
unset($sections[$index]); |
|
57
|
|
|
} |
|
58
|
5 |
|
}); |
|
59
|
|
|
|
|
60
|
5 |
|
return $sections; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Finds all plugins with a Template directory. |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
5 |
|
protected static function pluginsWithTemplates() |
|
69
|
|
|
{ |
|
70
|
5 |
|
$plugins = Plugin::loaded(); |
|
71
|
|
|
|
|
72
|
|
|
array_walk($plugins, function ($plugin, $index) use (&$plugins) { |
|
|
|
|
|
|
73
|
5 |
|
$paths = App::path('Template', $plugin); |
|
74
|
|
|
|
|
75
|
|
|
array_walk($paths, function ($path, $index) use (&$paths) { |
|
76
|
5 |
|
if (!is_dir($path)) { |
|
77
|
5 |
|
unset($paths[$index]); |
|
78
|
|
|
} |
|
79
|
5 |
|
}); |
|
80
|
5 |
|
}); |
|
81
|
|
|
|
|
82
|
5 |
|
return $plugins; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Return all templates for a given plugin. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $plugin The plugin to find all templates for. |
|
89
|
|
|
* |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
7 |
|
public static function plugin($plugin) |
|
93
|
|
|
{ |
|
94
|
7 |
|
$templates = []; |
|
95
|
|
|
|
|
96
|
7 |
|
foreach (App::path('Template', $plugin) as $path) { |
|
97
|
6 |
|
$templates = array_merge($templates, static::iteratePath($path)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
6 |
|
return $templates; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Iterage over the given path and return all matching .tpl files in it. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $path Path to iterate over. |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
8 |
|
protected static function iteratePath($path) |
|
111
|
|
|
{ |
|
112
|
8 |
|
return static::walkIterator(static::setupIterator($path)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Setup iterator for given path. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $path Path to setup iterator for. |
|
119
|
|
|
* |
|
120
|
|
|
* @return \Iterator |
|
121
|
|
|
*/ |
|
122
|
8 |
|
protected static function setupIterator($path) |
|
123
|
|
|
{ |
|
124
|
8 |
|
return new \RegexIterator(new \RecursiveIteratorIterator( |
|
125
|
8 |
|
new \RecursiveDirectoryIterator( |
|
126
|
8 |
|
$path, |
|
127
|
|
|
\FilesystemIterator::KEY_AS_PATHNAME | |
|
128
|
|
|
\FilesystemIterator::CURRENT_AS_FILEINFO | |
|
129
|
8 |
|
\FilesystemIterator::SKIP_DOTS |
|
130
|
|
|
), |
|
131
|
8 |
|
\RecursiveIteratorIterator::CHILD_FIRST, |
|
132
|
8 |
|
\RecursiveIteratorIterator::CATCH_GET_CHILD |
|
133
|
8 |
|
), '/.*?' . TwigView::EXT . '$/', \RegexIterator::GET_MATCH); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Walk over the iterator and compile all templates. |
|
138
|
|
|
* |
|
139
|
|
|
* @param \Iterator $iterator Iterator to walk. |
|
140
|
|
|
* |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
// @codingStandardsIgnoreStart |
|
144
|
8 |
|
protected static function walkIterator(\Iterator $iterator) |
|
145
|
|
|
{ |
|
146
|
8 |
|
$items = []; |
|
147
|
|
|
|
|
148
|
8 |
|
$array = iterator_to_array($iterator); |
|
149
|
8 |
|
uasort($array, function ($a, $b) { |
|
|
|
|
|
|
150
|
8 |
|
if ($a == $b) { |
|
151
|
|
|
return 0; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
8 |
|
return ($a < $b) ? -1 : 1; |
|
155
|
8 |
|
}); |
|
156
|
|
|
|
|
157
|
8 |
|
foreach ($array as $paths) { |
|
158
|
8 |
|
foreach ($paths as $path) { |
|
159
|
8 |
|
$items[] = $path; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
8 |
|
return $items; |
|
164
|
|
|
} |
|
165
|
|
|
// @codingStandardsIgnoreEnd |
|
166
|
|
|
} |
|
167
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.