-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAppManager.php
313 lines (280 loc) · 7.33 KB
/
AppManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
namespace ElfSundae\Apps;
use Closure;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
class AppManager
{
use Macroable;
/**
* The container instance.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
/**
* The current application identifier.
*
* @var string
*/
protected $appId;
/**
* Create a new app manager instance.
*
* @param \Illuminate\Contracts\Container\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
$this->container->rebinding('request', function () {
$this->refreshId();
});
}
/**
* Get all application URLs.
*
* @return array
*/
public function urls()
{
return $this->container['config']->get('apps.url', []);
}
/**
* Get all application identifiers.
*
* @return array
*/
public function ids()
{
return array_keys($this->urls());
}
/**
* Get the root URL for the application identifier.
*
* @param string $app
* @return string
*/
public function root($app = '')
{
return Arr::get($this->urls(), (string) $app)
?: $this->container['config']['app.url'];
}
/**
* Get the URL domain for the application identifier.
*
* @param string $app
* @return string
*/
public function domain($app = '')
{
return parse_url($this->root($app), PHP_URL_HOST);
}
/**
* Get the URL prefix for the application identifier.
*
* @param string $app
* @return string
*/
public function prefix($app = '')
{
return trim(parse_url($this->root($app), PHP_URL_PATH), '/');
}
/**
* Get or check the current application identifier.
*
* @return string|bool
*/
public function id()
{
if (is_null($this->appId)) {
$this->appId = (string) $this->idForUrl($this->container['request']->getUri());
}
if (func_num_args() > 0) {
return in_array($this->appId, is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args());
}
return $this->appId;
}
/**
* Get the application identifier for the given URL.
*
* @param string $url
* @return string|null
*/
public function idForUrl($url)
{
return collect($this->urls())
->filter(function ($root) use ($url) {
return $this->urlHasRoot($url, $root);
})
->sortByDesc(function ($root) {
return strlen($root);
})
->keys()
->first();
}
/**
* Refresh the current application identifier.
*
* @return $this
*/
public function refreshId()
{
$this->appId = null;
return $this;
}
/**
* Determine if a URL has the given root URL.
*
* @param string $url
* @param string $root
* @param bool $strict
* @return bool
*/
protected function urlHasRoot($url, $root, $strict = false)
{
if (! $strict) {
$url = $this->removeScheme($url);
$root = $this->removeScheme($root);
}
return (bool) preg_match('~^'.preg_quote($root, '~').'([/\?#].*)?$~i', $url);
}
/**
* Remove scheme for a URL.
*
* @param string $url
* @return string
*/
protected function removeScheme($url)
{
return preg_replace('#^https?://#i', '', $url);
}
/**
* Generate an absolute URL to a path for the given application identifier.
*
* @param string $app
* @param string $path
* @param mixed $parameters
* @return string
*/
public function url($app = '', $path = '', $parameters = [])
{
return $this->root($app).$this->stringAfter(
$this->container['url']->to($path, $parameters),
$this->container['url']->to('')
);
}
/**
* Generate the URL to an application asset.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
public function asset($path, $secure = null)
{
return $this->container['url']->assetFrom($this->root('assets'), $path, $secure);
}
/**
* Return the remainder of a string after a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
protected function stringAfter($subject, $search)
{
return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
}
/**
* Register routes for each application.
*
* You may call this method in the `map` method of your `RouteServiceProvider`.
*
* @param array|\Closure $attributes
* @return void
*/
public function routes($attributes = [])
{
if (! $attributes instanceof Closure) {
$attr = $attributes;
$attributes = function ($id) use ($attr) {
return Arr::get($attr, $id, []);
};
}
foreach ($this->ids() as $id) {
if (file_exists($file = $this->getRouteFile($id))) {
$this->container['router']->group(
$this->getRouteAttributes($id, $attributes),
$this->getRouteFileLoader($file)
);
}
}
}
/**
* Get the route file for the application.
*
* @param string $app
* @return string
*/
protected function getRouteFile($app)
{
return base_path("routes/{$app}.php");
}
/**
* Get the route file loader.
*
* @param string $file
* @return \Closure
*/
protected function getRouteFileLoader($file)
{
return function ($router) use ($file) {
require $file;
};
}
/**
* Get the route attributes for the application.
*
* @param string $app
* @param array|\Closure $attributes
* @return array
*/
protected function getRouteAttributes($app, $attributes = [])
{
if ($attributes instanceof Closure) {
$attributes = $attributes($app, $this) ?: [];
}
return array_filter(array_merge(
$this->getDefaultRouteAttributes($app), $attributes
));
}
/**
* Get the default route attributes for the application.
*
* @param string $app
* @return array
*/
protected function getDefaultRouteAttributes($app)
{
return [
'domain' => $this->domain($app),
'prefix' => $this->prefix($app),
'middleware' => $this->container['router']->hasMiddlewareGroup($app) ? $app : 'web',
'namespace' => $this->getRootControllerNamespace($app),
];
}
/**
* Get the root controller namespace for the application.
*
* @param string $app
* @return string
*/
protected function getRootControllerNamespace($app)
{
$namespace = $this->container['url']->getRootControllerNamespace()
?: 'App\Http\Controllers';
return trim($namespace.'\\'.Str::studly($app), '\\');
}
}