1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
11
|
|
|
|
12
|
|
|
class BankiruDoctrineApiExtension extends Extension |
13
|
|
|
{ |
14
|
|
|
/** {@inheritdoc} */ |
15
|
6 |
|
public function load(array $configs, ContainerBuilder $container) |
16
|
|
|
{ |
17
|
6 |
|
$config = $this->processConfiguration(new Configuration(), $configs); |
18
|
|
|
|
19
|
6 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
20
|
6 |
|
$loader->load('api.yml'); |
21
|
|
|
|
22
|
6 |
|
if ($container->hasParameter('kernel.environment') && |
23
|
5 |
|
$container->getParameter('kernel.environment') === 'test' |
24
|
6 |
|
) { |
25
|
5 |
|
$loader->load('test.yml'); |
26
|
5 |
|
} |
27
|
|
|
|
28
|
6 |
|
if ($this->isConfigEnabled($container, $config['logger'])) { |
29
|
2 |
|
$container->setParameter('bankiru_api.logger.service', $config['logger']['service']); |
30
|
2 |
|
$container->setParameter('bankiru_api.logger.debug_body', $config['logger']['debug_body']); |
31
|
2 |
|
} |
32
|
|
|
|
33
|
6 |
|
$container->setParameter('bankiru_api.profiler_enabled', $config['profiling']); |
34
|
|
|
|
35
|
6 |
|
$configuration = $container->getDefinition('bankiru_api.configuration'); |
36
|
6 |
|
if ($this->isConfigEnabled($container, $config['entity_cache'])) { |
37
|
1 |
|
if (null === $config['entity_cache']['service']) { |
38
|
|
|
throw new \LogicException('You should specify PSR-6 cache service in order to enable caching'); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$configuration->addMethodCall('setApiCache', [new Reference($config['entity_cache']['service'])]); |
42
|
1 |
|
if ($config['entity_cache']['logger'] !== null) { |
43
|
1 |
|
$configuration->addMethodCall('setApiCacheLogger', [new Reference($config['entity_cache']['logger'])]); |
44
|
1 |
|
} |
45
|
1 |
|
} |
46
|
|
|
|
47
|
6 |
|
if ($this->isConfigEnabled($container, $config['metadata_cache'])) { |
48
|
|
|
$container->getDefinition('bankiru_api.metadata_factory') |
49
|
|
|
->addMethodCall('setCacheDriver', [new Reference($config['metadata_cache']['service'])]); |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
$this->processSensioExtraConfig($container, $loader); |
53
|
|
|
|
54
|
6 |
|
foreach ($config['entity_cache']['configuration'] as $class => $options) { |
55
|
1 |
|
assert(array_key_exists('enabled', $options)); |
56
|
1 |
|
assert(array_key_exists('ttl', $options)); |
57
|
1 |
|
$configuration->addMethodCall('setCacheConfiguration', [$class, $options]); |
58
|
6 |
|
} |
59
|
6 |
|
} |
60
|
|
|
|
61
|
5 |
|
public function getAlias() |
62
|
|
|
{ |
63
|
5 |
|
return 'api_client'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ContainerBuilder $container |
68
|
|
|
* @param YamlFileLoader $loader |
69
|
|
|
*/ |
70
|
6 |
|
private function processSensioExtraConfig(ContainerBuilder $container, YamlFileLoader $loader) |
71
|
|
|
{ |
72
|
6 |
|
if (!$container->hasParameter('kernel.bundles')) { |
73
|
1 |
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
5 |
|
if (!class_exists('Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle')) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
5 |
|
if (in_array(SensioFrameworkExtraBundle::class, $container->getParameter('kernel.bundles'))) { |
81
|
1 |
|
$loader->load('sensio.yml'); |
82
|
1 |
|
} |
83
|
5 |
|
} |
84
|
|
|
} |
85
|
|
|
|