SymfonyCacheExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A registerCommands() 0 4 1
A registerServices() 0 13 3
1
<?php
2
3
namespace pjpawel\LightApi\Component\Cache;
4
5
use pjpawel\LightApi\Command\CommandsLoader;
6
use pjpawel\LightApi\Component\Extension\Extension;
7
use pjpawel\LightApi\Container\ContainerLoader;
8
use pjpawel\LightApi\Container\Definition\AliasDefinition;
9
use pjpawel\LightApi\Container\Definition\ClassDefinition;
10
use pjpawel\LightApi\Container\Definition\Definition;
11
use Symfony\Contracts\Cache\CacheInterface;
12
13
class SymfonyCacheExtension extends Extension
14
{
15
16
    /*
17
     * id => [
18
     *     class => '',
19
     *     args => []
20
     * ]
21
     */
22
23
    public function registerServices(ContainerLoader $container): void
24
    {
25
        $setKernelCache = !$container->has('kernel.cache');
26
        /** @var array<string, Definition> $definitions */
27
        $definitions = [];
28
        foreach ($this->config as $id => $adapterConfig) {
29
            $definitions[$id] =  new ClassDefinition($adapterConfig['class'], $adapterConfig['args'] ?? []);
30
            if ($setKernelCache) {
31
                $definitions['kernel.cache'] = new AliasDefinition(CacheInterface::class, '@' . $id);
32
                $setKernelCache = false;
33
            }
34
        }
35
        $container->addDefinitions($definitions);
36
    }
37
38
    public function registerCommands(CommandsLoader $commandLoader): void
39
    {
40
        $commandLoader->registerCommand('cache:clear', SymfonyCacheClearCommand::class);
41
        $commandLoader->registerCommand('cache:warmup', SymfonyCacheWarmupCommand::class);
42
    }
43
44
}