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