Completed
Push — assisted ( 82d8ca...d6f990 )
by Akihito
03:53
created

Injector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.67%

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 8
c 8
b 1
f 0
lcom 1
cbo 6
dl 0
loc 76
ccs 29
cts 30
cp 0.9667
rs 10

4 Methods

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