ElaoApiResourcesMetadataExtension::load()   B
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.439
c 0
b 0
f 0
cc 5
eloc 26
nc 9
nop 2
1
<?php
2
3
/*
4
 * This file is part of the "elao/api-resources-metadata" package.
5
 *
6
 * Copyright (C) 2016 Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\ApiResourcesMetadata\Bridge\Symfony\Bundle\DependencyInjection;
12
13
use Nelmio\ApiDocBundle\NelmioApiDocBundle;
14
use Symfony\Component\Cache\Adapter\AdapterInterface;
15
use Symfony\Component\Cache\Adapter\ApcuAdapter;
16
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
21
use Symfony\Component\DependencyInjection\Reference;
22
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
23
24
class ElaoApiResourcesMetadataExtension extends Extension
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function load(array $configs, ContainerBuilder $container)
30
    {
31
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
32
        $loader->load('services.xml');
33
34
        $configuration = $this->getConfiguration($configs, $container);
35
        $config = $this->processConfiguration($configuration, $configs);
36
37
        $arrayLocatorDef = $container->findDefinition('elao.api_resources_metadata.array_locator');
38
        $arrayLocatorDef->replaceArgument(0, $config['resources']);
39
40
        if ($config['nelmio_api_doc']) {
41
            $bundles = $container->getParameter('kernel.bundles');
42
            if (!isset($bundles['NelmioApiDocBundle'])) {
43
                throw new \RuntimeException(sprintf(
44
                    'Cannot enabled the NelmioApiDoc integration without the %s registered in your kernel.',
45
                    NelmioApiDocBundle::class
46
                ));
47
            }
48
49
            $loader->load('nelmio_api_doc.xml');
50
        }
51
52
        if ($config['loaders']['yaml']['enabled']) {
53
            $loader->load('yaml.xml');
54
55
            $path = $config['loaders']['yaml']['path'];
56
57
            $loaderDef = $container->findDefinition('elao.api_resources_metadata.yaml_file_loader');
58
            $loaderDef->replaceArgument(0, $path);
59
60
            $locatorDef = $container->findDefinition('elao.api_resources_metadata.yaml_file_locator');
61
            $locatorDef->replaceArgument(0, $path);
62
        }
63
64
        if ($config['loaders']['property_info']['enabled']) {
65
            $loader->load('property_info.xml');
66
            $def = $container->findDefinition('elao.api_resources_metadata.property_info_loader');
67
            $def->replaceArgument(0, new Reference($config['loaders']['property_info']['service']));
68
        }
69
70
        $this->registerCache($container, $config);
71
    }
72
73
    private function registerCache(ContainerBuilder $container, $config)
74
    {
75
        if (isset($config['cache'])) {
76
            if (false === $config['cache']) {
77
                return;
78
            }
79
80
            if (true === $config['cache']) {
81
                return $this->configureDefaultCache($container);
82
            }
83
84
            $container->setAlias('elao.api_resources_metadata.cache', $config['cache']);
85
86
            return;
87
        }
88
89
        if ($container->getParameter('kernel.debug') || !interface_exists(AdapterInterface::class)) {
90
            return;
91
        }
92
93
        $this->configureDefaultCache($container);
94
    }
95
96
    private function configureDefaultCache(ContainerBuilder $container)
97
    {
98
        $version = substr(str_replace('/', '-', base64_encode(md5(uniqid(mt_rand(), true), true))), 0, -2);
99
100
        if (ApcuAdapter::isSupported()) {
101
            $cacheDef = $container
102
                ->register($cacheId = 'elao.api_resources_metadata.cache.apcu', ApcuAdapter::class)
103
                ->setArguments([null, 0, $version])
104
            ;
105
        }
106
107
        if (!isset($cacheDef)) {
108
            $cacheDir = $container->getParameter('kernel.cache_dir') . '/elao.api_resources_meta';
109
            $cacheDef = $container
110
                ->register($cacheId = 'elao.api_resources_metadata.cache.filesystem', FilesystemAdapter::class)
111
                ->setArguments([null, 0, $cacheDir])
112
            ;
113
        }
114
115
        $cacheDef
116
            ->setPublic(false)
117
            ->addMethodCall('setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)])
118
            ->addTag('cache.pool', ['clearer' => 'cache.default_clearer'])
119
            ->addTag('monolog.logger', ['channel' => 'cache'])
120
        ;
121
122
        $container->setAlias('elao.api_resources_metadata.cache', $cacheId);
123
    }
124
}
125