Skip to content

Commit

Permalink
implement comprehensive container logging for compiler passes
Browse files Browse the repository at this point in the history
  • Loading branch information
robfrawley committed Jan 31, 2017
1 parent 7c0f500 commit 86329e3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
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 @@ -28,6 +27,7 @@ public function process(ContainerBuilder $container)
$manager = $container->getDefinition('liip_imagine.filter.manager');

foreach ($tags as $id => $tag) {
$this->log($container, 'Registering imagine filter loader: %s', array($id));
$manager->addMethodCall('addLoader', array($tag[0]['loader'], new Reference($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 @@ -28,6 +27,7 @@ public function process(ContainerBuilder $container)
$manager = $container->getDefinition('liip_imagine.data.manager');

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

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
class MetadataReaderCompilerPass extends AbstractCompilerPass
{
const METADATA_READER_PARAM = 'liip_imagine.meta_data.reader.class';

Expand All @@ -34,7 +33,7 @@ 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);
$this->log($container, 'Replacing imagine metadata reader: missing "exif" extension (installing this extension is suggested).');
}
}

Expand All @@ -50,21 +49,6 @@ protected function isDefaultMetadataReader(ContainerBuilder $container)
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
*/
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 @@ -33,6 +32,7 @@ public function process(ContainerBuilder $container)
$manager = $container->getDefinition('liip_imagine.filter.manager');

foreach ($tags as $id => $tag) {
$this->log($container, 'Registering imagine filter post-processor: %s', array($id));
$manager->addMethodCall('addPostProcessor', array($tag[0]['post_processor'], new Reference($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 @@ -28,6 +27,7 @@ public function process(ContainerBuilder $container)
$manager = $container->getDefinition('liip_imagine.cache.manager');

foreach ($tags as $id => $tag) {
$this->log($container, 'Registering imagine cache resolver: %s', array($id));
$manager->addMethodCall('addResolver', array($tag[0]['resolver'], new Reference($id)));
}
}
Expand Down

0 comments on commit 86329e3

Please sign in to comment.