|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\DependencyInjection\Compiler; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\MediaBundle\DependencyInjection\Configuration; |
|
17
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
|
18
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Thomas Rabaix <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class AddProviderCompilerPass implements CompilerPassInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function process(ContainerBuilder $container): void |
|
32
|
|
|
{ |
|
33
|
|
|
$config = $this->getExtensionConfig($container); |
|
34
|
|
|
|
|
35
|
|
|
// define configuration per provider |
|
36
|
|
|
$this->applyFormats($container, $config); |
|
37
|
|
|
$this->attachArguments($container, $config); |
|
38
|
|
|
$this->attachProviders($container); |
|
39
|
|
|
|
|
40
|
|
|
$format = $container->getParameter('sonata.media.admin_format'); |
|
41
|
|
|
|
|
42
|
|
|
foreach ($container->findTaggedServiceIds('sonata.media.provider') as $id => $attributes) { |
|
43
|
|
|
$container->getDefinition($id)->addMethodCall( |
|
44
|
|
|
'addFormat', |
|
45
|
|
|
[MediaProviderInterface::FORMAT_ADMIN, $format] |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* NEXT_MAJOR: Remove this method. |
|
52
|
|
|
* |
|
53
|
|
|
* @param ContainerBuilder $container |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function fixSettings(ContainerBuilder $container) |
|
58
|
|
|
{ |
|
59
|
|
|
@trigger_error( |
|
|
|
|
|
|
60
|
|
|
'The '.__METHOD__.' method is deprecated since 3.5, to be removed in 4.0.', |
|
61
|
|
|
E_USER_DEPRECATED |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
return $this->getExtensionConfig($container); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param ContainerBuilder $container |
|
69
|
|
|
*/ |
|
70
|
|
|
public function attachProviders(ContainerBuilder $container): void |
|
71
|
|
|
{ |
|
72
|
|
|
$pool = $container->getDefinition('sonata.media.pool'); |
|
73
|
|
|
foreach ($container->findTaggedServiceIds('sonata.media.provider') as $id => $attributes) { |
|
74
|
|
|
$pool->addMethodCall('addProvider', [$id, new Reference($id)]); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param ContainerBuilder $container |
|
80
|
|
|
* @param array $settings |
|
81
|
|
|
*/ |
|
82
|
|
|
public function attachArguments(ContainerBuilder $container, array $settings): void |
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($container->findTaggedServiceIds('sonata.media.provider') as $id => $attributes) { |
|
85
|
|
|
foreach ($settings['providers'] as $name => $config) { |
|
86
|
|
|
if ($config['service'] == $id) { |
|
87
|
|
|
$definition = $container->getDefinition($id); |
|
88
|
|
|
|
|
89
|
|
|
$definition |
|
90
|
|
|
->replaceArgument(1, new Reference($config['filesystem'])) |
|
91
|
|
|
->replaceArgument(2, new Reference($config['cdn'])) |
|
92
|
|
|
->replaceArgument(3, new Reference($config['generator'])) |
|
93
|
|
|
->replaceArgument(4, new Reference($config['thumbnail'])) |
|
94
|
|
|
; |
|
95
|
|
|
|
|
96
|
|
|
if ($config['resizer']) { |
|
97
|
|
|
$definition->addMethodCall('setResizer', [new Reference($config['resizer'])]); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Define the default settings to the config array. |
|
106
|
|
|
* |
|
107
|
|
|
* @param ContainerBuilder $container |
|
108
|
|
|
* @param array $settings |
|
109
|
|
|
*/ |
|
110
|
|
|
public function applyFormats(ContainerBuilder $container, array $settings): void |
|
111
|
|
|
{ |
|
112
|
|
|
foreach ($settings['contexts'] as $name => $context) { |
|
113
|
|
|
// add the different related formats |
|
114
|
|
|
foreach ($context['providers'] as $id) { |
|
115
|
|
|
$definition = $container->getDefinition($id); |
|
116
|
|
|
|
|
117
|
|
|
foreach ($context['formats'] as $format => $config) { |
|
118
|
|
|
$config['quality'] = $config['quality'] ?? 80; |
|
119
|
|
|
$config['format'] = $config['format'] ?? 'jpg'; |
|
120
|
|
|
$config['height'] = $config['height'] ?? false; |
|
121
|
|
|
$config['constraint'] = $config['constraint'] ?? true; |
|
122
|
|
|
|
|
123
|
|
|
$formatName = sprintf('%s_%s', $name, $format); |
|
124
|
|
|
$definition->addMethodCall('addFormat', [$formatName, $config]); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param ContainerBuilder $container |
|
132
|
|
|
* |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
private function getExtensionConfig(ContainerBuilder $container) |
|
136
|
|
|
{ |
|
137
|
|
|
$config = $container->getExtensionConfig('sonata_media'); |
|
138
|
|
|
$processor = new Processor(); |
|
139
|
|
|
|
|
140
|
|
|
return $processor->processConfiguration(new Configuration(), $config); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: