Completed
Push — master ( 78b3b4...1185ef )
by Fumio
02:21
created

Environment::makeAddonsPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Jumilla\Addomnipot\Laravel;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use UnexpectedValueException;
7
8
class Environment
9
{
10
    /**
11
     * @return array
12
     */
13
    protected $addons = null;
14
15
    /**
16
     * @return array
17
     */
18
    protected $spacePaths = [];
19
20
    /**
21
     * @var \Illuminate\Contracts\Foundation\Application
22
     */
23
    protected $app;
24
25
    /**
26
     * @param \Illuminate\Contracts\Foundation\Application $app
27
     */
28 28
    public function __construct(Application $app)
29
    {
30 28
        $this->app = $app;
31
32 28
        $this->makeAddonsPaths();
33 28
    }
34
35
    /**
36
     * @return void
37
     */
38 28
    private function makeAddonsPaths()
39
    {
40 28
        $addonsDirectoryPath = $this->path();
41
42 28
        $this->spacePaths[''] = $addonsDirectoryPath;
43 28
        foreach ($this->app['config']->get('addon.additional_paths', []) as $name => $path) {
44
            $this->addSpace($name, $path);
45 28
        }
46 28
    }
47
48
    /**
49
     * @return void
50
     */
51 28
    public function addSpace($name, $path)
52
    {
53
        $this->spacePaths[$name] = $this->app->basePath().'/'.$path;
54 28
    }
55
56
    /**
57
     * @param string $name
58
     *
59
     * @return string
60
     */
61 28
    public function path($name = null)
62
    {
63 28
        if ($name !== null) {
64
            return $this->path().'/'.$name;
65
        } else {
66 28
            return $this->app->basePath().'/'.$this->app['config']->get('addon.path', 'addons');
67
        }
68
    }
69
70
    /**
71
     * @param string $space
72
     * @param string $name
73
     *
74
     * @return string
75
     */
76 3
    public function spacePath($space, $name = null)
77
    {
78 3
        $path = $space ? array_get($this->spacePaths, $space) : $this->path();
79
80 3
        if ($path === null) {
81
            throw new UnexpectedValueException("addon space '{$space}' is not found.");
82
        }
83
84 3
        if ($name !== null) {
85 3
            return $path.'/'.$name;
86
        } else {
87
            return $path;
88
        }
89
    }
90
91
    /**
92
     * @param string $name
93
     *
94
     * @return bool
95
     */
96 3
    public function exists($name)
97
    {
98 3
        if ($this->existsOnSpace(null, $name)) {
99
            return true;
100
        }
101
102 3
        foreach ($this->spacePaths as $space => $path) {
103 3
            if ($this->existsOnSpace($space, $name)) {
104
                return true;
105
            }
106 3
        }
107
108 3
        return false;
109
    }
110
111
    /**
112
     * @param string $name
113
     *
114
     * @return bool
115
     */
116 3
    public function existsOnSpace($space, $name)
117
    {
118 3
        return is_dir($this->spacePath($space, $name));
119
    }
120
121
    /**
122
     * @param string $relativeClassName
123
     *
124
     * @return string
125
     */
126 2
    public function classToPath($relativeClassName)
127
    {
128 2
        return str_replace('\\', '/', $relativeClassName).'.php';
129
    }
130
131
    /**
132
     * @param string $relativePath
133
     *
134
     * @return mixed
135
     */
136 1
    public function pathToClass($relativePath)
137
    {
138 1
        if (strpos($relativePath, '/') !== false) {
139 1
            $relativePath = dirname($relativePath).'/'.basename($relativePath, '.php');
140 1
        } else {
141 1
            $relativePath = basename($relativePath, '.php');
142
        }
143
144 1
        return str_replace('/', '\\', $relativePath);
145
    }
146
147
    /**
148
     * @return array
149
     */
150 12
    public function loadAddons()
151
    {
152 12
        $files = $this->app['files'];
153 12
        $ignore_pattern = $this->app['config']->get('addon.ignore_pattern', '/^@/');
154
155 12
        $addons = [];
156
157 12
        foreach ($this->spacePaths as $path) {
158
            // make directory
159 12
            if (!$files->exists($path)) {
160
                $files->makeDirectory($path);
161
            }
162
163
            // load addons
164 12
            foreach ($files->directories($path) as $dir) {
165
                // test ignore pattern
166 7
                if (preg_match($ignore_pattern, basename($dir))) {
167
                    continue;
168
                }
169
170 7
                $addon = Addon::create($this->app, $dir);
171
172 7
                $addons[$addon->name()] = $addon;
173 12
            }
174 12
        }
175
176 12
        return $addons;
177
    }
178
179
    /**
180
     * @return array
181
     */
182 12
    public function addons()
183
    {
184 12
        if ($this->addons === null) {
185 12
            $this->addons = $this->loadAddons();
186 12
        }
187
188 12
        return $this->addons;
189
    }
190
191
    /**
192
     * @return \Jumilla\Addomnipot\Laravel\Addons\Addon
193
     */
194 8
    public function addon($name)
195
    {
196 8
        return array_get($this->addons(), $name ?: '', null);
197
    }
198
}
199