Completed
Pull Request — master (#5)
by Kevin
04:25
created

ZenstruckAssetManifestExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 0
loc 42
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadInternal() 0 14 2
A loadFromFile() 0 14 3
1
<?php
2
3
namespace Zenstruck\AssetManifestBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 */
14
final class ZenstruckAssetManifestExtension extends ConfigurableExtension
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
20
    {
21
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
22
        $loader->load('twig.xml');
23
24
        $manifest = [];
25
        $manifestFile = $mergedConfig['manifest_file'];
26
27
        if ($manifestFile !== null) {
28
            $manifest = $this->loadFromFile($manifestFile, $mergedConfig['prefix']['source'], $mergedConfig['prefix']['destination']);
29
        }
30
31
        $container->getDefinition('zenstruck_asset_manifest.twig_extension')->replaceArgument(0, $manifest);
32
    }
33
34
    /**
35
     * @param string $file
36
     * @param string $sourcePrefix
37
     * @param string $destPrefix
38
     *
39
     * @return array
40
     */
41
    private function loadFromFile($file, $sourcePrefix, $destPrefix)
42
    {
43
        if (!file_exists($file)) {
44
            throw new InvalidConfigurationException(sprintf('Manifest file "%s" does not exist.', $file));
45
        }
46
47
        $manifest = [];
48
49
        foreach (json_decode(file_get_contents($file), true) as $key => $value) {
50
            $manifest[$sourcePrefix.$key] = $destPrefix.$value;
51
        }
52
53
        return $manifest;
54
    }
55
}
56