|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Bootstrappers\Assets; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Assets\CacheManager\Dummy as DummyCacheManager; |
|
8
|
|
|
use AbterPhp\Framework\Assets\CacheManager\Flysystem as FlysystemCacheManager; |
|
9
|
|
|
use AbterPhp\Framework\Assets\CacheManager\ICacheManager; |
|
10
|
|
|
use AbterPhp\Framework\Constant\Env; |
|
11
|
|
|
use AbterPhp\Framework\Environments\Environment; |
|
12
|
|
|
use League\Flysystem\Filesystem; |
|
13
|
|
|
use League\Flysystem\Local\LocalFilesystemAdapter; |
|
14
|
|
|
use Opulence\Ioc\Bootstrappers\Bootstrapper; |
|
15
|
|
|
use Opulence\Ioc\Bootstrappers\ILazyBootstrapper; |
|
16
|
|
|
use Opulence\Ioc\IContainer; |
|
17
|
|
|
|
|
18
|
|
|
class CacheManagerBootstrapper extends Bootstrapper implements ILazyBootstrapper |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @inheritdoc |
|
22
|
|
|
*/ |
|
23
|
|
|
public function getBindings(): array |
|
24
|
|
|
{ |
|
25
|
|
|
return [ |
|
26
|
|
|
ICacheManager::class, |
|
27
|
|
|
]; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param IContainer $container |
|
32
|
|
|
*/ |
|
33
|
|
|
public function registerBindings(IContainer $container): void |
|
34
|
|
|
{ |
|
35
|
|
|
if (Environment::mustGetVar(Env::ENV_NAME) === Environment::DEVELOPMENT) { |
|
36
|
|
|
$cacheManager = new DummyCacheManager(); |
|
37
|
|
|
} else { |
|
38
|
|
|
$cacheManager = new FlysystemCacheManager(); |
|
39
|
|
|
|
|
40
|
|
|
$this->registerCachePaths($cacheManager); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$container->bindInstance(ICacheManager::class, $cacheManager); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param ICacheManager $cacheManager |
|
48
|
|
|
*/ |
|
49
|
|
|
private function registerCachePaths(ICacheManager $cacheManager): void |
|
50
|
|
|
{ |
|
51
|
|
|
$mediaDir = Environment::mustGetVar(Env::DIR_MEDIA, ""); |
|
52
|
|
|
$cacheBasePath = Environment::mustGetVar(Env::CACHE_BASE_PATH, ""); |
|
53
|
|
|
|
|
54
|
|
|
$cacheDir = sprintf( |
|
55
|
|
|
'%s%s%s', |
|
56
|
|
|
rtrim($mediaDir, DIRECTORY_SEPARATOR), |
|
57
|
|
|
DIRECTORY_SEPARATOR, |
|
58
|
|
|
rtrim($cacheBasePath, DIRECTORY_SEPARATOR) |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
$cacheManager->registerFilesystem(new Filesystem(new LocalFilesystemAdapter($cacheDir))); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|