Completed
Push — master ( d5222d...8300db )
by Fumio
02:37
created

Environment::addonConsoleCommands()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
    {
210 1
        $middlewares = [];
211
212 1
        foreach ($this->addons() as $addon) {
213
            $middlewares = array_merge($middlewares, $addon->config('addon.http.middlewares', []));
214 1
        }
215
216 1
        return $middlewares;
217
    }
218
219
    /**
220
     * @return array
221
     */
222 1 View Code Duplication
    public function addonRouteMiddlewares()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
    {
224 1
        $middlewares = [];
225
226 1
        foreach ($this->addons() as $addon) {
227
            $middlewares = array_merge($middlewares, $addon->config('addon.http.route_middlewares', []));
228 1
        }
229
230 1
        return $middlewares;
231
    }
232
}
233