Completed
Push — master ( 432b9c...be3959 )
by Elf
01:51
created

Apps::url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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