1 | <?php |
||
17 | trait UrlUtils |
||
18 | { |
||
19 | /** |
||
20 | * @return Request |
||
21 | */ |
||
22 | abstract public function getRequest(); |
||
23 | |||
24 | /** |
||
25 | * Gets the url for provided path |
||
26 | * |
||
27 | * @param string $path |
||
28 | * @return string |
||
29 | */ |
||
30 | 4 | public function getUrl($path) |
|
31 | { |
||
32 | 4 | $regExp = '/\/\/|https?:/i'; |
|
33 | 4 | if (preg_match($regExp, $path)) { |
|
34 | 2 | return $path; |
|
35 | } |
||
36 | try { |
||
37 | 2 | $generated = call_user_func_array( |
|
38 | 2 | [$this->getRouterGenerator(), 'generate'], |
|
39 | 1 | func_get_args() |
|
40 | 1 | ); |
|
41 | 1 | } catch (AuraException $caught) { |
|
42 | $generated = false; |
||
43 | } |
||
44 | |||
45 | 2 | $basePath = rtrim($this->getRequest()->getBasePath(), '/'); |
|
46 | 1 | $path = $generated |
|
47 | 1 | ? $generated |
|
48 | 2 | : "{$basePath}/{$path}"; |
|
49 | |||
50 | 2 | return $path; |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * Return Router path generator |
||
55 | * |
||
56 | * @return \Aura\Router\Generator |
||
57 | */ |
||
58 | 2 | protected function getRouterGenerator() |
|
64 | } |