isExifExtensionLoaded()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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\Compiler;
13
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\Definition;
16
17
/**
18
 * Replaces the default exif-extension-based metadata reader with a degraded one if the exif extensions is not loaded.
19
 */
20
class MetadataReaderCompilerPass extends AbstractCompilerPass
21
{
22
    /**
23
     * @var string
24
     */
25
    private static $metadataReaderServiceId = 'liip_imagine.meta_data.reader';
26
27
    /**
28
     * @var string
29
     */
30
    private static $metadataReaderDefaultClass = 'Imagine\Image\Metadata\DefaultMetadataReader';
31
32
    /**
33
     * @var string
34
     */
35
    private static $metadataReaderExifClass = 'Imagine\Image\Metadata\ExifMetadataReader';
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function process(ContainerBuilder $container): void
41
    {
42
        if (!$this->isExifExtensionLoaded() && $this->isExifMetadataReaderSet($container)) {
43
            $container->setDefinition(self::$metadataReaderServiceId, new Definition(self::$metadataReaderDefaultClass));
44
            $message = 'Replaced the "%s" metadata reader service with "%s" from "%s" due to missing "exif" extension '.
45
                       '(as you may experience degraded metadata handling without the exif extension, installation is '.
46
                       'highly recommended)';
47
            $this->log($container, $message, self::$metadataReaderServiceId, self::$metadataReaderDefaultClass, self::$metadataReaderExifClass);
48
        }
49
    }
50
51
    protected function isExifExtensionLoaded(): bool
52
    {
53
        return \extension_loaded('exif');
54
    }
55
56
    private function isExifMetadataReaderSet(ContainerBuilder $container): bool
57
    {
58
        return $container->getDefinition(self::$metadataReaderServiceId)->getClass() === self::$metadataReaderExifClass;
59
    }
60
}
61