Completed
Push — master ( df428d...59132f )
by Divine Niiquaye
02:39
created

CacheExtension::getConfigSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of BiuradPHP opensource projects.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\Cache\Bridges\Nette;
19
20
use Biurad\Cache\AdapterFactory;
21
use Biurad\Cache\Interfaces\CacheAdapterInterface;
22
use Nette;
0 ignored issues
show
Bug introduced by
The type Nette was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Nette\DI\Definitions\Statement;
0 ignored issues
show
Bug introduced by
The type Nette\DI\Definitions\Statement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Nette\Schema\Expect;
0 ignored issues
show
Bug introduced by
The type Nette\Schema\Expect was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
class CacheExtension extends Nette\DI\CompilerExtension
0 ignored issues
show
Bug introduced by
The type Nette\DI\CompilerExtension was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function getConfigSchema(): Nette\Schema\Schema
0 ignored issues
show
Bug introduced by
The type Nette\Schema\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
    {
33
        return Nette\Schema\Expect::structure([
34
            'driver' => Nette\Schema\Expect::anyOf(Expect::string(), Expect::object(), null),
35
        ]);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function loadConfiguration(): void
42
    {
43
        $builder = $this->getContainerBuilder();
44
45
        $cacheDefinition = $builder->addDefinition($this->prefix('psr16'))
46
            ->setFactory(Biurad\Cache\SimpleCache::class)
0 ignored issues
show
Bug introduced by
The type Biurad\Cache\Bridges\Net...iurad\Cache\SimpleCache was not found. Did you mean Biurad\Cache\SimpleCache? If so, make sure to prefix the type with \.
Loading history...
47
            ->setArguments([new Statement([AdapterFactory::class, 'createHandler'], ['array'])]);
48
49
        if (\extension_loaded('apcu')) {
50
            $cacheDefinition->setArgument(0, new Statement([AdapterFactory::class, 'createHandler'], ['apcu']));
51
        }
52
53
        $builder->addDefinition($this->prefix('psr6'))
54
            ->setFactory(Biurad\Cache\CacheItemPool::class);
0 ignored issues
show
Bug introduced by
The type Biurad\Cache\Bridges\Net...rad\Cache\CacheItemPool was not found. Did you mean Biurad\Cache\CacheItemPool? If so, make sure to prefix the type with \.
Loading history...
55
56
        $builder->addAlias('cache', $this->prefix('psr16'));
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function beforeCompile(): void
63
    {
64
        $builder = $this->getContainerBuilder();
65
        $adapter = $this->config->driver;
66
67
        foreach ($builder->findByType(CacheAdapterInterface::class) as $name => $definition) {
68
            if ($adapter && $adapter !== $definition->getEntity()::getName()) {
69
                $builder->removeDefinition($name);
70
71
                continue;
72
            }
73
74
            $adapter = $definition->getFactory();
75
            $builder->removeDefinition($name);
76
        }
77
78
        if (null !== $adapter) {
79
            $builder->getDefinition($this->prefix('psr16'))
80
                ->setArgument(0, new Statement([AdapterFactory::class, 'createHandler'], [$adapter]));
81
        }
82
    }
83
}
84