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
|
|
|
} |
46
|
28 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function addSpace($name, $path) |
52
|
|
|
{ |
53
|
|
|
$this->spacePaths[$name] = $this->app->basePath().'/'.$path; |
54
|
|
|
} |
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
|
3 |
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
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
|
|
|
} 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($dir); |
171
|
|
|
|
172
|
12 |
|
$addons[$addon->name()] = $addon; |
173
|
|
|
} |
174
|
|
|
} |
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
|
|
|
} |
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
|
|
|
/** |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
2 |
|
public function addonConsoleCommands() |
203
|
|
|
{ |
204
|
2 |
|
$commands = []; |
205
|
|
|
|
206
|
2 |
|
foreach ($this->addons() as $addon) { |
207
|
|
|
$commands = array_merge($commands, $addon->config('addon.console.commands', [])); |
208
|
|
|
} |
209
|
|
|
|
210
|
2 |
|
return $commands; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
1 |
View Code Duplication |
public function addonHttpMiddlewares() |
|
|
|
|
217
|
|
|
{ |
218
|
1 |
|
$middlewares = []; |
219
|
|
|
|
220
|
1 |
|
foreach ($this->addons() as $addon) { |
221
|
|
|
$middlewares = array_merge($middlewares, $addon->config('addon.http.middlewares', [])); |
222
|
|
|
} |
223
|
|
|
|
224
|
1 |
|
return $middlewares; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
1 |
View Code Duplication |
public function addonRouteMiddlewares() |
|
|
|
|
231
|
|
|
{ |
232
|
1 |
|
$middlewares = []; |
233
|
|
|
|
234
|
1 |
|
foreach ($this->addons() as $addon) { |
235
|
|
|
$middlewares = array_merge($middlewares, $addon->config('addon.http.route_middlewares', [])); |
236
|
|
|
} |
237
|
|
|
|
238
|
1 |
|
return $middlewares; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
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.