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