Completed
Push — master ( cf4988...bd7a3a )
by Ryan
10:17
created

UrlGenerator::route()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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