Completed
Push — 1.x ( 6d5e6d...45e5b4 )
by Akihito
03:03
created

src/AppInjector.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package;
8
9
use BEAR\AppMeta\AbstractAppMeta;
10
use BEAR\AppMeta\AppMeta;
11
use BEAR\Package\Exception\InvalidContextException;
12
use BEAR\Package\Provide\Resource\ResourceObjectModule;
13
use Ray\Compiler\DiCompiler;
14
use Ray\Compiler\Exception\NotCompiled;
15
use Ray\Compiler\ScriptInjector;
16
use Ray\Di\AbstractModule;
17
use Ray\Di\InjectorInterface;
18
use Ray\Di\Name;
19
20
final class AppInjector implements InjectorInterface
21
{
22
    /**
23
     * @var InjectorInterface
24
     */
25
    private $injector;
26
27
    /**
28
     * @var AppMeta
29
     */
30
    private $appMeta;
31
32 8
    public function __construct($name, $contexts)
33
    {
34 8
        $this->appMeta = new AppMeta($name, $contexts);
35 8
        $this->injector = $this->getInjector($this->appMeta, $contexts);
36 6
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 6
    public function getInstance($interface, $name = Name::ANY)
42
    {
43
        try {
44 6
            return $this->injector->getInstance($interface, $name);
45 1
        } catch (NotCompiled $e) {
46 1
            file_put_contents(sprintf('%s/%s', $this->appMeta->logDir, 'compile-err.log'), (string) $e);
47
48 1
            throw $e;
49
        }
50
    }
51
52
    /**
53
     * @param AbstractAppMeta $appMeta
54
     * @param string          $contexts
55
     *
56
     * @return InjectorInterface
57
     */
58 8
    private function getInjector(AbstractAppMeta $appMeta, $contexts)
59
    {
60 8
        $module = $this->newModule($appMeta, $contexts);
61 6
        $module->override(new AppMetaModule($appMeta));
62 6
        $scriptDir = $appMeta->tmpDir;
63 6
        $scriptInjector = new ScriptInjector($scriptDir);
64
        try {
65 6
            $injector = $scriptInjector->getInstance(InjectorInterface::class);
66 4
        } catch (NotCompiled $e) {
67 4
            $this->compile($module, $appMeta, $scriptDir);
68 4
            $injector = $scriptInjector->getInstance(InjectorInterface::class);
69
        }
70
71 6
        return $injector;
72
    }
73
74
    /**
75
     * Compile dependencies
76
     *
77
     * @param AbstractModule  $module
78
     * @param AbstractAppMeta $appMeta
79
     * @param string          $scriptDir
80
     */
81 4
    private function compile(AbstractModule $module, AbstractAppMeta $appMeta, $scriptDir)
0 ignored issues
show
The parameter $appMeta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The method parameter $appMeta is never used
Loading history...
82
    {
83 4
        $compiler = new DiCompiler($module, $scriptDir);
84 4
        $compiler->compile();
85 4
    }
86 4
87
    /**
88
     * Return configured module
89 4
     *
90 4
     * @param AbstractAppMeta $appMeta
91 4
     * @param string          $contexts
92 4
     *
93 4
     * @return AbstractModule
94
     */
95
    private function newModule(AbstractAppMeta $appMeta, $contexts)
96
    {
97
        $contextsArray = array_reverse(explode('-', $contexts));
98
        $module = null;
99
        foreach ($contextsArray as $context) {
100
            $class = $appMeta->name . '\Module\\' . ucwords($context) . 'Module';
101
            if (! class_exists($class)) {
102
                $class = 'BEAR\Package\Context\\' . ucwords($context) . 'Module';
103 8
            }
104
            if (! is_a($class, AbstractModule::class, true)) {
105 8
                throw new InvalidContextException($class);
106 8
            }
107 8
            /* @var $module AbstractModule */
108 8
            $module = new $class($module);
109 8
        }
110 6
        $module->install(new ResourceObjectModule($appMeta));
111
112 8
        return $module;
113 2
    }
114
}
115