Completed
Pull Request — 2.x (#216)
by Akihito
09:22
created

CachedInjectorFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 19 4
A getInjector() 0 9 2
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\InjectorInterface;
10
11
final class CachedInjectorFactory
12
{
13
    /**
14
     * @var array<string, InjectorInterface>
15
     */
16
    private static $injectors = [];
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
     * @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...
25
     */
26
    public static function getInstance(string $injectorId, string $scriptDir, callable $modules, CacheProvider $cache = null, array $savedSingletons = []) : InjectorInterface
27
    {
28
        if (isset(self::$injectors[$injectorId])) {
29
            return self::$injectors[$injectorId];
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
        $injector = self::getInjector($modules, $scriptDir, $savedSingletons);
38
        if ($injector instanceof ScriptInjector) {
39
            $cache->save(InjectorInterface::class, $injector);
40
        }
41
        self::$injectors[$injectorId] = $injector;
42
43
        return $injector;
44
    }
45
46
    /**
47
     * @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...
48
     * @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...
49
     */
50
    private static function getInjector(callable $modules, string $scriptDir, array $savedSingletons) : InjectorInterface
51
    {
52
        $injector = InjectorFactory::getInstance($modules, $scriptDir);
53
        foreach ($savedSingletons as $singleton) {
54
            $injector->getInstance($singleton);
55
        }
56
57
        return $injector;
58
    }
59
}
60