Completed
Push — master ( bd7a3a...ee577f )
by Ryan
08:36
created

UrlGenerator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 140
wmc 17
lcom 2
cbo 7
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A locale() 0 8 3
B asset() 0 33 5
B make() 0 23 5
A route() 0 10 2
1
<?php namespace Anomaly\Streams\Platform\Routing;
2
3
use Anomaly\Streams\Platform\Model\EloquentModel;
4
use Anomaly\Streams\Platform\Support\Presenter;
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\RouteCollection;
8
use Illuminate\Support\Str;
9
use StringTemplate\Engine;
10
11
/**
12
 * Class UrlGenerator
13
 *
14
 * @link          http://anomaly.is/streams-platform
15
 * @author        AnomalyLabs, Inc. <[email protected]>
16
 * @author        Ryan Thompson <[email protected]>
17
 * @package       Anomaly\Streams\Platform\Routing
18
 */
19
class UrlGenerator extends \Illuminate\Routing\UrlGenerator
20
{
21
22
    /**
23
     * The parser engine.
24
     *
25
     * @var Engine
26
     */
27
    protected $parser;
28
29
    /**
30
     * Create a new UrlGenerator instance.
31
     *
32
     * @param RouteCollection $routes
33
     * @param Request         $request
34
     */
35
    public function __construct(RouteCollection $routes, Request $request)
36
    {
37
        parent::__construct(app('router')->getRoutes(), $request);
38
39
        $this->parser = app(Engine::class);
40
41
        if (defined('LOCALE')) {
42
            $this->forceRootUrl($this->getRootUrl($this->getScheme(null)) . '/' . LOCALE);
43
        }
44
    }
45
46
    /**
47
     * Generate an absolute URL to the given asset.
48
     *
49
     * @param            $path
50
     * @param null       $locale
51
     * @param  mixed     $extra
52
     * @param  bool|null $secure
53
     * @return string
54
     */
55
    public function locale($path, $locale = null, $extra = [], $secure = null)
56
    {
57
        if ($locale == config('streams::locales.default')) {
58
            $locale = null;
59
        }
60
61
        return $this->asset($locale ? $locale . '/' . $path : $path, $extra, $secure);
62
    }
63
64
    /**
65
     * Generate an absolute URL to the given asset.
66
     *
67
     * @param  string    $asset
68
     * @param  mixed     $extra
69
     * @param  bool|null $secure
70
     * @return string
71
     */
72
    public function asset($asset, $extra = [], $secure = null)
73
    {
74
        // First we will check if the URL is already a valid URL. If it is we will not
75
        // try to generate a new one but will simply return the URL as is, which is
76
        // convenient since developers do not always have to check if it's valid.
77
        if ($this->isValidUrl($asset)) {
78
            return $asset;
79
        }
80
81
        $scheme = $this->getScheme($secure);
82
83
        $extra = $this->formatParameters($extra);
84
85
        $tail = implode('/', array_map('rawurlencode', (array)$extra));
86
87
        // Once we have the scheme we will compile the "tail" by collapsing the values
88
        // into a single string delimited by slashes. This just makes it convenient
89
        // for passing the array of parameters to this URL as a list of segments.
90
        $root = $this->getRootUrl($scheme);
91
92
        if (defined('LOCALE') && ends_with($root, $search = '/' . LOCALE)) {
93
            $root = substr_replace($root, '', strrpos($root, $search), strlen($search));
94
        }
95
96
        if (($queryPosition = strpos($asset, '?')) !== false) {
97
            $query = mb_substr($asset, $queryPosition);
98
            $asset = mb_substr($asset, 0, $queryPosition);
99
        } else {
100
            $query = '';
101
        }
102
103
        return $this->trimUrl($root, $asset, $tail) . $query;
104
    }
105
106
    /**
107
     * Make a route path.
108
     *
109
     * @param       $name
110
     * @param       $entry
111
     * @param array $parameters
112
     * @return mixed|null|string
113
     */
114
    public function make($name, $entry, array $parameters = [])
115
    {
116
        if (!$route = $this->routes->getByName($name)) {
117
            return null;
118
        }
119
120
        if ($entry instanceof EloquentModel) {
121
            $entry = $entry->toRoutable();
122
        }
123
124
        if ($entry instanceof Presenter) {
125
            $entry = $entry->getObject();
126
        }
127
128
        if ($entry instanceof Arrayable) {
129
            $entry = $entry->toArray();
130
        }
131
132
        return '/' . $this->addQueryString(
133
            $this->parser->render(str_replace('?}', '}', $route->uri()), $entry),
134
            $parameters
135
        );
136
    }
137
138
    /**
139
     * Get the URL to a named route.
140
     *
141
     * @param  string $name
142
     * @param  mixed  $parameters
143
     * @param  bool   $absolute
144
     * @return string
145
     *
146
     * @throws \InvalidArgumentException
147
     */
148
    public function route($name, $parameters = [], $absolute = true)
149
    {
150
        $route = parent::route($name, $parameters, $absolute);
151
152
        if (!array_filter($parameters)) {
153
            $route = trim($route, '?');
154
        }
155
156
        return $route;
157
    }
158
}
159