Completed
Pull Request — master (#44)
by Cees-Jan
20:58 queued 19:07
created

Scanner::setupIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.0156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
ccs 6
cts 8
cp 0.75
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
crap 1.0156
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 4
    public static function all()
22
    {
23
        // @codingStandardsIgnoreEnd
24 4
        $sections = [];
25
26 3
        foreach (App::path('Template') as $path) {
27
            if (is_dir($path)) {
28 3
                $sections['APP'] = isset($sections['APP']) ? $sections['APP'] : [];
29
                $sections['APP'] = array_merge($sections['APP'], static::iteratePath($path));
30
            }
31 1
        }
32
33 4
        foreach (static::pluginsWithTemplates() as $plugin) {
34
            foreach (App::path('Template', $plugin) as $path) {
35
                if (is_dir($path)) {
36
                    $sections[$plugin] = isset($sections[$plugin]) ? $sections[$plugin] : [];
37
                    $sections[$plugin] = array_merge($sections[$plugin], static::iteratePath($path));
38
                }
39
            }
40
        }
41
42
        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 4
    protected static function clearEmptySections(array $sections)
53
    {
54
        array_walk($sections, function ($templates, $index) use (&$sections) {
55
            if (count($templates) == 0) {
56
                unset($sections[$index]);
57
            }
58
        });
59
60 4
        return $sections;
61 4
    }
62
63
    /**
64
     * Finds all plugins with a Template directory.
65
     *
66
     * @return array
67
     */
68 4
    protected static function pluginsWithTemplates()
69
    {
70
        $plugins = Plugin::loaded();
71
72
        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...
73
            $paths = App::path('Template', $plugin);
74
75
            array_walk($paths, function ($path, $index) use (&$paths) {
76
                if (!is_dir($path)) {
77 4
                    unset($paths[$index]);
78
                }
79
            });
80
        });
81
82 4
        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 4
    public static function plugin($plugin)
93
    {
94 4
        $templates = [];
95
96 4
        foreach (App::path('Template', $plugin) as $path) {
97
            $templates = array_merge($templates, static::iteratePath($path));
98
        }
99
100 3
        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
    protected static function iteratePath($path)
111
    {
112
        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 3
    protected static function setupIterator($path)
123
    {
124
        return new \RegexIterator(new \RecursiveIteratorIterator(
125
            new \RecursiveDirectoryIterator(
126
                $path,
127 3
                \FilesystemIterator::KEY_AS_PATHNAME |
128 3
                \FilesystemIterator::CURRENT_AS_FILEINFO |
129 3
                \FilesystemIterator::SKIP_DOTS
130
            ),
131 3
            \RecursiveIteratorIterator::CHILD_FIRST,
132 3
            \RecursiveIteratorIterator::CATCH_GET_CHILD
133
        ), '/.*?' . 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 3
    protected static function walkIterator(\Iterator $iterator)
145
    {
146 3
        $items = [];
147
148
        $array = iterator_to_array($iterator);
149
        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...
150
            if ($a == $b) {
151
                return 0;
152
            }
153
154
            return ($a < $b) ? -1 : 1;
155
        });
156
157
        foreach ($array as $paths) {
158
            foreach ($paths as $path) {
159
                $items[] = $path;
160
            }
161
        }
162
163 3
        return $items;
164 3
    }
165
    // @codingStandardsIgnoreEnd
166
}
167