|
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
|
29 |
|
public function __construct(Application $app) |
|
29
|
|
|
{ |
|
30
|
29 |
|
$this->app = $app; |
|
31
|
|
|
|
|
32
|
29 |
|
$this->makeAddonsPaths(); |
|
33
|
29 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
29 |
|
private function makeAddonsPaths() |
|
39
|
|
|
{ |
|
40
|
29 |
|
$addonsDirectoryPath = $this->path(); |
|
41
|
|
|
|
|
42
|
29 |
|
$this->spacePaths[''] = $addonsDirectoryPath; |
|
43
|
29 |
|
foreach ($this->app['config']->get('addon.additional_paths', []) as $name => $path) { |
|
44
|
|
|
$this->addSpace($name, $path); |
|
45
|
|
|
} |
|
46
|
29 |
|
} |
|
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
|
29 |
|
public function path($name = null) |
|
62
|
|
|
{ |
|
63
|
29 |
|
if ($name !== null) { |
|
64
|
|
|
return $this->path().'/'.$name; |
|
65
|
|
|
} else { |
|
66
|
29 |
|
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
|
13 |
|
public function loadAddons() |
|
151
|
|
|
{ |
|
152
|
13 |
|
$files = $this->app['files']; |
|
153
|
13 |
|
$ignore_pattern = $this->app['config']->get('addon.ignore_pattern', '/^@/'); |
|
154
|
13 |
|
$name_pattern = $this->app['config']->get('addon.name_pattern', '/^(.+)$/'); |
|
155
|
|
|
|
|
156
|
13 |
|
$addons = []; |
|
157
|
|
|
|
|
158
|
13 |
|
foreach ($this->spacePaths as $path) { |
|
159
|
|
|
// make directory |
|
160
|
13 |
|
if (!$files->exists($path)) { |
|
161
|
|
|
$files->makeDirectory($path); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// load addons |
|
165
|
13 |
|
foreach ($files->directories($path) as $dir) { |
|
166
|
|
|
// test ignore pattern |
|
167
|
7 |
|
if (preg_match($ignore_pattern, basename($dir))) { |
|
168
|
|
|
continue; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// test name pattern |
|
172
|
7 |
|
if (! preg_match($name_pattern, basename($dir), $matches)) { |
|
173
|
|
|
continue; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
7 |
|
$name = count($matches) > 2 ? $matches[1] : $matches[0]; |
|
177
|
|
|
|
|
178
|
7 |
|
$addon = Addon::create($this->app, $name, $dir); |
|
179
|
|
|
|
|
180
|
13 |
|
$addons[$addon->name()] = $addon; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
13 |
|
return $addons; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
13 |
|
public function addons() |
|
191
|
|
|
{ |
|
192
|
13 |
|
if ($this->addons === null) { |
|
193
|
13 |
|
$this->addons = $this->loadAddons(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
13 |
|
return $this->addons; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return \Jumilla\Addomnipot\Laravel\Addons\Addon |
|
201
|
|
|
*/ |
|
202
|
8 |
|
public function addon($name) |
|
203
|
|
|
{ |
|
204
|
8 |
|
return array_get($this->addons(), $name ?: '', null); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|