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