1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Apps; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Contracts\Container\Container; |
7
|
|
|
|
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
|
7 |
|
public function __construct(Container $container) |
30
|
|
|
{ |
31
|
7 |
|
$this->container = $container; |
32
|
|
|
|
33
|
|
|
$this->container->rebinding('request', function () { |
34
|
1 |
|
$this->refreshId(); |
35
|
7 |
|
}); |
36
|
7 |
|
} |
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() |
62
|
|
|
{ |
63
|
2 |
|
$this->id = false; |
64
|
|
|
|
65
|
2 |
|
return $this; |
66
|
|
|
} |
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
|
|
|
->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) |
96
|
|
|
{ |
97
|
4 |
|
if (! $strict) { |
98
|
4 |
|
$url = preg_replace('#^https?://#i', '', $url); |
99
|
4 |
|
$root = preg_replace('#^https?://#i', '', $root); |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
return preg_match('~^'.preg_quote($root, '~').'([/\?#].*)?$~i', $url); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Generate an absolute URL to the given path. |
107
|
|
|
* |
108
|
|
|
* @param string $path |
109
|
|
|
* @param mixed $query |
110
|
|
|
* @param mixed $appId |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
public function url($path = '', $query = [], $appId = '') |
114
|
|
|
{ |
115
|
1 |
|
if (is_string($query)) { |
116
|
1 |
|
list($query, $appId) = [$appId, $query]; |
117
|
|
|
} |
118
|
|
|
|
119
|
1 |
|
$url = $this->container['config']->get( |
120
|
1 |
|
"apps.url.$appId", |
121
|
1 |
|
$this->container['config']['app.url'] |
122
|
|
|
); |
123
|
|
|
|
124
|
1 |
|
if ($path = ltrim($path, '/')) { |
125
|
1 |
|
$url .= (strpos($path, '?') === 0 ? '' : '/').$path; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
if ($query && $query = http_build_query($query, '', '&', PHP_QUERY_RFC3986)) { |
129
|
1 |
|
$url .= (strpos($url, '?') === false ? '?' : '&').$query; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return $url; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|