|
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
|
196 |
|
public function __construct(ConfiguratorInterface $config) |
|
31
|
|
|
{ |
|
32
|
196 |
|
$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
|
196 |
|
public function boot(Container $container, EnvironmentInterface $env, DirectoriesInterface $dirs): void |
|
44
|
|
|
{ |
|
45
|
196 |
|
$this->initConfig($env, $dirs); |
|
46
|
|
|
|
|
47
|
196 |
|
$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 function (CacheManager $manager) use ($storageName) { |
|
59
|
|
|
return $manager->storage($storageName); |
|
60
|
|
|
}); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
return $manager; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
196 |
|
private function initConfig(EnvironmentInterface $env, DirectoriesInterface $dirs): void |
|
67
|
|
|
{ |
|
68
|
196 |
|
$this->config->setDefaults( |
|
69
|
|
|
CacheConfig::CONFIG, |
|
70
|
|
|
[ |
|
71
|
196 |
|
'default' => $env->get('CACHE_STORAGE', 'array'), |
|
72
|
|
|
'aliases' => [], |
|
73
|
|
|
'storages' => [ |
|
74
|
|
|
'array' => [ |
|
75
|
|
|
'type' => 'array', |
|
76
|
|
|
], |
|
77
|
|
|
'file' => [ |
|
78
|
196 |
|
'type' => 'file', |
|
79
|
196 |
|
'path' => $dirs->get('runtime') . 'cache', |
|
80
|
|
|
], |
|
81
|
|
|
], |
|
82
|
|
|
'typeAliases' => [ |
|
83
|
|
|
'array' => ArrayStorage::class, |
|
84
|
|
|
'file' => FileStorage::class, |
|
85
|
|
|
], |
|
86
|
|
|
] |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|