Grapher::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
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
final class Grapher
16
{
17
    /** @var string */
18
    private $classDir;
19
20
    /** @var Container */
21
    private $container;
22
23
    /**
24
     * @param AbstractModule $module Binding module
25
     *
26
     * @throws AnnotationException
27
     */
28
    public function __construct(AbstractModule $module, string $classDir)
29
    {
30
        $module->install(new AssistedModule());
31
        $this->container = $module->getContainer();
32
        $this->classDir = $classDir;
33
        $this->container->weaveAspects(new Compiler($this->classDir));
34
35
        // builtin injection
36
        (new Bind($this->container, InjectorInterface::class))->toInstance(new Injector($module));
37
    }
38
39
    /**
40
     * Wakeup
41
     */
42
    public function __wakeup()
43
    {
44
        spl_autoload_register(
45
            function (string $class): void {
46
                $file = sprintf('%s/%s.php', $this->classDir, str_replace('\\', '_', $class));
47
                if (file_exists($file)) {
48
                    include $file; //@codeCoverageIgnore
49
                }
50
            }
51
        );
52
    }
53
54
    /**
55
     * Build an object graph with give constructor parameters
56
     *
57
     * @param string            $class  class name
58
     * @param array<int, mixed> $params construct parameters
59
     *
60
     * @return mixed
61
     */
62
    public function newInstanceArgs(string $class, array $params)
63
    {
64
        return $this->container->getInstanceWithArgs($class, $params);
65
    }
66
}
67