Completed
Push — cache ( ab24d0 )
by Akihito
05:07
created

CachedInjectorFactory::getCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Compiler;
6
7
use Doctrine\Common\Cache\CacheProvider;
8
use Ray\Di\AbstractModule;
9
use Ray\Di\InjectorInterface;
10
use Symfony\Component\Cache\Adapter\ApcuAdapter;
11
use Symfony\Component\Cache\Adapter\ChainAdapter;
12
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
13
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
14
use Symfony\Contracts\Cache\CacheInterface;
15
16
final class CachedInjectorFactory
17
{
18
    /** @var array<string, InjectorInterface> */
19
    private static $injectors = [];
20
21
    private const KEY_INJECTOR = 'injector';
22
23
    private function __construct()
24
    {
25
    }
26
27
    /**
28
     * @param callable(): AbstractModule $modules
29
     * @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...
30
     */
31
    public static function getInstance(string $injectorId, string $scriptDir, callable $modules, ?CacheProvider $cache = null, array $savedSingletons = []): InjectorInterface
32
    {
33
        if (isset(self::$injectors[$injectorId])) {
34
            return self::$injectors[$injectorId];
35
        }
36
37
        $cache = self::getCache($injectorId, $cache);
38
39
        return $cache->get(self::KEY_INJECTOR, static function () use ($injectorId, $modules, $scriptDir, $savedSingletons) {
40
            $injector = self::getInjector($modules, $scriptDir, $savedSingletons);
41
            self::$injectors[$injectorId] = $injector;
42
43
            return $injector;
44
        });
45
    }
46
47
    /**
48
     * @param callable(): AbstractModule $modules
49
     * @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...
50
     */
51
    private static function getInjector(callable $modules, string $scriptDir, array $savedSingletons): InjectorInterface
52
    {
53
        $injector = InjectorFactory::getInstance($modules, $scriptDir);
54
        foreach ($savedSingletons as $singleton) {
55
            $injector->getInstance($singleton);
56
        }
57
58
        return $injector;
59
    }
60
61
    private static function getCache(string $injectorId, ?CacheProvider $cache): CacheInterface
62
    {
63
        if ($cache instanceof CacheProvider) {
64
            return new DoctrineAdapter($cache);
65
        }
66
67
        return new ChainAdapter([new ApcuAdapter($injectorId), new PhpFilesAdapter($injectorId)]);
68
    }
69
}
70