Completed
Pull Request — master (#3)
by Elf
02:53
created

Apps::domain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Apps;
4
5
use Illuminate\Support\Str;
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 10
    public function __construct(Container $container)
30
    {
31 10
        $this->container = $container;
32
33
        $this->container->rebinding('request', function () {
34 1
            $this->refreshId();
35 10
        });
36 10
    }
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 3
        }
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 collect($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 = $this->removeScheme($url);
99 4
            $root = $this->removeScheme($root);
100 4
        }
101
102 4
        return (bool) preg_match('~^'.preg_quote($root, '~').'([/\?#].*)?$~i', $url);
103
    }
104
105
    /**
106
     * Remove scheme for an URL.
107
     *
108
     * @param  string  $url
109
     * @return string
110
     */
111 4
    protected function removeScheme($url)
112
    {
113 4
        return preg_replace('#^https?://#i', '', $url);
114
    }
115
116
    /**
117
     * Get the root URL for the given application identifier.
118
     *
119
     * @param  string  $appId
120
     * @return string
121
     */
122 4
    public function root($appId = '')
123
    {
124 4
        return $this->container['config']["apps.url.$appId"]
125 4
            ?: $this->container['config']['app.url'];
126
    }
127
128
    /**
129
     * Get the domain for the given application identifier.
130
     *
131
     * @param  string  $appId
132
     * @return string
133
     */
134 1
    public function domain($appId = '')
135
    {
136 1
        return parse_url($this->root($appId), PHP_URL_HOST);
137
    }
138
139
    /**
140
     * Get the URL prefix for the given application identifier.
141
     *
142
     * @param  string  $appId
143
     * @return string
144
     */
145 1
    public function prefix($appId = '')
146
    {
147 1
        return trim(parse_url($this->root($appId), PHP_URL_PATH), '/');
148
    }
149
150
    /**
151
     * Generate an absolute URL to a path for the given application identifier.
152
     *
153
     * @param  string  $appId
154
     * @param  string  $path
155
     * @param  mixed  $extra
156
     * @return string
157
     */
158 1
    public function url($appId = '', $path = '', $extra = [])
159
    {
160 1
        return $this->root($appId).$this->stringAfter(
161 1
            $this->container['url']->to($path, $extra),
162 1
            $this->container['url']->to('')
163 1
        );
164
    }
165
166
    /**
167
     * Return the remainder of a string after a given value.
168
     *
169
     * @param  string  $subject
170
     * @param  string  $search
171
     * @return string
172
     */
173 1
    protected function stringAfter($subject, $search)
174
    {
175 1
        return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
176
    }
177
}
178