Completed
Pull Request — master (#32)
by Paulo Rodrigues
11:47 queued 10:15
created

AssetExtensionLoader::load()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 18
cts 18
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 4
nop 2
crap 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Rj\FrontendBundle\DependencyInjection;
4
5
if (!class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
6
    class_alias(
7
        'Symfony\Component\DependencyInjection\DefinitionDecorator',
8
        'Symfony\Component\DependencyInjection\ChildDefinition'
9
    );
10
}
11
12
use Rj\FrontendBundle\Util\Util;
13
use Symfony\Component\Config\Loader\LoaderInterface;
14
use Symfony\Component\DependencyInjection\ChildDefinition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
class AssetExtensionLoader
20
{
21
    /**
22
     * @var string
23
     */
24
    private $alias;
25
26
    /**
27
     * @var ContainerBuilder
28
     */
29
    private $container;
30
31
    /**
32
     * @param string           $alias
33
     * @param ContainerBuilder $container
34
     */
35 20
    public function __construct($alias, ContainerBuilder $container)
36
    {
37 20
        $this->alias = $alias;
38 20
        $this->container = $container;
39 20
    }
40
41
    /**
42
     * @param array           $config
43
     * @param LoaderInterface $loader
44
     */
45 20
    public function load(array $config, LoaderInterface $loader)
46
    {
47 20
        $loader->load('asset.yml');
48
49 20
        if ($config['override_default_package']) {
50 19
            $loader->load('fallback.yml');
51
52 19
            $defaultPackage = $this->createPackage('default', [
53 19
                'prefix' => $config['prefix'],
54 19
                'manifest' => $config['manifest'],
55
            ]);
56
57 19
            $defaultPackageId = $this->getPackageId('default');
58 19
            $this->container->setDefinition($defaultPackageId, $defaultPackage);
59
60 19
            $this->container->getDefinition($this->namespaceService('package.fallback'))
61 19
                ->addArgument($config['fallback_patterns'])
62 19
                ->addArgument(new Reference($defaultPackageId));
63
        }
64
65 20
        foreach ($config['packages'] as $name => $packageConfig) {
66 10
            $packageTag = $this->namespaceService('package.asset');
67 10
            $package = $this->createPackage($name, $packageConfig)
68 10
                ->addTag($packageTag, ['alias' => $name]);
69
70 10
            $this->container->setDefinition($this->getPackageId($name), $package);
71
        }
72 20
    }
73
74
    /**
75
     * @param string $name
76
     * @param array  $config
77
     *
78
     * @return Definition
79
     */
80 19
    private function createPackage($name, array $config)
81
    {
82 19
        $prefixes = $config['prefix'];
83 19
        $isUrl = Util::containsUrl($prefixes);
84
85 19
        $packageDefinition = $isUrl
86 4
            ? new ChildDefinition($this->namespaceService('asset.package.url'))
87 19
            : new ChildDefinition($this->namespaceService('asset.package.path'))
88
        ;
89
90 19
        if ($config['manifest']['enabled']) {
91 4
            $versionStrategy = $this->createManifestVersionStrategy($name, $config['manifest']);
92
        } else {
93 17
            $versionStrategy = new Reference($this->namespaceService('version_strategy.empty'));
94
        }
95
96
        return $packageDefinition
97 19
            ->addArgument($isUrl ? $prefixes : $prefixes[0])
98 19
            ->addArgument($versionStrategy)
99 19
            ->setPublic(false);
100
    }
101
102
    /**
103
     * @param string $name
104
     *
105
     * @return string
106
     */
107 19
    private function getPackageId($name)
108
    {
109 19
        return $this->namespaceService("_package.$name");
110
    }
111
112
    /**
113
     * @param string $packageName
114
     * @param array  $config
115
     *
116
     * @return Reference
117
     */
118 4
    private function createManifestVersionStrategy($packageName, $config)
119
    {
120 4
        $loader = new ChildDefinition($this->namespaceService('manifest.loader.'.$config['format']));
121
        $loader
122 4
            ->addArgument($config['path'])
123 4
            ->addArgument($config['root_key'])
124
        ;
125
126 4
        $loaderId = $this->namespaceService("_package.$packageName.manifest_loader");
127 4
        $this->container->setDefinition($loaderId, $loader);
128
129 4
        $cachedLoader = new ChildDefinition($this->namespaceService('manifest.loader.cached'));
130 4
        $cachedLoader->addArgument(new Reference($loaderId));
131
132 4
        $cachedLoaderId = $this->namespaceService("_package.$packageName.manifest_loader_cached");
133 4
        $this->container->setDefinition($cachedLoaderId, $cachedLoader);
134
135 4
        $versionStrategy = new ChildDefinition($this->namespaceService('version_strategy.manifest'));
136 4
        $versionStrategy->addArgument(new Reference($cachedLoaderId));
137
138 4
        $versionStrategyId = $this->namespaceService("_package.$packageName.version_strategy");
139 4
        $this->container->setDefinition($versionStrategyId, $versionStrategy);
140
141 4
        return new Reference($versionStrategyId);
142
    }
143
144
    /**
145
     * @param string $id
146
     *
147
     * @return string
148
     */
149 19
    private function namespaceService($id)
150
    {
151 19
        return $this->alias.'.'.$id;
152
    }
153
}
154