You've already forked php-flasher
mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-04-05 12:32:55 +01:00
feat: auto inject flasher with flasher aware interface
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Prime\Aware;
|
||||
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
|
||||
interface FlasherAwareInterface
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setFlasher(FlasherInterface $flasher);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Prime\Aware;
|
||||
|
||||
use Flasher\Prime\FlasherInterface;
|
||||
|
||||
trait FlasherAwareTrait
|
||||
{
|
||||
/**
|
||||
* @var FlasherInterface
|
||||
*/
|
||||
protected $flasher;
|
||||
|
||||
public function setFlasher(FlasherInterface $flasher)
|
||||
{
|
||||
$this->flasher = $flasher;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Symfony\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
|
||||
final class FlasherAwareCompilerPass implements CompilerPassInterface
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->has('flasher')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$flasher = $container->findDefinition('flasher');
|
||||
|
||||
foreach ($container->findTaggedServiceIds('flasher.flasher_aware') as $id => $tags) {
|
||||
$service = $container->findDefinition($id);
|
||||
$service->addMethodCall('setFlasher', array($flasher));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ final class FlasherExtension extends Extension implements CompilerPassInterface
|
||||
$this->registerFlasherConfiguration($config, $container);
|
||||
$this->registerListeners($config, $container);
|
||||
$this->registerStorageManager($config, $container);
|
||||
$this->registerFlasherAutoConfiguration($container);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,4 +158,18 @@ final class FlasherExtension extends Extension implements CompilerPassInterface
|
||||
$storageManager = $container->getDefinition('flasher.storage_manager');
|
||||
$storageManager->replaceArgument(2, $criteria);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function registerFlasherAutoConfiguration(ContainerBuilder $container)
|
||||
{
|
||||
if (!method_exists($container, 'registerForAutoconfiguration')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$container
|
||||
->registerForAutoconfiguration('Flasher\Prime\Aware\FlasherAwareInterface')
|
||||
->addTag('flasher.flasher_aware');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ use Flasher\Prime\Plugin\FlasherPlugin;
|
||||
use Flasher\Symfony\Container\SymfonyContainer;
|
||||
use Flasher\Symfony\DependencyInjection\Compiler\EventSubscriberCompilerPass;
|
||||
use Flasher\Symfony\DependencyInjection\Compiler\FactoryCompilerPass;
|
||||
use Flasher\Symfony\DependencyInjection\Compiler\FlasherAwareCompilerPass;
|
||||
use Flasher\Symfony\DependencyInjection\Compiler\PresenterCompilerPass;
|
||||
use Flasher\Symfony\DependencyInjection\FlasherExtension;
|
||||
use Flasher\Symfony\Support\Bundle;
|
||||
@@ -27,6 +28,7 @@ class FlasherSymfonyBundle extends Bundle // Symfony\Component\HttpKernel\Bundle
|
||||
$container->addCompilerPass(new FactoryCompilerPass());
|
||||
$container->addCompilerPass(new EventSubscriberCompilerPass());
|
||||
$container->addCompilerPass(new PresenterCompilerPass());
|
||||
$container->addCompilerPass(new FlasherAwareCompilerPass());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user