1 | <?php |
||
8 | class Apps |
||
9 | { |
||
10 | /** |
||
11 | * The container instance. |
||
12 | * |
||
13 | * @var \Illuminate\Contracts\Container\Container |
||
14 | */ |
||
15 | protected $container; |
||
16 | |||
17 | /** |
||
18 | * The current application identifier. |
||
19 | * |
||
20 | * @var string|false |
||
21 | */ |
||
22 | protected $id = false; |
||
23 | |||
24 | /** |
||
25 | * Create a new Apps instance. |
||
26 | * |
||
27 | * @param \Illuminate\Contracts\Container\Container $container |
||
28 | */ |
||
29 | 8 | public function __construct(Container $container) |
|
37 | |||
38 | /** |
||
39 | * Get or check the current application identifier. |
||
40 | * |
||
41 | * @return string|bool |
||
42 | */ |
||
43 | 3 | public function id() |
|
44 | { |
||
45 | 3 | if ($this->id === false) { |
|
46 | 3 | $this->id = $this->idForUrl($this->container['request']->getUri()); |
|
47 | } |
||
48 | |||
49 | 3 | if (func_num_args() > 0) { |
|
50 | 1 | return in_array($this->id, is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args()); |
|
51 | } |
||
52 | |||
53 | 2 | return $this->id; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * Refresh the current application identifier. |
||
58 | * |
||
59 | * @return $this |
||
60 | */ |
||
61 | 2 | public function refreshId() |
|
67 | |||
68 | /** |
||
69 | * Get application identifier for the given URL. |
||
70 | * |
||
71 | * @param string $url |
||
72 | * @return string |
||
73 | */ |
||
74 | 4 | public function idForUrl($url) |
|
75 | { |
||
76 | 4 | return Collection::make($this->container['config']['apps.url']) |
|
77 | 4 | ->filter(function ($root) use ($url) { |
|
78 | 4 | return $this->urlHasRoot($url, $root); |
|
79 | 4 | }) |
|
80 | 4 | ->sortByDesc(function ($root) { |
|
81 | 4 | return strlen($root); |
|
82 | 4 | }) |
|
83 | 4 | ->keys() |
|
84 | 4 | ->first(); |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Determine if an URL has the given root URL. |
||
89 | * |
||
90 | * @param string $url |
||
91 | * @param string $root |
||
92 | * @param bool $strict |
||
93 | * @return bool |
||
94 | */ |
||
95 | 4 | protected function urlHasRoot($url, $root, $strict = false) |
|
104 | |||
105 | /** |
||
106 | * Remove scheme for an URL. |
||
107 | * |
||
108 | * @param string $url |
||
109 | * @return string |
||
110 | */ |
||
111 | 4 | protected function removeSchemeForUrl($url) |
|
115 | |||
116 | /** |
||
117 | * Get the root URL for the given application identifier. |
||
118 | * |
||
119 | * @param string $appId |
||
120 | * @return string |
||
121 | */ |
||
122 | 1 | public function rootUrl($appId = '') |
|
128 | |||
129 | /** |
||
130 | * Generate an absolute URL to the given path. |
||
131 | * |
||
132 | * @param string $path |
||
133 | * @param mixed $query |
||
134 | * @param mixed $appId |
||
135 | * @return string |
||
136 | */ |
||
137 | 1 | public function url($path = '', $query = [], $appId = '') |
|
158 | } |
||
159 |