Completed
Push — master ( 1758db...0d3940 )
by Jordi Sala
02:09
created

Compiler/AddProviderCompilerPass.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
 * @final since sonata-project/media-bundle 3.21.0
25
 *
26
 * @author Thomas Rabaix <[email protected]>
27
 */
28
class AddProviderCompilerPass implements CompilerPassInterface
29
{
30
    public function process(ContainerBuilder $container): void
31
    {
32
        $config = $this->getExtensionConfig($container);
33
34
        // define configuration per provider
35
        $this->applyFormats($container, $config);
36
        $this->attachArguments($container, $config);
37
        $this->attachProviders($container);
38
39
        $format = $container->getParameter('sonata.media.admin_format');
40
41
        foreach ($container->findTaggedServiceIds('sonata.media.provider') as $id => $attributes) {
42
            $container->getDefinition($id)->addMethodCall(
43
                'addFormat',
44
                [MediaProviderInterface::FORMAT_ADMIN, $format]
45
            );
46
        }
47
    }
48
49
    /**
50
     * NEXT_MAJOR: Remove this method.
51
     *
52
     * @return array
53
     */
54
    public function fixSettings(ContainerBuilder $container)
55
    {
56
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
57
            'The '.__METHOD__.' method is deprecated since 3.5, to be removed in 4.0.',
58
            E_USER_DEPRECATED
59
        );
60
61
        return $this->getExtensionConfig($container);
62
    }
63
64
    public function attachProviders(ContainerBuilder $container): void
65
    {
66
        $pool = $container->getDefinition('sonata.media.pool');
67
        foreach ($container->findTaggedServiceIds('sonata.media.provider') as $id => $attributes) {
68
            $pool->addMethodCall('addProvider', [$id, new Reference($id)]);
69
        }
70
    }
71
72
    public function attachArguments(ContainerBuilder $container, array $settings): void
73
    {
74
        foreach ($container->findTaggedServiceIds('sonata.media.provider') as $id => $attributes) {
75
            foreach ($settings['providers'] as $name => $config) {
76
                if ($config['service'] === $id) {
77
                    $definition = $container->getDefinition($id);
78
79
                    $definition
80
                        ->replaceArgument(1, new Reference($config['filesystem']))
81
                        ->replaceArgument(2, new Reference($config['cdn']))
82
                        ->replaceArgument(3, new Reference($config['generator']))
83
                        ->replaceArgument(4, new Reference($config['thumbnail']))
84
                    ;
85
86
                    if ($config['resizer']) {
87
                        $definition->addMethodCall('setResizer', [new Reference($config['resizer'])]);
88
                    }
89
                }
90
            }
91
        }
92
    }
93
94
    /**
95
     * Define the default settings to the config array.
96
     */
97
    public function applyFormats(ContainerBuilder $container, array $settings): void
98
    {
99
        foreach ($settings['contexts'] as $name => $context) {
100
            // add the different related formats
101
            foreach ($context['providers'] as $id) {
102
                $definition = $container->getDefinition($id);
103
104
                foreach ($context['formats'] as $format => $config) {
105
                    $config['quality'] = $config['quality'] ?? 80;
106
                    $config['format'] = $config['format'] ?? 'jpg';
107
                    $config['height'] = $config['height'] ?? null;
108
                    $config['constraint'] = $config['constraint'] ?? true;
109
                    $config['resizer'] = $config['resizer'] ?? false;
110
111
                    $formatName = sprintf('%s_%s', $name, $format);
112
                    $definition->addMethodCall('addFormat', [$formatName, $config]);
113
                }
114
            }
115
        }
116
    }
117
118
    private function getExtensionConfig(ContainerBuilder $container): array
119
    {
120
        $config = $container->getExtensionConfig('sonata_media');
121
        $config = $container->getParameterBag()->resolveValue($config);
122
        $processor = new Processor();
123
124
        return $processor->processConfiguration(new Configuration(), $config);
125
    }
126
}
127