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; |
||
19 | |||
20 | use Biurad\Cache\AdapterFactory; |
||
21 | use Biurad\Cache\Interfaces\CacheAdapterInterface; |
||
22 | use Nette; |
||
23 | use Nette\DI\Definitions\Statement; |
||
24 | use Nette\Schema\Expect; |
||
25 | |||
26 | class CacheExtension extends Nette\DI\CompilerExtension |
||
27 | { |
||
28 | /** |
||
29 | * {@inheritDoc} |
||
30 | */ |
||
31 | public function getConfigSchema(): Nette\Schema\Schema |
||
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
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
|
|||
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 |