Passed
Pull Request — 1.x (#417)
by Akihito
06:19 queued 04:11
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\Exception\LogicException;
9
use BEAR\Package\LazyModule;
10
use BEAR\Package\Module;
11
use BEAR\Sunday\Extension\Application\AppInterface;
12
use Ray\Compiler\Annotation\Compile;
13
use Ray\Compiler\CompileInjector;
14
use Ray\Compiler\ScriptInjector;
15
use Ray\Di\AbstractModule;
16
use Ray\Di\Injector as RayInjector;
17
use Ray\Di\InjectorInterface;
18
use Symfony\Component\Cache\Adapter\AdapterInterface;
19
use Symfony\Contracts\Cache\CacheInterface;
20
21
use function assert;
22
use function is_bool;
23
use function is_dir;
24
use function mkdir;
25
use function str_replace;
26
27
final class PackageInjector
28
{
29
    /**
30
     * Serialized injector instances
31
     *
32
     * @var array<string, InjectorInterface>
33
     */
34
    private static array $instances;
35
36
    /** @codeCoverageIgnore */
37
    private function __construct()
38
    {
39
    }
40
41
    public static function getInstance(AbstractAppMeta $meta, string $context, CacheInterface|null $cache): InjectorInterface
42
    {
43
        $injectorId = str_replace('\\', '_', $meta->name) . $context;
44
        if (isset(self::$instances[$injectorId])) {
45
            return self::$instances[$injectorId];
46
        }
47
48
        assert($cache instanceof AdapterInterface);
49
        /** @psalm-suppress all */
50
        [$injector, $fileUpdate] = $cache->getItem($injectorId)->get(); // @phpstan-ignore-line
51
        $isCacheableInjector = $injector instanceof ScriptInjector || ($injector instanceof InjectorInterface && $fileUpdate instanceof FileUpdate && $fileUpdate->isNotUpdated($meta));
52
        if (! $isCacheableInjector) {
53
            $injector = self::factory($meta, $context);
54
            $cache->save($cache->getItem($injectorId)->set([$injector, new FileUpdate($meta)]));
55
            // Check the cache
56
            if ($cache->getItem($injectorId)->get() === null) {
57
                throw new LogicException('Failed to save the injector cache. Perhaps a non-serializable object is contained.');
58
            }
59
        }
60
61
        self::$instances[$injectorId] = $injector;
62
63
        return $injector;
64
    }
65
66
    public static function factory(AbstractAppMeta $meta, string $context, AbstractModule|null $overideModule = null): InjectorInterface
67
    {
68
        $scriptDir = $meta->tmpDir . '/di';
69
        ! is_dir($scriptDir) && ! @mkdir($scriptDir) && ! is_dir($scriptDir);
70
        $module = (new Module())($meta, $context);
71
72
        if ($overideModule instanceof AbstractModule) {
73
            $module->override($overideModule);
74
        }
75
76
        $injector = new RayInjector($module, $scriptDir);
77
        $isProd = $injector->getInstance('', Compile::class);
78
        assert(is_bool($isProd));
79
        if ($isProd) {
80
            $injector = new CompileInjector($scriptDir, new LazyModule($meta, $context, $scriptDir));
81
        }
82
83
        $injector->getInstance(AppInterface::class);
84
85
        return $injector;
86
    }
87
}
88