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\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* By default, a metadata reader based on the exif php extension is used. |
19
|
|
|
* This compiler pass checks if the extension is loaded an switches to a simpler |
20
|
|
|
* implementation if not. |
21
|
|
|
*/ |
22
|
|
|
class MetadataReaderCompilerPass implements CompilerPassInterface |
23
|
|
|
{ |
24
|
|
|
const METADATA_READER_PARAM = 'liip_imagine.meta_data.reader.class'; |
25
|
|
|
|
26
|
|
|
const DEFAULT_METADATA_READER_CLASS = 'Imagine\Image\Metadata\DefaultMetadataReader'; |
27
|
|
|
|
28
|
|
|
const EXIF_METADATA_READER_CLASS = 'Imagine\Image\Metadata\ExifMetadataReader'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function process(ContainerBuilder $container) |
34
|
|
|
{ |
35
|
|
|
if (!$this->extExifIsAvailable() && $this->isDefaultMetadataReader($container)) { |
36
|
|
|
$container->setParameter(self::METADATA_READER_PARAM, self::DEFAULT_METADATA_READER_CLASS); |
37
|
|
|
$this->logMetadataReaderReplaced($container); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ContainerBuilder $container |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
protected function isDefaultMetadataReader(ContainerBuilder $container) |
47
|
|
|
{ |
48
|
|
|
$currentMetadataReaderParameter = $container->getParameter(self::METADATA_READER_PARAM); |
49
|
|
|
|
50
|
|
|
return $currentMetadataReaderParameter === self::EXIF_METADATA_READER_CLASS; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param ContainerBuilder $container |
55
|
|
|
*/ |
56
|
|
|
protected function logMetadataReaderReplaced(ContainerBuilder $container) |
57
|
|
|
{ |
58
|
|
|
$compiler = $container->getCompiler(); |
59
|
|
|
$formatter = $compiler->getLoggingFormatter(); |
60
|
|
|
$message = 'Automatically replaced Imagine ExifMetadataReader with DefaultMetadataReader; '. |
61
|
|
|
'you might experience issues with LiipImagineBundle; reason: PHP extension "exif" is missing; solution: '. |
62
|
|
|
'for advanced metadata extraction install the PHP extension "exif" or set a custom MetadataReader '. |
63
|
|
|
'through the "liip_imagine.meta_data.reader.class" parameter'; |
64
|
|
|
|
65
|
|
|
$compiler->addLogMessage($formatter->format($this, $message)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
protected function extExifIsAvailable() |
72
|
|
|
{ |
73
|
|
|
return extension_loaded('exif'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|