|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of php-cache organization. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Cache\AdapterBundle\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Cache\AdapterBundle\DummyAdapter; |
|
15
|
|
|
use Symfony\Component\Config\FileLocator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
20
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class CacheAdapterExtension extends Extension |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Loads the configs for Cache and puts data into the container. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $configs Array of configs |
|
31
|
|
|
* @param ContainerBuilder $container Container Object |
|
32
|
|
|
*/ |
|
33
|
6 |
|
public function load(array $configs, ContainerBuilder $container) |
|
34
|
|
|
{ |
|
35
|
6 |
|
$configuration = new Configuration(); |
|
36
|
6 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
37
|
|
|
|
|
38
|
6 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
39
|
6 |
|
$loader->load('services.yml'); |
|
40
|
|
|
|
|
41
|
|
|
// Configure client services |
|
42
|
6 |
|
$first = isset($config['providers']['default']) ? 'default' : null; |
|
43
|
6 |
|
foreach ($config['providers'] as $name => $arguments) { |
|
44
|
6 |
|
if (null === $first) { |
|
45
|
6 |
|
$first = $name; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
6 |
|
$factoryClass = $container->getDefinition($arguments['factory'])->getClass(); |
|
49
|
6 |
|
$factoryClass::validate($arguments['options'], $name); |
|
50
|
|
|
|
|
51
|
|
|
// See if any option has a service reference |
|
52
|
6 |
|
$arguments['options'] = $this->findReferences($arguments['options']); |
|
53
|
|
|
|
|
54
|
6 |
|
$def = $container->register('cache.provider.'.$name, DummyAdapter::class); |
|
55
|
6 |
|
$def->setFactory([new Reference($arguments['factory']), 'createAdapter']) |
|
56
|
6 |
|
->addArgument($arguments['options']) |
|
57
|
6 |
|
->setPublic(true); |
|
58
|
|
|
|
|
59
|
6 |
|
$def->addTag('cache.provider'); |
|
60
|
6 |
|
foreach ($arguments['aliases'] as $alias) { |
|
61
|
5 |
|
$container->setAlias($alias, new Alias('cache.provider.'.$name, true)); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
6 |
|
if (null !== $first) { |
|
66
|
6 |
|
$container->setAlias('cache', 'cache.provider.'.$first); |
|
67
|
6 |
|
$container->setAlias('php_cache', 'cache.provider.'.$first); |
|
68
|
|
|
} |
|
69
|
6 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param array $options |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
6 |
|
private function findReferences(array $options) |
|
77
|
|
|
{ |
|
78
|
6 |
|
foreach ($options as $key => $value) { |
|
79
|
3 |
|
if (is_array($value)) { |
|
80
|
3 |
|
$options[$key] = $this->findReferences($value); |
|
81
|
3 |
|
} elseif ('_service' === substr($key, -8) || 0 === strpos($value, '@') || 'service' === $key) { |
|
82
|
3 |
|
$options[$key] = new Reference(ltrim($value, '@')); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
return $options; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|