|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Di; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\AnnotationException; |
|
8
|
|
|
use Ray\Aop\Compiler; |
|
9
|
|
|
|
|
10
|
|
|
use function file_exists; |
|
11
|
|
|
use function spl_autoload_register; |
|
12
|
|
|
use function sprintf; |
|
13
|
|
|
use function str_replace; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @psalm-import-type MethodArguments from Types |
|
17
|
|
|
*/ |
|
18
|
|
|
final class Grapher |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var non-empty-string */ |
|
|
|
|
|
|
21
|
|
|
private $classDir; |
|
22
|
|
|
|
|
23
|
|
|
/** @var Container */ |
|
24
|
|
|
private $container; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param AbstractModule $module Binding module |
|
28
|
|
|
* @param non-empty-string $classDir Class directory |
|
|
|
|
|
|
29
|
|
|
* |
|
30
|
|
|
* @throws AnnotationException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(AbstractModule $module, string $classDir) |
|
33
|
|
|
{ |
|
34
|
|
|
$module->install(new AssistedModule()); |
|
35
|
|
|
$this->container = $module->getContainer(); |
|
36
|
|
|
$this->classDir = $classDir; |
|
37
|
|
|
$this->container->weaveAspects(new Compiler($this->classDir)); |
|
38
|
|
|
|
|
39
|
|
|
// builtin injection |
|
40
|
|
|
(new Bind($this->container, InjectorInterface::class))->toInstance(new Injector($module)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Wakeup |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __wakeup() |
|
47
|
|
|
{ |
|
48
|
|
|
spl_autoload_register( |
|
49
|
|
|
function (string $class): void { |
|
50
|
|
|
$file = sprintf('%s/%s.php', $this->classDir, str_replace('\\', '_', $class)); |
|
51
|
|
|
if (file_exists($file)) { |
|
52
|
|
|
include $file; //@codeCoverageIgnore |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Build an object graph with give constructor parameters |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $class class name |
|
62
|
|
|
* @param list<mixed> $params construct parameters (MethodArguments) |
|
|
|
|
|
|
63
|
|
|
* |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
|
|
public function newInstanceArgs(string $class, array $params) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->container->getInstanceWithArgs($class, $params); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|