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