1 | <?php declare(strict_types=1); |
||
13 | class Scanner |
||
14 | { |
||
15 | /** |
||
16 | * Return all sections (app & plugins) with an Template directory. |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | 4 | public static function all() |
|
21 | { |
||
22 | 4 | $sections = []; |
|
23 | |||
24 | 4 | foreach (App::path('Template') as $path) { |
|
25 | 3 | if (is_dir($path)) { |
|
26 | 3 | $sections['APP'] = isset($sections['APP']) ? $sections['APP'] : []; |
|
27 | 3 | $sections['APP'] = array_merge($sections['APP'], static::iteratePath($path)); |
|
28 | } |
||
29 | } |
||
30 | |||
31 | 4 | foreach (static::pluginsWithTemplates() as $plugin) { |
|
32 | 4 | foreach (App::path('Template', $plugin) as $path) { |
|
33 | 4 | if (is_dir($path)) { |
|
34 | 4 | $sections[$plugin] = isset($sections[$plugin]) ? $sections[$plugin] : []; |
|
35 | 4 | $sections[$plugin] = array_merge($sections[$plugin], static::iteratePath($path)); |
|
36 | } |
||
37 | } |
||
38 | } |
||
39 | |||
40 | 4 | return static::clearEmptySections($sections); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Return all templates for a given plugin. |
||
45 | * |
||
46 | * @param string $plugin The plugin to find all templates for. |
||
47 | * |
||
48 | * @return mixed |
||
49 | */ |
||
50 | 4 | public static function plugin($plugin) |
|
51 | { |
||
52 | 4 | $templates = []; |
|
53 | |||
54 | 4 | foreach (App::path('Template', $plugin) as $path) { |
|
55 | 3 | $templates = array_merge($templates, static::iteratePath($path)); |
|
56 | } |
||
57 | |||
58 | 3 | return $templates; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Check sections a remove the ones without anything in them. |
||
63 | * |
||
64 | * @param array $sections Sections to check. |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | protected static function clearEmptySections(array $sections) |
||
78 | |||
79 | /** |
||
80 | * Finds all plugins with a Template directory. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | 4 | protected static function pluginsWithTemplates() |
|
100 | |||
101 | /** |
||
102 | * Iterage over the given path and return all matching .tpl files in it. |
||
103 | * |
||
104 | * @param string $path Path to iterate over. |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | 7 | protected static function iteratePath($path) |
|
112 | |||
113 | /** |
||
114 | * Setup iterator for given path. |
||
115 | * |
||
116 | * @param string $path Path to setup iterator for. |
||
117 | * |
||
118 | * @return \Iterator |
||
119 | */ |
||
120 | 7 | protected static function setupIterator($path) |
|
121 | { |
||
122 | 7 | return new \RegexIterator(new \RecursiveIteratorIterator( |
|
123 | 7 | new \RecursiveDirectoryIterator( |
|
124 | 7 | $path, |
|
125 | 7 | \FilesystemIterator::KEY_AS_PATHNAME | |
|
126 | 7 | \FilesystemIterator::CURRENT_AS_FILEINFO | |
|
127 | 7 | \FilesystemIterator::SKIP_DOTS |
|
128 | ), |
||
129 | 7 | \RecursiveIteratorIterator::CHILD_FIRST, |
|
130 | 7 | \RecursiveIteratorIterator::CATCH_GET_CHILD |
|
131 | 7 | ), '/.*?' . TwigView::EXT . '$/', \RegexIterator::GET_MATCH); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Walk over the iterator and compile all templates. |
||
136 | * |
||
137 | * @param \Iterator $iterator Iterator to walk. |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | 7 | protected static function walkIterator(\Iterator $iterator) |
|
162 | } |
||
163 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.