Completed
Push — tmp-dir ( 9e223b )
by Akihito
02:10
created

Injector::setupClassDir()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
crap 3
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\Untargetted;
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 = null)
32
    {
33 40
        $module = $module ?? new NullModule;
34 40
        $module->install(new AssistedModule);
0 ignored issues
show
Documentation introduced by
new \Ray\Di\AssistedModule() is of type object<Ray\Di\AssistedModule>, but the function expects a object<self>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35 40
        $this->container = $module->getContainer();
36 40
        $this->classDir = $this->setupClassDir($tmpDir);
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 10
    public function __wakeup()
47
    {
48 3
        spl_autoload_register(
49 10
            function ($class) {
50 7
                $file = $this->classDir . DIRECTORY_SEPARATOR . $class . '.php';
51 7
                if (file_exists($file)) {
52
                    // @codeCoverageIgnoreStart
53
                    include $file;
54
                    // @codeCoverageIgnoreEnd
55
                }
56 10
            }
57
        );
58 3
    }
59
60
    /**
61
     * @param string $interface
62
     * @param string $name
63
     *
64
     * @return mixed
65
     */
66 40
    public function getInstance($interface, $name = Name::ANY)
67
    {
68
        try {
69 40
            $instance = $this->container->getInstance($interface, $name);
70 16
        } catch (Untargetted $e) {
71 15
            $this->bind($interface);
72 14
            $instance = $this->getInstance($interface, $name);
73
        }
74
75 38
        return $instance;
76
    }
77
78
    /**
79
     * @param string $class
80
     */
81 15
    private function bind(string $class)
82
    {
83 15
        new Bind($this->container, $class);
84
        /* @var $bound Dependency */
85 15
        $bound = $this->container->getContainer()[$class . '-' . Name::ANY];
86 15
        if ($bound instanceof Dependency) {
87 15
            $this->container->weaveAspect(new Compiler($this->classDir), $bound)->getInstance($class, Name::ANY);
88
        }
89 14
    }
90
91 40
    private function setupClassDir(string $tmpDir = null) : string
92
    {
93 40
        $tmpRootDir = is_dir((string) $tmpDir) ? $tmpDir : sys_get_temp_dir();
94 40
        $classDir = $tmpRootDir . '/ray-di/';
95 40
        if (! is_dir($classDir)) {
96 2
            mkdir($classDir, 0777, true);
97
        }
98
99 40
        return $classDir;
100
    }
101
}
102