|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of the Ray.Di package. |
|
7
|
|
|
* |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Ray\Di; |
|
11
|
|
|
|
|
12
|
|
|
use Ray\Aop\Compiler; |
|
13
|
|
|
use Ray\Di\Exception\Untargeted; |
|
14
|
|
|
|
|
15
|
|
|
class Injector implements InjectorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $classDir; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Container |
|
24
|
|
|
*/ |
|
25
|
|
|
private $container; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param AbstractModule $module Binding module |
|
29
|
|
|
* @param string $tmpDir Temp directory for generated class |
|
30
|
|
|
*/ |
|
31
|
40 |
|
public function __construct(AbstractModule $module = null, string $tmpDir = '') |
|
32
|
|
|
{ |
|
33
|
40 |
|
$module = $module ?? new NullModule; |
|
34
|
40 |
|
$module->install(new AssistedModule); |
|
|
|
|
|
|
35
|
40 |
|
$this->container = $module->getContainer(); |
|
36
|
40 |
|
$this->classDir = is_dir($tmpDir) ? $tmpDir : sys_get_temp_dir(); |
|
37
|
40 |
|
$this->container->weaveAspects(new Compiler($this->classDir)); |
|
38
|
|
|
|
|
39
|
|
|
// builtin injection |
|
40
|
40 |
|
(new Bind($this->container, InjectorInterface::class))->toInstance($this); |
|
41
|
40 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Wakeup |
|
45
|
|
|
*/ |
|
46
|
7 |
|
public function __wakeup() |
|
47
|
|
|
{ |
|
48
|
3 |
|
spl_autoload_register( |
|
49
|
7 |
|
function (string $class) { |
|
50
|
4 |
|
$file = $this->classDir . DIRECTORY_SEPARATOR . $class . '.php'; |
|
51
|
4 |
|
if (file_exists($file)) { |
|
52
|
|
|
include $file; //@codeCoverageIgnore |
|
53
|
|
|
} |
|
54
|
7 |
|
} |
|
55
|
|
|
); |
|
56
|
3 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $interface |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
40 |
|
public function getInstance($interface, $name = Name::ANY) |
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
40 |
|
$instance = $this->container->getInstance($interface, $name); |
|
68
|
17 |
|
} catch (Untargeted $e) { |
|
69
|
16 |
|
$this->bind($interface); |
|
70
|
15 |
|
$instance = $this->getInstance($interface, $name); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
38 |
|
return $instance; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $class |
|
78
|
|
|
*/ |
|
79
|
16 |
|
private function bind(string $class) |
|
80
|
|
|
{ |
|
81
|
16 |
|
new Bind($this->container, $class); |
|
82
|
|
|
/* @var $bound Dependency */ |
|
83
|
16 |
|
$bound = $this->container->getContainer()[$class . '-' . Name::ANY]; |
|
84
|
16 |
|
if ($bound instanceof Dependency) { |
|
85
|
16 |
|
$this->container->weaveAspect(new Compiler($this->classDir), $bound)->getInstance($class, Name::ANY); |
|
86
|
|
|
} |
|
87
|
15 |
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
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: