Skip to content

Instantly share code, notes, and snippets.

@tPl0ch
Created January 18, 2013 12:19
Show Gist options
  • Save tPl0ch/4564244 to your computer and use it in GitHub Desktop.
Save tPl0ch/4564244 to your computer and use it in GitHub Desktop.
Using DefinitionDecorator to extend abstract service definition in a CompilerPass.
<?php
/**
* ShopCompilerPass.php
*
* @author Thomas Ploch <[email protected]>
* @since 17.01.13 14:26
* @filesource
*/
namespace Einblick\CoreShopBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* ShopCompilerPass class
*/
class ShopCompilerPass implements CompilerPassInterface
{
/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
*
* @api
*/
public function process(ContainerBuilder $container)
{
$config = $container->getExtensionConfig('core_shop');
$productTypeKey = $config[0]['product_type_key'];
$productTypeFactory = $config[0]['product_type_factory_service'];
if (!$container->hasDefinition($productTypeFactory)) {
throw new RuntimeException("ProductType factory service '$productTypeFactory' does not exist.");
}
$factoryDefinition = new DefinitionDecorator($productTypeFactory);
$factoryDefinition->addArgument($productTypeKey);
$container->setDefinition($productTypeKey, $factoryDefinition);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment