|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface; |
|
15
|
|
|
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface; |
|
16
|
|
|
use Symfony\Component\Config\FileLocator; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
21
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
22
|
|
|
|
|
23
|
|
|
class LiipImagineExtension extends Extension |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ResolverFactoryInterface[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $resolversFactories = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var LoaderFactoryInterface[] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $loadersFactories = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ResolverFactoryInterface $resolverFactory |
|
37
|
|
|
*/ |
|
38
|
|
|
public function addResolverFactory(ResolverFactoryInterface $resolverFactory) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->resolversFactories[$resolverFactory->getName()] = $resolverFactory; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param LoaderFactoryInterface $loaderFactory |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addLoaderFactory(LoaderFactoryInterface $loaderFactory) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->loadersFactories[$loaderFactory->getName()] = $loaderFactory; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getConfiguration(array $config, ContainerBuilder $container) |
|
55
|
|
|
{ |
|
56
|
|
|
return new Configuration($this->resolversFactories, $this->loadersFactories); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @see \Symfony\Component\DependencyInjection\Extension.ExtensionInterface::load() |
|
61
|
|
|
*/ |
|
62
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
63
|
|
|
{ |
|
64
|
|
|
$config = $this->processConfiguration( |
|
65
|
|
|
$this->getConfiguration($configs, $container), |
|
|
|
|
|
|
66
|
|
|
$configs |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$this->loadResolvers($config['resolvers'], $container); |
|
70
|
|
|
$this->loadLoaders($config['loaders'], $container); |
|
71
|
|
|
|
|
72
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
73
|
|
|
$loader->load('imagine.xml'); |
|
74
|
|
|
|
|
75
|
|
|
$container->getDefinition('liip_imagine.'.$config['driver'])->addMethodCall('setMetadataReader', [ |
|
76
|
|
|
new Reference('liip_imagine.meta_data.reader') |
|
77
|
|
|
]); |
|
78
|
|
|
|
|
79
|
|
|
$container->setAlias('liip_imagine', new Alias('liip_imagine.'.$config['driver'])); |
|
80
|
|
|
|
|
81
|
|
|
$container->setParameter('liip_imagine.cache.resolver.default', $config['cache']); |
|
82
|
|
|
|
|
83
|
|
|
$container->setParameter('liip_imagine.default_image', $config['default_image']); |
|
84
|
|
|
|
|
85
|
|
|
$container->setParameter('liip_imagine.filter_sets', $config['filter_sets']); |
|
86
|
|
|
$container->setParameter('liip_imagine.binary.loader.default', $config['data_loader']); |
|
87
|
|
|
|
|
88
|
|
|
$container->setParameter('liip_imagine.controller.filter_action', $config['controller']['filter_action']); |
|
89
|
|
|
$container->setParameter('liip_imagine.controller.filter_runtime_action', $config['controller']['filter_runtime_action']); |
|
90
|
|
|
|
|
91
|
|
|
$container->setParameter('twig.form.resources', array_merge( |
|
92
|
|
|
$container->hasParameter('twig.form.resources') ? $container->getParameter('twig.form.resources') : [], |
|
93
|
|
|
['LiipImagineBundle:Form:form_div_layout.html.twig'] |
|
94
|
|
|
)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param array $config |
|
99
|
|
|
* @param ContainerBuilder $container |
|
100
|
|
|
*/ |
|
101
|
|
|
private function loadResolvers(array $config, ContainerBuilder $container) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->createFactories($this->resolversFactories, $config, $container); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param array $config |
|
108
|
|
|
* @param ContainerBuilder $container |
|
109
|
|
|
*/ |
|
110
|
|
|
private function loadLoaders(array $config, ContainerBuilder $container) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->createFactories($this->loadersFactories, $config, $container); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param array $factories |
|
117
|
|
|
* @param array $configurations |
|
118
|
|
|
* @param ContainerBuilder $container |
|
119
|
|
|
*/ |
|
120
|
|
|
private function createFactories(array $factories, array $configurations, ContainerBuilder $container) |
|
121
|
|
|
{ |
|
122
|
|
|
foreach ($configurations as $name => $conf) { |
|
123
|
|
|
$factories[key($conf)]->create($container, $name, $conf[key($conf)]); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: