/
MetadataReaderCompilerPass.php
68 lines (59 loc) · 2.18 KB
/
MetadataReaderCompilerPass.php
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
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
/**
* Replaces the default exif-extension-based metadata reader with a degraded one if the exif extensions is not loaded.
*/
class MetadataReaderCompilerPass extends AbstractCompilerPass
{
/**
* @var string
*/
private static $metadataReaderServiceId = 'liip_imagine.meta_data.reader';
/**
* @var string
*/
private static $metadataReaderDefaultClass = 'Imagine\Image\Metadata\DefaultMetadataReader';
/**
* @var string
*/
private static $metadataReaderExifClass = 'Imagine\Image\Metadata\ExifMetadataReader';
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
if (!$this->isExifExtensionLoaded() && $this->isExifMetadataReaderSet($container)) {
$container->setDefinition(self::$metadataReaderServiceId, new Definition(self::$metadataReaderDefaultClass));
$message = 'Replaced the "%s" metadata reader service with "%s" from "%s" due to missing "exif" extension '.
'(as you may experience degraded metadata handling without the exif extension, installation is '.
'highly recommended)';
$this->log($container, $message, self::$metadataReaderServiceId, self::$metadataReaderDefaultClass, self::$metadataReaderExifClass);
}
}
/**
* @param ContainerBuilder $container
*
* @return bool
*/
private function isExifMetadataReaderSet(ContainerBuilder $container): bool
{
return $container->getDefinition(self::$metadataReaderServiceId)->getClass() === self::$metadataReaderExifClass;
}
/**
* @return bool
*/
protected function isExifExtensionLoaded(): bool
{
return extension_loaded('exif');
}
}