Passed
Push — 1.x ( 2e6d71...2bb73e )
by Akihito
04:39 queued 02:13
created

PackageInjector::getInstance()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 23
rs 8.8333
cc 7
nc 13
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Injector;
6
7
use BEAR\AppMeta\AbstractAppMeta;
8
use BEAR\Package\LazyModule;
9
use BEAR\Package\Module;
10
use BEAR\Sunday\Extension\Application\AppInterface;
11
use Ray\Compiler\Annotation\Compile;
12
use Ray\Compiler\CompileInjector;
13
use Ray\Compiler\ScriptInjector;
14
use Ray\Di\AbstractModule;
15
use Ray\Di\Injector as RayInjector;
16
use Ray\Di\InjectorInterface;
17
use Symfony\Component\Cache\Adapter\AdapterInterface;
18
use Symfony\Contracts\Cache\CacheInterface;
19
20
use function assert;
21
use function is_bool;
22
use function is_dir;
23
use function mkdir;
24
use function str_replace;
25
use function trigger_error;
26
27
use const E_USER_WARNING;
28
29
final class PackageInjector
30
{
31
    /**
32
     * Serialized injector instances
33
     *
34
     * @var array<string, InjectorInterface>
35
     */
36
    private static array $instances;
37
38
    /** @codeCoverageIgnore */
39
    private function __construct()
40
    {
41
    }
42
43
    public static function getInstance(AbstractAppMeta $meta, string $context, CacheInterface|null $cache): InjectorInterface
44
    {
45
        $injectorId = str_replace('\\', '_', $meta->name) . $context;
46
        if (isset(self::$instances[$injectorId])) {
47
            return self::$instances[$injectorId];
48
        }
49
50
        assert($cache instanceof AdapterInterface);
51
        /** @psalm-suppress all */
52
        [$injector, $fileUpdate] = $cache->getItem($injectorId)->get(); // @phpstan-ignore-line
53
        $isCacheableInjector = $injector instanceof ScriptInjector || ($injector instanceof InjectorInterface && $fileUpdate instanceof FileUpdate && $fileUpdate->isNotUpdated($meta));
54
        if (! $isCacheableInjector) {
55
            $injector = self::factory($meta, $context);
56
            $cache->save($cache->getItem($injectorId)->set([$injector, new FileUpdate($meta)]));
57
            // Check the cache
58
            if ($cache->getItem($injectorId)->get() === null) {
59
                trigger_error('Failed to verify the injector cache. See https://github.com/bearsunday/BEAR.Package/issues/418', E_USER_WARNING);
60
            }
61
        }
62
63
        self::$instances[$injectorId] = $injector;
64
65
        return $injector;
66
    }
67
68
    public static function factory(AbstractAppMeta $meta, string $context, AbstractModule|null $overideModule = null): InjectorInterface
69
    {
70
        $scriptDir = $meta->tmpDir . '/di';
71
        ! is_dir($scriptDir) && ! @mkdir($scriptDir) && ! is_dir($scriptDir);
72
        $module = (new Module())($meta, $context);
73
74
        if ($overideModule instanceof AbstractModule) {
75
            $module->override($overideModule);
76
        }
77
78
        $injector = new RayInjector($module, $scriptDir);
79
        $isProd = $injector->getInstance('', Compile::class);
80
        assert(is_bool($isProd));
81
        if ($isProd) {
82
            $injector = new CompileInjector($scriptDir, new LazyModule($meta, $context, $scriptDir));
83
        }
84
85
        $injector->getInstance(AppInterface::class);
86
87
        return $injector;
88
    }
89
}
90