/
AddImagineClassPass.php
60 lines (51 loc) · 1.36 KB
/
AddImagineClassPass.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
<?php
/*
* This file is part of Contao.
*
* Copyright (c) 2005-2017 Leo Feyer
*
* @license LGPL-3.0+
*/
namespace Contao\CoreBundle\DependencyInjection\Compiler;
use Imagine\Exception\RuntimeException;
use Imagine\Gd\Imagine;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Sets the available Imagine class name in the container.
*
* @author Martin Auswöger <martin@auswoeger.com>
*/
class AddImagineClassPass implements CompilerPassInterface
{
/**
* @var array
*/
private $magicks = ['Gmagick', 'Imagick'];
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$container->getDefinition('contao.image.imagine')->setClass($this->getImagineImplementation());
}
/**
* Returns the available Imagine implementation.
*
* @return string
*/
private function getImagineImplementation()
{
foreach ($this->magicks as $name) {
$class = 'Imagine\\'.$name.'\Imagine';
// Will throw an exception if the PHP implementation is not available
try {
new $class();
} catch (RuntimeException $e) {
continue;
}
return $class;
}
return Imagine::class; // see #616
}
}