|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Router\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Request; |
|
6
|
|
|
use Nip\Router\Generator\Traits\FormattingTrait; |
|
7
|
|
|
use Nip\Router\Generator\Traits\HasPreviousUrlTrait; |
|
8
|
|
|
use Nip\Router\Generator\Traits\UrlFunctionTrait; |
|
9
|
|
|
use Nip\Router\RequestContext; |
|
10
|
|
|
use Nip\Router\Route\Route; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class UrlGenerator |
|
14
|
|
|
* @package Nip\Router\Generator |
|
15
|
|
|
* @method RequestContext getContext() |
|
16
|
|
|
*/ |
|
17
|
|
|
class UrlGenerator extends \Symfony\Component\Routing\Generator\UrlGenerator |
|
18
|
|
|
{ |
|
19
|
|
|
use FormattingTrait; |
|
20
|
|
|
use UrlFunctionTrait; |
|
21
|
|
|
use HasPreviousUrlTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Set the current request instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Request $request |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setRequest(Request $request) |
|
29
|
|
|
{ |
|
30
|
|
|
$context = (new RequestContext())->fromRequest($request); |
|
31
|
|
|
|
|
32
|
|
|
$this->cachedRoot = null; |
|
33
|
|
|
$this->cachedSchema = null; |
|
34
|
|
|
return $this->setContext($context); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH) |
|
38
|
|
|
{ |
|
39
|
|
|
$name = $this->initDefaultRoute($name, $parameters); |
|
40
|
|
|
return parent::generate($name, $parameters, $referenceType); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $name |
|
46
|
|
|
* @param array $params |
|
47
|
|
|
* @return null|Route |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function initDefaultRoute($name, &$params = []) |
|
50
|
|
|
{ |
|
51
|
|
|
$route = $this->routes->get($name); |
|
52
|
|
|
if ($route) { |
|
53
|
|
|
return $name; |
|
54
|
|
|
} |
|
55
|
|
|
$parts = explode(".", $name); |
|
56
|
|
|
$count = count($parts); |
|
57
|
|
|
if ($count <= 3) { |
|
58
|
|
|
if (in_array(reset($parts), app('mvc.modules')->getNames())) { |
|
59
|
|
|
$module = array_shift($parts); |
|
60
|
|
|
$params['controller'] = isset($parts[0]) ? $parts[0] : null; |
|
61
|
|
|
$params['action'] = isset($parts[1]) ? $parts[1] : null; |
|
62
|
|
|
$route = $this->routes->get($module . '.default'); |
|
|
|
|
|
|
63
|
|
|
if ($route) { |
|
64
|
|
|
return $route->getName(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
return $name; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: