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 | 28 | public function __construct(Application $app) |
|
34 | |||
35 | /** |
||
36 | * @return void |
||
37 | */ |
||
38 | 28 | private function makeAddonsPaths() |
|
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) |
|
69 | |||
70 | /** |
||
71 | * @param string $space |
||
72 | * @param string $name |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | 3 | public function spacePath($space, $name = null) |
|
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) |
|
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 | 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) |
|
198 | } |
||
199 |