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