BundleMap::getPathKeys()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelLangBundler\Bundle;
4
5
use Illuminate\Support\Collection;
6
7
class BundleMap
8
{
9
    /**
10
     * Array containing mapped lang bundles.
11
     *
12
     * @var array
13
     */
14
    protected $bundleMap = [];
15
16
    /**
17
     * Array of auto-aliased file names.
18
     *
19
     * @var array
20
     */
21
    protected $autoAliases = [];
22
23
    /**
24
     * Return bundle map.
25
     *
26
     * @return array
27
     */
28
    public function getBundleMap()
29
    {
30
        return $this->bundleMap;
31
    }
32
33
    /**
34
     * Get auto-alias array.
35
     *
36
     * @return array
37
     */
38
    public function getAutoAliases()
39
    {
40
        return $this->autoAliases;
41
    }
42
43
    /**
44
     * Return true if bundleMap is empty.
45
     *
46
     * @return bool
47
     */
48
    public function bundleMapIsEmpty()
49
    {
50
        return empty($this->bundleMap);
51
    }
52
53
    /**
54
     * Get trans values for path keys.
55
     *
56
     * @param array $pathKeys
57
     *
58
     * @return Collection
59
     */
60
    public function getBundleValues(array $pathKeys)
61
    {
62
        $this->mapBundles();
63
64
        $temp = &$this->bundleMap;
65
66
        foreach ($pathKeys as $key) {
67
            $temp = &$temp[$key];
68
        }
69
70
        return collect($temp);
71
    }
72
73
    /**
74
     * Get all the lang bundle files from the bundles directory.
75
     *
76
     * @return array
77
     */
78
    public function mapBundles()
79
    {
80
        if (!empty($this->bundleMap)) {
81
            return $this->getBundleMap();
82
        }
83
84
        $pathCollection = $this->getPathCollection();
85
86
        foreach ($pathCollection as $path) {
87
            $content = include $path;
88
89
            $pathKeys = $this->getPathKeys($path);
90
91
            $this->registerAlias(collect($pathKeys));
92
93
            $this->mapContent($content, $pathKeys);
94
        }
95
96
        return $this->getBundleMap();
97
    }
98
99
    /**
100
     * Get paths to all bundle files.
101
     *
102
     * @return Collection
103
     */
104
    protected function getPathCollection()
105
    {
106
        $bundlePath = resource_path('lang/bundles');
107
108
        if (!file_exists($bundlePath)) {
109
            return collect([]);
110
        }
111
112
        $directory = new \RecursiveDirectoryIterator($bundlePath);
113
114
        $iterator = new \RecursiveIteratorIterator($directory);
115
116
        $paths = new \RegexIterator(
117
            $iterator,
118
            '/^.+\.php$/i',
119
            \RecursiveRegexIterator::GET_MATCH
120
        );
121
122
        return collect(iterator_to_array($paths))->flatten();
123
    }
124
125
    /**
126
     * Get array of keys describing file path.
127
     *
128
     * @param string $path
129
     *
130
     * @return array
131
     */
132
    protected function getPathKeys($path)
133
    {
134
        $key = str_replace('.php', '', explode(DIRECTORY_SEPARATOR.'bundles'.DIRECTORY_SEPARATOR, $path)[1]);
135
136
        return explode('/', $key);
137
    }
138
139
    /**
140
     * Register file paths as auto aliases.
141
     *
142
     * @param Collection $pathKeys
143
     */
144
    protected function registerAlias(Collection $pathKeys)
145
    {
146
        $className = $pathKeys->last();
147
148
        $path = $pathKeys->implode('.');
149
150
        $this->autoAliases[$path] = $className;
151
    }
152
153
    /**
154
     * Map content on bundleMap.
155
     *
156
     * @param array $content
157
     * @param array $pathKeys
158
     */
159
    protected function mapContent(array $content, array $pathKeys)
160
    {
161
        $temp = &$this->bundleMap;
162
163
        foreach ($pathKeys as $key) {
164
            $temp = &$temp[$key];
165
        }
166
167
        $temp = $content;
168
    }
169
}
170