1 | <?php |
||
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 | 21 | public function __construct(Application $app) |
|
29 | { |
||
30 | 21 | $this->app = $app; |
|
31 | |||
32 | 21 | $this->makeAddonsPaths(); |
|
33 | 21 | } |
|
34 | |||
35 | /** |
||
36 | * @return void |
||
37 | */ |
||
38 | 21 | private function makeAddonsPaths() |
|
39 | { |
||
40 | 21 | $addonsDirectoryPath = $this->path(); |
|
41 | |||
42 | 21 | $this->spacePaths[''] = $addonsDirectoryPath; |
|
43 | 21 | foreach ($this->app['config']->get('addon.additional_paths', []) as $name => $path) { |
|
44 | $this->addSpace($name, $path); |
||
45 | } |
||
46 | 21 | } |
|
47 | |||
48 | /** |
||
49 | * @return void |
||
50 | */ |
||
51 | public function addSpace($name, $path) |
||
55 | |||
56 | /** |
||
57 | * @param string $name |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 21 | public function path($name = null) |
|
69 | |||
70 | /** |
||
71 | * @param string $space |
||
72 | * @param string $name |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | public function spacePath($space, $name = null) |
||
77 | { |
||
78 | $path = $space ? array_get($this->spacePaths, $space) : $this->path(); |
||
79 | |||
80 | if ($path === null) { |
||
81 | throw new UnexpectedValueException("addon space '{$space}' is not found."); |
||
82 | } |
||
83 | |||
84 | if ($name !== null) { |
||
85 | return $path.'/'.$name; |
||
86 | } else { |
||
87 | return $path; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param string $name |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function exists($name) |
||
97 | { |
||
98 | if ($this->existsOnSpace(null, $name)) { |
||
99 | return true; |
||
100 | } |
||
101 | |||
102 | foreach ($this->spacePaths as $space => $path) { |
||
103 | if ($this->existsOnSpace($space, $name)) { |
||
104 | return true; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | return false; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param string $name |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function existsOnSpace($space, $name) |
||
117 | { |
||
118 | 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) |
|
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 | 10 | public function loadAddons() |
|
151 | { |
||
152 | 10 | $files = $this->app['files']; |
|
153 | 10 | $ignore_pattern = $this->app['config']->get('addon.ignore_pattern', '/^@/'); |
|
154 | 10 | $name_pattern = $this->app['config']->get('addon.name_pattern', '/^(.+)$/'); |
|
155 | |||
156 | 10 | $addons = []; |
|
157 | |||
158 | 10 | foreach ($this->spacePaths as $path) { |
|
159 | // make directory |
||
160 | 10 | if (!$files->exists($path)) { |
|
161 | $files->makeDirectory($path); |
||
162 | } |
||
163 | |||
164 | // load addons |
||
165 | 10 | foreach ($files->directories($path) as $dir) { |
|
166 | // test ignore pattern |
||
167 | 4 | if (preg_match($ignore_pattern, basename($dir))) { |
|
168 | continue; |
||
169 | } |
||
170 | |||
171 | // test name pattern |
||
172 | 4 | if (! preg_match($name_pattern, basename($dir), $matches)) { |
|
173 | continue; |
||
174 | } |
||
175 | |||
176 | 4 | $name = count($matches) >= 2 ? $matches[1] : $matches[0]; |
|
177 | |||
178 | 4 | $addon = Addon::create($this->app, $name, $dir); |
|
179 | |||
180 | 10 | $addons[$addon->name()] = $addon; |
|
181 | } |
||
182 | } |
||
183 | |||
184 | 10 | return $addons; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return array |
||
189 | */ |
||
190 | 10 | public function addons() |
|
198 | |||
199 | /** |
||
200 | * @return \Jumilla\Addomnipot\Laravel\Addons\Addon |
||
201 | */ |
||
202 | 5 | public function addon($name) |
|
206 | } |
||
207 |