Completed
Pull Request — 2.x (#216)
by Akihito
03:32
created

InjectorFactory::getScriptInjector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
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\Compiler;
6
7
use Ray\Compiler\Annotation\Compile;
8
use Ray\Di\AbstractModule;
9
use Ray\Di\Exception\Unbound;
10
use Ray\Di\Injector as RayInjector;
11
use Ray\Di\InjectorInterface;
12
13
/**
14
 * @psalm-immutable
15
 */
16
final class InjectorFactory
17
{
18
    private function __construct()
19
    {
20
    }
21
22
    /**
23
     * @param callable():\Ray\Di\AbstractModule $modules
0 ignored issues
show
Documentation introduced by
The doc-type callable():\Ray\Di\AbstractModule could not be parsed: Expected "|" or "end of type", but got "(" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
24
     */
25
    public static function getInstance(callable $modules, string $scriptDir) : InjectorInterface
26
    {
27
        ! is_dir($scriptDir) && ! @mkdir($scriptDir) && ! is_dir($scriptDir);
28
        $module = $modules();
29
        $rayInjector = new RayInjector($module, $scriptDir);
30
        $isProd = false;
31
        try {
32
            $isProd = $rayInjector->getInstance('', Compile::class);
33
        } catch (Unbound $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
34
        }
35
36
        return $isProd ? self::getScriptInjector($scriptDir, $module) : $rayInjector;
37
    }
38
39
    private static function getScriptInjector(string $scriptDir, AbstractModule $module) : ScriptInjector
40
    {
41
        return new ScriptInjector($scriptDir, function () use ($scriptDir, $module) {
42
            return new ScriptinjectorModule($scriptDir, $module);
43
        });
44
    }
45
}
46