Completed
Pull Request — master (#58)
by
unknown
05:54
created

Scanner::clearEmptySections()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
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 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)
69
    {
70 4
        array_walk($sections, function ($templates, $index) use (&$sections) {
71 4
            if (count($templates) == 0) {
72 4
                unset($sections[$index]);
73
            }
74 4
        });
75
76 4
        return $sections;
77
    }
78
79
    /**
80
     * Finds all plugins with a Template directory.
81
     *
82
     * @return array
83
     */
84 4
    protected static function pluginsWithTemplates()
85
    {
86 4
        $plugins = Plugin::loaded();
87
88 4
        array_walk($plugins, function ($plugin, $index) use (&$plugins) {
0 ignored issues
show
Unused Code introduced by
The parameter $index is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89 4
            $paths = App::path('Template', $plugin);
90
91 4
            array_walk($paths, function ($path, $index) use (&$paths) {
92 4
                if (!is_dir($path)) {
93 4
                    unset($paths[$index]);
94
                }
95 4
            });
96 4
        });
97
98 4
        return $plugins;
99
    }
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)
109
    {
110 7
        return static::walkIterator(static::setupIterator($path));
111
    }
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)
142
    {
143 7
        $items = [];
144
145 7
        $array = iterator_to_array($iterator);
146 7
        uasort($array, function ($a, $b) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
147 6
            if ($a == $b) {
148
                return 0;
149
            }
150
151 6
            return ($a < $b) ? -1 : 1;
152 7
        });
153
154
        foreach ($array as $paths) {
155
            foreach ($paths as $path) {
156
                $items[] = $path;
157
            }
158
        }
159
160
        return $items;
161
    }
162
}
163