Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set DefaultMetadataReader when ext-exif is not present #841

Merged
merged 1 commit into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions DependencyInjection/Compiler/MetadataReaderCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* By default, a metadata reader based on the exif php extension is used.
* This compiler pass checks if the extension is loaded an switches to a simpler
* implementation if not.
*/
class MetadataReaderCompilerPass implements CompilerPassInterface
{
const METADATA_READER_PARAM = 'liip_imagine.meta_data.reader.class';

const DEFAULT_METADATA_READER_CLASS = 'Imagine\Image\Metadata\DefaultMetadataReader';

const EXIF_METADATA_READER_CLASS = 'Imagine\Image\Metadata\ExifMetadataReader';

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$this->extExifIsAvailable() && $this->isDefaultMetadataReader($container)) {
$container->setParameter(self::METADATA_READER_PARAM, self::DEFAULT_METADATA_READER_CLASS);
$this->logMetadataReaderReplaced($container);
}
}

/**
* @param ContainerBuilder $container
*
* @return bool
*/
protected function isDefaultMetadataReader(ContainerBuilder $container)
{
$currentMetadataReaderParameter = $container->getParameter(self::METADATA_READER_PARAM);

return $currentMetadataReaderParameter === self::EXIF_METADATA_READER_CLASS;
}

/**
* @param ContainerBuilder $container
*/
protected function logMetadataReaderReplaced(ContainerBuilder $container)
{
$compiler = $container->getCompiler();
$formatter = $compiler->getLoggingFormatter();
$message = 'Automatically replaced Imagine ExifMetadataReader with DefaultMetadataReader; '.
'you might experience issues with LiipImagineBundle; reason: PHP extension "exif" is missing; solution: '.
'for advanced metadata extraction install the PHP extension "exif" or set a custom MetadataReader '.
'through the "liip_imagine.meta_data.reader.class" parameter';

$compiler->addLogMessage($formatter->format($this, $message));
}

/**
* @return bool
*/
protected function extExifIsAvailable()
{
return extension_loaded('exif');
}
}
2 changes: 2 additions & 0 deletions LiipImagineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Liip\ImagineBundle\DependencyInjection\Compiler\FiltersCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\LoadersCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\PostProcessorsCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\ResolversCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FileSystemLoaderFactory;
Expand All @@ -38,6 +39,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new FiltersCompilerPass());
$container->addCompilerPass(new PostProcessorsCompilerPass());
$container->addCompilerPass(new ResolversCompilerPass());
$container->addCompilerPass(new MetadataReaderCompilerPass());

/** @var $extension LiipImagineExtension */
$extension = $container->getExtension('liip_imagine');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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\Tests\DependencyInjection\Compiler;

use Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @covers Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass
*/
class MetadataReaderCompilerPassTest extends \PHPUnit_Framework_TestCase
{
public function testProcessWithoutExtExifAddsDefaultReader()
{
$container = new ContainerBuilder();
$container->setParameter(
MetadataReaderCompilerPass::METADATA_READER_PARAM,
MetadataReaderCompilerPass::EXIF_METADATA_READER_CLASS
);

/** @var MetadataReaderCompilerPass $pass */
$pass = $this->getMock(
'Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass',
array('extExifIsAvailable'),
array()
);
$pass->expects($this->once())
->method('extExifIsAvailable')
->willReturn(false);

//guard
$this->assertEquals(
MetadataReaderCompilerPass::EXIF_METADATA_READER_CLASS,
$container->getParameter(MetadataReaderCompilerPass::METADATA_READER_PARAM)
);

$pass->process($container);

$this->assertEquals(
MetadataReaderCompilerPass::DEFAULT_METADATA_READER_CLASS,
$container->getParameter(MetadataReaderCompilerPass::METADATA_READER_PARAM)
);
}

public function testProcessWithExtExifKeepsExifReader()
{
$container = new ContainerBuilder();
$container->setParameter(
MetadataReaderCompilerPass::METADATA_READER_PARAM,
MetadataReaderCompilerPass::EXIF_METADATA_READER_CLASS
);

/** @var MetadataReaderCompilerPass $pass */
$pass = $this->getMock(
'Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass',
array('extExifIsAvailable'),
array()
);
$pass->expects($this->once())
->method('extExifIsAvailable')
->willReturn(true);

//guard
$this->assertEquals(
MetadataReaderCompilerPass::EXIF_METADATA_READER_CLASS,
$container->getParameter(MetadataReaderCompilerPass::METADATA_READER_PARAM)
);

$pass->process($container);

$this->assertEquals(
MetadataReaderCompilerPass::EXIF_METADATA_READER_CLASS,
$container->getParameter(MetadataReaderCompilerPass::METADATA_READER_PARAM)
);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"suggest": {
"amazonwebservices/aws-sdk-for-php": "Required to use AWS version 1 cache resolver.",
"aws/aws-sdk-php": "Required to use AWS version 2/3 cache resolver.",
"ext-exif": "Required to read metadata from Exif information",
"league/flysystem": "Required to use FlySystem data loader or cache resolver.",
"monolog/monolog": "A psr/log compatible logger is required to enable logging.",
"twig/twig": "Required to use the provided Twig extension. Version 1.12 or greater needed."
Expand Down