1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jumilla\Addomnipot\Laravel; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
6
|
|
|
use Illuminate\Console\AppNamespaceDetectorTrait; |
7
|
|
|
|
8
|
|
|
class AliasResolver |
9
|
|
|
{ |
10
|
|
|
use AppNamespaceDetectorTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var static |
14
|
|
|
*/ |
15
|
|
|
protected static $instance; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $apppath |
19
|
|
|
* @param array $addons |
20
|
|
|
* @param array $aliases |
21
|
|
|
*/ |
22
|
2 |
|
public static function register($apppath, array $addons, array $aliases) |
23
|
|
|
{ |
24
|
2 |
|
static::$instance = new static($apppath, $addons, $aliases); |
25
|
|
|
|
26
|
2 |
|
spl_autoload_register([static::$instance, 'load'], true, false); |
27
|
2 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
*/ |
31
|
2 |
|
public static function unregister() |
32
|
|
|
{ |
33
|
2 |
|
if (static::$instance) { |
34
|
2 |
|
spl_autoload_unregister([static::$instance, 'load']); |
35
|
2 |
|
} |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $addons; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $globalClassAliases; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The constructor. |
50
|
|
|
* |
51
|
|
|
* @param string $apppath |
52
|
|
|
* @param array $addons |
53
|
|
|
* @param array $aliases |
54
|
|
|
*/ |
55
|
3 |
|
public function __construct($apppath, array $addons, array $aliases) |
56
|
|
|
{ |
57
|
3 |
|
$this->addons = array_merge([$this->makeAppAddon($apppath)], $addons); |
58
|
3 |
|
$this->globalClassAliases = $aliases; |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Make addon instance for application namespace. |
63
|
|
|
* |
64
|
|
|
* @param string $path |
65
|
|
|
* |
66
|
|
|
* @return static |
67
|
|
|
*/ |
68
|
3 |
|
protected function makeAppAddon($path) |
69
|
|
|
{ |
70
|
|
|
$config = [ |
71
|
3 |
|
'namespace' => trim($this->getAppNamespace(), '\\'), |
72
|
3 |
|
]; |
73
|
|
|
|
74
|
3 |
|
return new Addon(null, 'app', $path, $config); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $className |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
1 |
|
public function load($className) |
83
|
|
|
{ |
84
|
1 |
|
foreach ($this->addons as $addon) { |
85
|
1 |
|
$namespace = $addon->phpNamespace(); |
86
|
|
|
|
87
|
|
|
// 名前空間のないパッケージはエイリアス解決をする必要がない |
88
|
1 |
|
if (empty($namespace)) { |
89
|
1 |
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
$namespacePrefix = $namespace.'\\'; |
93
|
1 |
|
$includesGlobalAliases = $addon->config('addon.includes_global_aliases', true); |
94
|
1 |
|
$addonAliases = $addon->config('addon.aliases', []); |
95
|
|
|
|
96
|
|
|
// アドオンの名前空間下のクラスでないなら |
97
|
1 |
|
if (!starts_with($className, $namespacePrefix)) { |
98
|
1 |
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// クラス名を取り出す |
102
|
1 |
|
$parts = explode('\\', $className); |
103
|
1 |
|
$relativeClassName = $parts[count($parts) - 1]; |
104
|
|
|
|
105
|
|
|
// グローバルなエイリアスかチェックする |
106
|
1 |
|
if ($includesGlobalAliases) { |
107
|
1 |
|
if (isset($this->globalClassAliases[$relativeClassName])) { |
108
|
1 |
|
$originalClassName = $this->globalClassAliases[$relativeClassName]; |
109
|
1 |
|
class_alias($originalClassName, $className); |
110
|
|
|
|
111
|
1 |
|
return true; |
112
|
|
|
} |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
// パッケージ固有のエイリアスかチェックする |
116
|
1 |
|
if ($addonAliases) { |
117
|
1 |
|
if (isset($addonAliases[$relativeClassName])) { |
118
|
1 |
|
$originalClassName = $addonAliases[$relativeClassName]; |
119
|
1 |
|
class_alias($originalClassName, $className); |
120
|
|
|
|
121
|
1 |
|
return true; |
122
|
|
|
} |
123
|
|
|
} |
124
|
1 |
|
} |
125
|
|
|
|
126
|
1 |
|
return false; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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: