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

[DependencyInjection] Container logging for compiler passes #867

Merged
merged 1 commit into from
Feb 1, 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
36 changes: 36 additions & 0 deletions DependencyInjection/Compiler/AbstractCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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;

abstract class AbstractCompilerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
protected function log(ContainerBuilder $container, $message, array $replacements = array())
{
if (count($replacements) > 0) {
$message = vsprintf($message, $replacements);
}

if (method_exists($container, 'log')) {
$container->log($this, $message);
} else {
$compiler = $container->getCompiler();
$formatter = $compiler->getLoggingFormatter();
$compiler->addLogMessage($formatter->format($this, $message));
}
}
}
4 changes: 2 additions & 2 deletions DependencyInjection/Compiler/FiltersCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

namespace Liip\ImagineBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class FiltersCompilerPass implements CompilerPassInterface
class FiltersCompilerPass extends AbstractCompilerPass
{
/**
* {@inheritdoc}
Expand All @@ -29,6 +28,7 @@ public function process(ContainerBuilder $container)

foreach ($tags as $id => $tag) {
$manager->addMethodCall('addLoader', array($tag[0]['loader'], new Reference($id)));
$this->log($container, 'Registered imagine-bimdle filter loader: %s', array($id));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Compiler/LoadersCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

namespace Liip\ImagineBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class LoadersCompilerPass implements CompilerPassInterface
class LoadersCompilerPass extends AbstractCompilerPass
{
/**
* {@inheritdoc}
Expand All @@ -29,6 +28,7 @@ public function process(ContainerBuilder $container)

foreach ($tags as $id => $tag) {
$manager->addMethodCall('addLoader', array($tag[0]['loader'], new Reference($id)));
$this->log($container, 'Registered imagine-bimdle binary loader: %s', array($id));
}
}
}
Expand Down
59 changes: 28 additions & 31 deletions DependencyInjection/Compiler/MetadataReaderCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,44 @@

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.
* Be default, a metadata reader that requires the "exif" PHP extension is used. This compiler pass checks if the
* extension is loaded or not, and switches to a metadata reader (that does not rely on "exif") if not.
*/
class MetadataReaderCompilerPass implements CompilerPassInterface
class MetadataReaderCompilerPass extends AbstractCompilerPass
{
const METADATA_READER_PARAM = 'liip_imagine.meta_data.reader.class';
/**
* @var string
*/
private static $metadataReaderParameter = 'liip_imagine.meta_data.reader.class';

const DEFAULT_METADATA_READER_CLASS = 'Imagine\Image\Metadata\DefaultMetadataReader';
/**
* @var string
*/
private static $metadataReaderDefaultClass = 'Imagine\Image\Metadata\DefaultMetadataReader';

const EXIF_METADATA_READER_CLASS = 'Imagine\Image\Metadata\ExifMetadataReader';
/**
* @var string
*/
private static $metadataReaderExifClass = '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);
if (!$this->isExifExtensionLoaded() && $this->isExifMetadataReaderSet($container)) {
$container->setParameter(self::$metadataReaderParameter, self::$metadataReaderDefaultClass);
$message = 'Overwrote "%s" parameter value from "%s" to "%s" due to missing "exif" extension '
.'(installing the "exif" extension is highly recommended; you may experience degraded '
.'metadata handling without it)';
$this->log($container, $message, array(
self::$metadataReaderParameter,
self::$metadataReaderExifClass,
self::$metadataReaderDefaultClass,
));
}
}

Expand All @@ -43,32 +57,15 @@ public function process(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)
private function isExifMetadataReaderSet(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 $container->getParameter(self::$metadataReaderParameter) === self::$metadataReaderExifClass;
}

/**
* @return bool
*/
protected function extExifIsAvailable()
protected function isExifExtensionLoaded()
{
return extension_loaded('exif');
}
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Compiler/PostProcessorsCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Liip\ImagineBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

Expand All @@ -20,7 +19,7 @@
*
* @author Konstantin Tjuterev <[email protected]>
*/
class PostProcessorsCompilerPass implements CompilerPassInterface
class PostProcessorsCompilerPass extends AbstractCompilerPass
{
/**
* {@inheritdoc}
Expand All @@ -34,6 +33,7 @@ public function process(ContainerBuilder $container)

foreach ($tags as $id => $tag) {
$manager->addMethodCall('addPostProcessor', array($tag[0]['post_processor'], new Reference($id)));
$this->log($container, 'Registered imagine-bimdle filter post-processor: %s', array($id));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Compiler/ResolversCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

namespace Liip\ImagineBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class ResolversCompilerPass implements CompilerPassInterface
class ResolversCompilerPass extends AbstractCompilerPass
{
/**
* {@inheritdoc}
Expand All @@ -29,6 +28,7 @@ public function process(ContainerBuilder $container)

foreach ($tags as $id => $tag) {
$manager->addMethodCall('addResolver', array($tag[0]['resolver'], new Reference($id)));
$this->log($container, 'Registered imagine-bimdle cache resolver: %s', array($id));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use Symfony\Component\DependencyInjection\Definition;

/**
* @covers Liip\ImagineBundle\DependencyInjection\Compiler\FiltersCompilerPass
* @covers \Liip\ImagineBundle\DependencyInjection\Compiler\AbstractCompilerPass
* @covers \Liip\ImagineBundle\DependencyInjection\Compiler\FiltersCompilerPass
*/
class FiltersCompilerPassTest extends \PHPUnit_Framework_TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use Symfony\Component\DependencyInjection\Definition;

/**
* @covers Liip\ImagineBundle\DependencyInjection\Compiler\LoadersCompilerPass
* @covers \Liip\ImagineBundle\DependencyInjection\Compiler\AbstractCompilerPass
* @covers \Liip\ImagineBundle\DependencyInjection\Compiler\LoadersCompilerPass
*/
class LoadersCompilerPassTest extends \PHPUnit_Framework_TestCase
{
Expand Down
110 changes: 61 additions & 49 deletions Tests/DependencyInjection/Compiler/MetadataReaderCompilerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,83 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @covers Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass
* @covers \Liip\ImagineBundle\DependencyInjection\Compiler\AbstractCompilerPass
* @covers \Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass
*/
class MetadataReaderCompilerPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @param \ReflectionClass $r
* @param string $p
*
* @return string
*/
private static function getPrivateStaticProperty(\ReflectionClass $r, $p)
{
$property = $r->getProperty($p);
$property->setAccessible(true);

return $property->getValue();
}

/**
* @return mixed[]
*/
private static function getReaderParamAndDefaultAndExifValues()
{
$r = new \ReflectionClass('Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass');

return array(
static::getPrivateStaticProperty($r, 'metadataReaderParameter'),
static::getPrivateStaticProperty($r, 'metadataReaderExifClass'),
static::getPrivateStaticProperty($r, 'metadataReaderDefaultClass'),
);
}

/**
* @param bool $return
*
* @return \PHPUnit_Framework_MockObject_MockObject|MetadataReaderCompilerPass
*/
private function getMetadataReaderCompilerPass($return)
{
$mock = $this->getMockBuilder('Liip\ImagineBundle\DependencyInjection\Compiler\MetadataReaderCompilerPass')
->setMethods(array('isExifExtensionLoaded'))
->getMock();

$mock
->expects($this->atLeastOnce())
->method('isExifExtensionLoaded')
->willReturn($return);

return $mock;
}

public function testProcessWithoutExtExifAddsDefaultReader()
{
list($metadataParameter, $metadataExifClass, $metadataDefaultClass) = static::getReaderParamAndDefaultAndExifValues();

$container = new ContainerBuilder();
$container->setParameter(
MetadataReaderCompilerPass::METADATA_READER_PARAM,
MetadataReaderCompilerPass::EXIF_METADATA_READER_CLASS
);
$container->setParameter($metadataParameter, $metadataExifClass);

/** @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 = $this->getMetadataReaderCompilerPass(false);
$this->assertEquals($metadataExifClass, $container->getParameter($metadataParameter));

$pass->process($container);

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

public function testProcessWithExtExifKeepsExifReader()
{
list($metadataParameter, $metadataExifClass) = static::getReaderParamAndDefaultAndExifValues();

$container = new ContainerBuilder();
$container->setParameter(
MetadataReaderCompilerPass::METADATA_READER_PARAM,
MetadataReaderCompilerPass::EXIF_METADATA_READER_CLASS
);
$container->setParameter($metadataParameter, $metadataExifClass);

/** @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 = static::getMetadataReaderCompilerPass(true);
$this->assertEquals($metadataExifClass, $container->getParameter($metadataParameter));

$pass->process($container);

$this->assertEquals(
MetadataReaderCompilerPass::EXIF_METADATA_READER_CLASS,
$container->getParameter(MetadataReaderCompilerPass::METADATA_READER_PARAM)
);
$this->assertEquals($metadataExifClass, $container->getParameter($metadataParameter));
}
}
Loading