Completed
Pull Request — master (#3)
by Elf
05:38
created

Apps::root()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Apps;
4
5
use Illuminate\Contracts\Container\Container;
6
7
class Apps
8
{
9
    /**
10
     * The container instance.
11
     *
12
     * @var \Illuminate\Contracts\Container\Container
13
     */
14
    protected $container;
15
16
    /**
17
     * The current application identifier.
18
     *
19
     * @var string|false
20
     */
21
    protected $id = false;
22
23
    /**
24
     * Create a new Apps instance.
25
     *
26
     * @param  \Illuminate\Contracts\Container\Container  $container
27
     */
28 10
    public function __construct(Container $container)
29
    {
30 10
        $this->container = $container;
31
32
        $this->container->rebinding('request', function () {
33 1
            $this->refreshId();
34 10
        });
35 10
    }
36
37
    /**
38
     * Get or check the current application identifier.
39
     *
40
     * @return string|bool
41
     */
42 3
    public function id()
43
    {
44 3
        if ($this->id === false) {
45 3
            $this->id = $this->idForUrl($this->container['request']->getUri());
46
        }
47
48 3
        if (func_num_args() > 0) {
49 1
            return in_array($this->id, is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args());
50
        }
51
52 2
        return $this->id;
53
    }
54
55
    /**
56
     * Refresh the current application identifier.
57
     *
58
     * @return $this
59
     */
60 2
    public function refreshId()
61
    {
62 2
        $this->id = false;
63
64 2
        return $this;
65
    }
66
67
    /**
68
     * Get application identifier for the given URL.
69
     *
70
     * @param  string  $url
71
     * @return string
72
     */
73 4
    public function idForUrl($url)
74
    {
75 4
        return collect($this->container['config']['apps.url'])
76
            ->filter(function ($root) use ($url) {
77 4
                return $this->urlHasRoot($url, $root);
78 4
            })
79 4
            ->sortByDesc(function ($root) {
80 4
                return strlen($root);
81 4
            })
82 4
            ->keys()
83 4
            ->first();
84
    }
85
86
    /**
87
     * Determine if an URL has the given root URL.
88
     *
89
     * @param  string  $url
90
     * @param  string  $root
91
     * @param  bool  $strict
92
     * @return bool
93
     */
94 4
    protected function urlHasRoot($url, $root, $strict = false)
95
    {
96 4
        if (! $strict) {
97 4
            $url = $this->removeScheme($url);
98 4
            $root = $this->removeScheme($root);
99
        }
100
101 4
        return (bool) preg_match('~^'.preg_quote($root, '~').'([/\?#].*)?$~i', $url);
102
    }
103
104
    /**
105
     * Remove scheme for an URL.
106
     *
107
     * @param  string  $url
108
     * @return string
109
     */
110 4
    protected function removeScheme($url)
111
    {
112 4
        return preg_replace('#^https?://#i', '', $url);
113
    }
114
115
    /**
116
     * Get the root URL for the given application identifier.
117
     *
118
     * @param  string  $appId
119
     * @return string
120
     */
121 4
    public function root($appId = '')
122
    {
123 4
        return $this->container['config']["apps.url.$appId"]
124 4
            ?: $this->container['config']['app.url'];
125
    }
126
127
    /**
128
     * Get the domain for the given application identifier.
129
     *
130
     * @param  string  $appId
131
     * @return string
132
     */
133 1
    public function domain($appId = '')
134
    {
135 1
        return parse_url($this->root($appId), PHP_URL_HOST);
136
    }
137
138
    /**
139
     * Get the URL prefix for the given application identifier.
140
     *
141
     * @param  string  $appId
142
     * @return string
143
     */
144 1
    public function prefix($appId = '')
145
    {
146 1
        return trim(parse_url($this->root($appId), PHP_URL_PATH), '/');
147
    }
148
149
    /**
150
     * Generate an absolute URL to a path for the given application identifier.
151
     *
152
     * @param  string  $appId
153
     * @param  string  $path
154
     * @param  mixed  $extra
155
     * @return string
156
     */
157 1
    public function url($appId = '', $path = '', $extra = [])
158
    {
159 1
        return $this->root($appId).$this->stringAfter(
160 1
            $this->container['url']->to($path, $extra),
161 1
            $this->container['url']->to('')
162
        );
163
    }
164
165
    /**
166
     * Return the remainder of a string after a given value.
167
     *
168
     * @param  string  $subject
169
     * @param  string  $search
170
     * @return string
171
     */
172 1
    protected function stringAfter($subject, $search)
173
    {
174 1
        return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
175
    }
176
}
177