Commit 4341af3c authored by Daniel Król's avatar Daniel Król

Initial commit

parents
/vendor/
composer.lock
{
"name": "mleko/event-bundle",
"description": "Event library bundle",
"tags": [
"mleko",
"event",
"bundle",
"symfony"
],
"type": "library",
"require": {
"mleko/event": "^0.1.0",
"symfony/http-kernel": "^2.3|^3.0",
"symfony/config": "^2.3|^3.0",
"symfony/dependency-injection": "^2.3|^3.0"
},
"license": "MIT",
"authors": [
{
"name": "Daniel Król",
"email": "daniel@krol.me"
}
],
"autoload": {
"psr-4": {
"Mleko\\Event\\Bundle\\": "src/"
}
}
}
<?php
/**
* @package event-bundle
*/
namespace Mleko\Event\Bundle\DependencyInjection\Compiler;
class ListenerPass implements \Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface
{
/**
* You can modify the container here before it is dumped to PHP code.
*
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
public function process(\Symfony\Component\DependencyInjection\ContainerBuilder $container)
{
$services = $container->findTaggedServiceIds('mleko.event.listener');
foreach ($services as $serviceId => $tags) {
foreach ($tags as $tag) {
if (!isset($tag['event'])) {
throw new \RuntimeException('The mleko.event.listener must have event attribute');
}
$eventName = $tag['event'];
$emitterName = isset($tag['emitter']) ? $tag['emitter'] : 'default';
$methodName = isset($tag['method']) ? $tag['method'] : null;
$this->registerListener($container, $eventName, $emitterName, $serviceId, $methodName);
}
}
}
private function registerListener(\Symfony\Component\DependencyInjection\ContainerBuilder $container, $eventName, $emitterName, $listenerServiceId, $methodName)
{
$emitterDefinition = $container->getDefinition('mleko.event.emitter.' . $emitterName);
$listeners = $emitterDefinition->getArgument(1);
$eventListeners = isset($listeners[$eventName]) ? $listeners[$eventName] : [];
$eventListeners[] = ['serviceId' => $listenerServiceId, 'methodName' => $methodName];
$listeners[$eventName] = $eventListeners;
$emitterDefinition->replaceArgument(1, $listeners);
}
}
<?php
namespace Mleko\Event\Bundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mleko_event');
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
}
<?php
namespace Mleko\Event\Bundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class MlekoEventExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}
}
<?php
/**
* @package event-bundle
*/
namespace Mleko\Event\Bundle;
class EmitterFactory
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
/**
* EmitterFactory constructor.
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
*/
public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container)
{
$this->container = $container;
}
/**
* @param \Mleko\Event\EventNameExtractor $nameExtractor
* @param $listenerDefinitions
* @return \Mleko\Event\SimpleEmitter
*/
public function createEmitter(\Mleko\Event\EventNameExtractor $nameExtractor, $listenerDefinitions)
{
$listeners = [];
foreach ($listenerDefinitions as $eventName => $listenerDefinition) {
foreach ($listenerDefinition as $listenerData) {
$listeners[$eventName][] = new \Mleko\Event\Bundle\Listener\ListenerService($listenerData['serviceId'], $this->container, $listenerData['methodName']);
}
}
return new \Mleko\Event\SimpleEmitter($nameExtractor, $listeners);
}
}
<?php
/**
* @package event-bundle
*/
namespace Mleko\Event\Bundle\Listener;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ListenerService implements \Mleko\Event\Listener
{
/** @var ContainerInterface */
private $container;
/** @var string */
private $serviceId;
/** @var string */
private $methodName;
/**
* ListenerService constructor.
* @param string $serviceId
* @param ContainerInterface $container
* @param string $methodName
*/
public function __construct($serviceId, ContainerInterface $container, $methodName = 'handle')
{
$this->serviceId = $serviceId;
$this->container = $container;
$this->methodName = $methodName ?: 'handle';
}
/**
* Handle an event
*
* @param object $event
* @param \Mleko\Event\Meta $meta
*/
public function handle($event, \Mleko\Event\Meta $meta)
{
$listener = $this->container->get($this->serviceId);
call_user_func_array([$listener, $this->methodName], [$event, $meta]);
}
}
<?php
namespace Mleko\Event\Bundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class MlekoEventBundle extends Bundle
{
public function build(\Symfony\Component\DependencyInjection\ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new \Mleko\Event\Bundle\DependencyInjection\Compiler\ListenerPass());
}
}
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="mleko.event.emitter" alias="mleko.event.emitter.default"/>
<service id="mleko.event.emitter.default" class="Mleko\Event\SimpleEmitter">
<factory service="mleko.event.emitter_factory" method="createEmitter"/>
<argument type="service" id="mleko.event.name_extractor.class_name"/>
<argument type="collection"/>
</service>
<service id="mleko.event.name_extractor.class_name" class="Mleko\Event\EventNameExtractor\ClassNameExtractor"/>
<service id="mleko.event.emitter_factory" class="Mleko\Event\Bundle\EmitterFactory">
<argument type="service" id="service_container"/>
</service>
</services>
</container>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment