1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Cache\Bootloader; |
6
|
|
|
|
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
8
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
9
|
|
|
use Spiral\Boot\DirectoriesInterface; |
10
|
|
|
use Spiral\Boot\EnvironmentInterface; |
11
|
|
|
use Spiral\Cache\CacheManager; |
12
|
|
|
use Spiral\Cache\CacheStorageProviderInterface; |
13
|
|
|
use Spiral\Cache\Config\CacheConfig; |
14
|
|
|
use Spiral\Cache\Core\CacheInjector; |
15
|
|
|
use Spiral\Cache\Storage\ArrayStorage; |
16
|
|
|
use Spiral\Cache\Storage\FileStorage; |
17
|
|
|
use Spiral\Config\ConfiguratorInterface; |
18
|
|
|
use Spiral\Config\Patch\Append; |
19
|
|
|
use Spiral\Core\Container; |
20
|
|
|
|
21
|
|
|
final class CacheBootloader extends Bootloader |
22
|
|
|
{ |
23
|
|
|
protected const SINGLETONS = [ |
24
|
|
|
CacheStorageProviderInterface::class => CacheManager::class, |
25
|
|
|
CacheManager::class => [self::class, 'initCacheManager'], |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
private ConfiguratorInterface $config; |
29
|
|
|
|
30
|
192 |
|
public function __construct(ConfiguratorInterface $config) |
31
|
|
|
{ |
32
|
192 |
|
$this->config = $config; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function registerTypeAlias(string $storageClass, string $alias): void |
36
|
|
|
{ |
37
|
|
|
$this->config->modify( |
38
|
|
|
'cache', |
39
|
|
|
new Append('typeAliases', $alias, $storageClass) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
192 |
|
public function boot(Container $container, EnvironmentInterface $env, DirectoriesInterface $dirs): void |
44
|
|
|
{ |
45
|
192 |
|
$this->initConfig($env, $dirs); |
46
|
|
|
|
47
|
192 |
|
$container->bindInjector(CacheInterface::class, CacheInjector::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @noRector RemoveUnusedPrivateMethodRector |
52
|
|
|
*/ |
53
|
2 |
|
private function initCacheManager(Container $container, CacheConfig $config): CacheManager |
54
|
|
|
{ |
55
|
2 |
|
$manager = new CacheManager($config, $container); |
56
|
|
|
|
57
|
2 |
|
foreach ($config->getAliases() as $alias => $storageName) { |
58
|
2 |
|
$container->bind($alias, static fn (CacheManager $manager) => $manager->storage($storageName)); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
return $manager; |
62
|
|
|
} |
63
|
|
|
|
64
|
192 |
|
private function initConfig(EnvironmentInterface $env, DirectoriesInterface $dirs): void |
65
|
|
|
{ |
66
|
192 |
|
$this->config->setDefaults( |
67
|
|
|
CacheConfig::CONFIG, |
68
|
|
|
[ |
69
|
192 |
|
'default' => $env->get('CACHE_STORAGE', 'array'), |
70
|
|
|
'aliases' => [], |
71
|
|
|
'storages' => [ |
72
|
|
|
'array' => [ |
73
|
|
|
'type' => 'array', |
74
|
|
|
], |
75
|
|
|
'file' => [ |
76
|
192 |
|
'type' => 'file', |
77
|
192 |
|
'path' => $dirs->get('runtime') . 'cache', |
78
|
|
|
], |
79
|
|
|
], |
80
|
|
|
'typeAliases' => [ |
81
|
|
|
'array' => ArrayStorage::class, |
82
|
|
|
'file' => FileStorage::class, |
83
|
|
|
], |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|