1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\SerializerBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Cache\Psr16Cache; |
6
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
10
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* SerializerExtension. |
15
|
|
|
*/ |
16
|
|
|
class BdfSerializerExtension extends Extension |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritDoc} |
20
|
|
|
*/ |
21
|
5 |
|
public function load(array $configs, ContainerBuilder $container) |
22
|
|
|
{ |
23
|
5 |
|
$configuration = $this->getConfiguration($configs, $container); |
24
|
5 |
|
$config = $this->processConfiguration($configuration, $configs); |
25
|
|
|
|
26
|
5 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
27
|
5 |
|
$loader->load('serializer.yaml'); |
28
|
|
|
|
29
|
5 |
|
$this->configureCache($config, $container); |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
public function configureCache(array $config, ContainerBuilder $container) |
33
|
|
|
{ |
34
|
5 |
|
if (isset($config['cache'])) { |
35
|
2 |
|
$ref = $this->createCacheReference('bdf_serializer.cache', $config['cache'], $container); |
36
|
|
|
|
37
|
2 |
|
if (null !== $ref) { |
38
|
2 |
|
$serializerDefinition = $container->getDefinition('bdf_serializer.metadata_factory'); |
39
|
2 |
|
$serializerDefinition->replaceArgument(1, $ref); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
private function createCacheReference(string $namespace, array $config, ContainerBuilder $container): ?Reference |
45
|
|
|
{ |
46
|
2 |
|
if (isset($config['service'])) { |
47
|
1 |
|
return new Reference($config['service']); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
if (isset($config['pool'])) { |
51
|
1 |
|
$definition = $container->register($namespace, Psr16Cache::class); |
52
|
1 |
|
$definition->addArgument(new Reference($config['pool'])); |
53
|
|
|
|
54
|
1 |
|
return new Reference($namespace); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
5 |
|
public function getConfiguration(array $config, ContainerBuilder $container): ?ConfigurationInterface |
64
|
|
|
{ |
65
|
5 |
|
return new Configuration(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|