Completed
Push — refactor ( 558ab7...25d2de )
by Akihito
08:19
created

CachedInjectorFactory::getInstance()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 4
nc 4
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Compiler;
6
7
use Doctrine\Common\Cache\CacheProvider;
8
use Doctrine\Common\Cache\VoidCache;
9
use Ray\Di\AbstractModule;
10
use Ray\Di\InjectorInterface;
11
12
final class CachedInjectorFactory
13
{
14
    /** @var array<string, InjectorInterface> */
15
    private static $injectors = [];
16
17
    private function __construct()
18
    {
19
    }
20
21
    /**
22
     * @param callable(): AbstractModule $modules
23
     * @param array<class-string>        $savedSingletons
0 ignored issues
show
Documentation introduced by
The doc-type array<class-string> could not be parsed: Unknown type name "class-string" at position 6. (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(string $injectorId, string $scriptDir, callable $modules, ?CacheProvider $cache = null, array $savedSingletons = []): InjectorInterface
26
    {
27
        if (isset(self::$injectors[$injectorId])) {
28
            return self::$injectors[$injectorId];
29
        }
30
31
        $cache = $cache ?? new VoidCache();
32
        $cache->setNamespace($injectorId);
33
        $cachedInjector = $cache->fetch(InjectorInterface::class);
34
        if ($cachedInjector instanceof InjectorInterface) {
35
            return $cachedInjector;
36
        }
37
38
        $injector = self::getInjector($modules, $scriptDir, $savedSingletons);
39
        if ($injector instanceof ScriptInjector) {
40
            $cache->save(InjectorInterface::class, $injector);
41
        }
42
43
        self::$injectors[$injectorId] = $injector;
44
45
        return $injector;
46
    }
47
48
    /**
49
     * @param callable(): AbstractModule $modules
50
     * @param array<class-string>        $savedSingletons
0 ignored issues
show
Documentation introduced by
The doc-type array<class-string> could not be parsed: Unknown type name "class-string" at position 6. (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...
51
     */
52
    private static function getInjector(callable $modules, string $scriptDir, array $savedSingletons): InjectorInterface
53
    {
54
        $injector = InjectorFactory::getInstance($modules, $scriptDir);
55
        foreach ($savedSingletons as $singleton) {
56
            $injector->getInstance($singleton);
57
        }
58
59
        return $injector;
60
    }
61
}
62