-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEzEnhancedImageAssetExtension.php
More file actions
117 lines (104 loc) · 3.97 KB
/
EzEnhancedImageAssetExtension.php
File metadata and controls
117 lines (104 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* NovaeZEnhancedImageAssetBundle.
*
* @package NovaeZEnhancedImageAssetBundle
*
* @author Novactive <f.alexandre@novactive.com>
* @copyright 2019 Novactive
* @license https://github.com/Novactive/NovaeZEnhancedImageAssetBundle/blob/master/LICENSE
*/
declare(strict_types=1);
namespace Novactive\EzEnhancedImageAssetBundle\DependencyInjection;
use Exception;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Yaml\Yaml;
/**
* Class EzEnhancedImageAssetExtension.
*
* @package Novactive\EzEnhancedImageAssetBundle\DependencyInjection
*/
class EzEnhancedImageAssetExtension extends Extension implements PrependExtensionInterface
{
/**
* {@inheritDoc}
*
* @throws Exception
* @throws Exception
* @throws Exception
* @throws Exception
* @throws Exception
* @throws Exception
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load('default_settings.yml');
$loader->load('fieldtypes.yml');
$loader->load('field_value_converters.yml');
$loader->load('migration.yml');
$loader->load('ezadminui/components.yml');
}
/**
* Allow an extension to prepend the extension configurations.
*/
public function prepend(ContainerBuilder $container): void
{
$container->prependExtensionConfig(
'assetic',
[
'bundles' => [
'EzEnhancedImageAssetBundle',
],
]
);
$configs = [
'field_templates.yml' => 'ezpublish',
'admin_ui_forms.yml' => 'ezpublish',
'image_variations.yml' => 'ezpublish',
'twig.yml' => 'twig',
];
$activatedBundles = array_keys($container->getParameter('kernel.bundles'));
if (in_array('EzPlatformAdminUiBundle', $activatedBundles, true)) {
$configs['ezadminui/twig.yml'] = 'twig';
}
foreach ($configs as $fileName => $extensionName) {
$configFile = __DIR__.'/../Resources/config/'.$fileName;
$config = Yaml::parse((string) file_get_contents($configFile));
$container->prependExtensionConfig($extensionName, $config);
$container->addResource(new FileResource($configFile));
}
$configs = $container->getExtensionConfig('ezpublish');
$newConfig = [];
foreach ($configs as $config) {
if (!isset($config['system'])) {
continue;
}
foreach ($config['system'] as $system => $systemConfig) {
if (!isset($systemConfig['image_variations'])) {
continue;
}
foreach (array_keys($systemConfig['image_variations']) as $imageVariation) {
if (false !== strpos($imageVariation, '_retina')) {
$webpVariationName = preg_replace('/^(.+)(_retina)$/', '$1_webp$2', $imageVariation);
} else {
$webpVariationName = $imageVariation.'_webp';
}
$newConfig['system'][$system]['image_variations'][$webpVariationName] = [
'reference' => $imageVariation,
'filters' => [
['name' => 'toFormat', 'params' => ['format' => 'webp']],
],
];
}
}
}
$container->prependExtensionConfig('ezpublish', $newConfig);
}
}