|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
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.