fix event dispatcher and call the storage service

This commit is contained in:
Khoubza Younes
2020-12-06 04:03:14 +01:00
parent 10e1ec7aa0
commit 1d0c60a5f7
31 changed files with 407 additions and 110 deletions
@@ -0,0 +1,37 @@
<?php
namespace Flasher\Prime\EventDispatcher\Event;
use Flasher\Prime\Envelope;
final class EnvelopeDispatchedEvent
{
/**
* @var Envelope
*/
private $envelope;
/**
* @param Envelope $envelope
*/
public function __construct(Envelope $envelope)
{
$this->envelope = $envelope;
}
/**
* @return Envelope
*/
public function getEnvelope()
{
return $this->envelope;
}
/**
* @param Envelope $envelope
*/
public function setEnvelope(Envelope $envelope)
{
$this->envelope = $envelope;
}
}
+9 -4
View File
@@ -3,7 +3,7 @@
namespace Flasher\Prime\EventDispatcher;
use Flasher\Prime\EventDispatcher\Event\StoppableEventInterface;
use Flasher\Prime\EventDispatcher\EventSubscriber\EventSubscriberInterface;
use Flasher\Prime\EventDispatcher\EventListener\EventSubscriberInterface;
final class EventDispatcher implements EventDispatcherInterface
{
@@ -81,10 +81,15 @@ final class EventDispatcher implements EventDispatcherInterface
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
{
foreach ((array) $subscriber->getSubscribedEvents() as $eventName => $params) {
if (\is_string($params)) {
foreach ((array) $subscriber->getSubscribedEvents() as $eventName => $params) {
if (is_int($eventName)) {
$eventName = $params;
$params = '__invoke';
}
if (is_string($params)) {
$this->addListener($eventName, array($subscriber, $params));
} elseif (\is_string($params[0])) {
} elseif (is_string($params[0])) {
$this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
} else {
foreach ($params as $listener) {
+1 -1
View File
@@ -2,7 +2,7 @@
namespace Flasher\Prime\EventDispatcher;
use Flasher\Prime\EventDispatcher\EventSubscriber\EventSubscriberInterface;
use Flasher\Prime\EventDispatcher\EventListener\EventSubscriberInterface;
interface EventDispatcherInterface
{
@@ -1,6 +1,6 @@
<?php
namespace Flasher\Prime\EventDispatcher\EventSubscriber;
namespace Flasher\Prime\EventDispatcher\EventListener;
interface EventSubscriberInterface
{
@@ -0,0 +1,35 @@
<?php
namespace Flasher\Prime\EventDispatcher\EventListener;
use Flasher\Prime\EventDispatcher\Event\EnvelopeDispatchedEvent;
use Flasher\Prime\Middleware\FlasherBus;
final class MiddlewareListener implements EventSubscriberInterface
{
/**
* @var FlasherBus
*/
private $flasherBus;
/**
* @param FlasherBus $flasherBus
*/
public function __construct(FlasherBus $flasherBus)
{
$this->flasherBus = $flasherBus;
}
/**
* @param EnvelopeDispatchedEvent $event
*/
public function __invoke(EnvelopeDispatchedEvent $event)
{
$this->flasherBus->dispatch($event->getEnvelope());
}
public static function getSubscribedEvents()
{
return 'Flasher\Prime\EventDispatcher\Event\EnvelopeDispatchedEvent';
}
}
@@ -1,11 +1,11 @@
<?php
namespace Flasher\Prime\EventDispatcher\EventSubscriber;
namespace Flasher\Prime\EventDispatcher\EventListener;
use Flasher\Prime\EventDispatcher\Event\PostFilterEvent;
use Flasher\Prime\Envelope;
final class FilterEnvelopesByHopsListener implements EventSubscriberInterface
final class PostFilterListener implements EventSubscriberInterface
{
/**
* @param PostFilterEvent $event
@@ -16,14 +16,11 @@ final class FilterEnvelopesByHopsListener implements EventSubscriberInterface
{
$envelopes = $event->getEnvelopes();
$envelopes = array_filter(
$envelopes,
static function (Envelope $envelope) {
$hopsStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
$envelopes = array_filter($envelopes, static function (Envelope $envelope) {
$hopsStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
return $hopsStamp->getAmount() > 0;
}
);
return $hopsStamp->getAmount() > 0;
});
$event->setEnvelopes($envelopes);
}
@@ -1,12 +1,12 @@
<?php
namespace Flasher\Prime\EventDispatcher\EventSubscriber;
namespace Flasher\Prime\EventDispatcher\EventListener;
use Flasher\Prime\EventDispatcher\Event\PostFlushEvent;
use Flasher\Prime\Stamp\HopsStamp;
use Flasher\Prime\Storage\StorageInterface;
class RemoveRenderedEnvelopesSubscriber implements EventSubscriberInterface
final class PostFlushListener implements EventSubscriberInterface
{
/**
* @var StorageInterface
@@ -0,0 +1,32 @@
<?php
namespace Flasher\Prime\EventDispatcher\EventListener;
use Flasher\Prime\EventDispatcher\Event\EnvelopeDispatchedEvent;
use Flasher\Prime\Storage\StorageInterface;
final class StorageListener implements EventSubscriberInterface
{
/**
* @var StorageInterface
*/
private $storage;
/**
* @param StorageInterface $storage
*/
public function __construct(StorageInterface $storage)
{
$this->storage = $storage;
}
public function __invoke(EnvelopeDispatchedEvent $event)
{
$this->storage->add($event->getEnvelope());
}
public static function getSubscribedEvents()
{
return 'Flasher\Prime\EventDispatcher\Event\EnvelopeDispatchedEvent';
}
}
@@ -2,6 +2,7 @@
namespace Flasher\Prime\Factory;
use Flasher\Prime\EventDispatcher\EventDispatcherInterface;
use Flasher\Prime\Notification\Notification;
use Flasher\Prime\Notification\NotificationBuilder;
use Flasher\Prime\Notification\NotificationBuilderInterface;
@@ -22,14 +23,27 @@ use Flasher\Prime\Notification\NotificationInterface;
* @method NotificationBuilderInterface warning($message = null, array $options = array())
* @method NotificationInterface getNotification()
*/
abstract class AbstractFlasher implements FactoryInterface
abstract class AbstractFactory implements FlasherFactoryInterface
{
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* {@inheritdoc}
*/
public function createNotificationBuilder()
{
return new NotificationBuilder($this->createNotification());
return new NotificationBuilder($this->getEventDispatcher(), $this->createNotification(), $this->createHandler());
}
/**
@@ -40,6 +54,14 @@ abstract class AbstractFlasher implements FactoryInterface
return new Notification();
}
/**
* {@inheritdoc}
*/
public function createHandler()
{
return null;
}
/**
* @inheritDoc
*/
@@ -60,4 +82,12 @@ abstract class AbstractFlasher implements FactoryInterface
{
return call_user_func_array(array($this->createNotificationBuilder(), $method), $parameters);
}
/**
* @return EventDispatcherInterface
*/
public function getEventDispatcher()
{
return $this->eventDispatcher;
}
}
@@ -5,7 +5,7 @@ namespace Flasher\Prime\Factory;
use Flasher\Prime\Notification\NotificationBuilderInterface;
use Flasher\Prime\Notification\NotificationInterface;
interface FactoryInterface
interface FlasherFactoryInterface
{
/**
* @return NotificationBuilderInterface
+22
View File
@@ -2,17 +2,39 @@
namespace Flasher\Prime\Filter;
use Flasher\Prime\Envelope;
final class DefaultFilter implements FilterInterface
{
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @param FilterBuilder $filterBuilder
*/
public function __construct(FilterBuilder $filterBuilder)
{
$this->filterBuilder = $filterBuilder;
}
/**
* @param Envelope[] $envelopes
* @param array $criteria
*
* @return array
*/
public function filter($envelopes, $criteria = array())
{
return $this->filterBuilder->withCriteria($criteria)->filter($envelopes);
}
/**
* @inheritDoc
*/
public function supports($name = null, array $context = array())
{
return in_array($name, array(__CLASS__, 'default'));
}
}
+1 -4
View File
@@ -4,10 +4,7 @@ namespace Flasher\Prime\Filter;
use Flasher\Prime\Manager\AbstractManager;
/**
* @method \Flasher\Prime\Filter\FilterInterface make($driver = null)
*/
final class FilterManager extends AbstractManager
final class FilterManager extends AbstractManager implements FilterManagerInterface
{
protected function getDefaultDriver()
{
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Flasher\Prime\Filter;
interface FilterManagerInterface
{
/**
* Get a driver instance.
*
* @param string|null $name
* @param array $context
*
* @return FilterInterface
*
* @throws \InvalidArgumentException
*/
public function make($name = null, array $context = array());
/**
* Register a custom driver creator.
*
* @param \Closure|FilterInterface $driver
*
* @return $this
*/
public function addDriver($driver);
}
+4 -2
View File
@@ -2,6 +2,8 @@
namespace Flasher\Prime;
use Flasher\Prime\Factory\FlasherFactoryInterface;
interface FlasherInterface
{
/**
@@ -10,7 +12,7 @@ interface FlasherInterface
* @param string|null $name
* @param array $context
*
* @return NotifyFactoryInterface
* @return FlasherFactoryInterface
*
* @throws \InvalidArgumentException
*/
@@ -19,7 +21,7 @@ interface FlasherInterface
/**
* Register a custom driver creator.
*
* @param \Closure|NotifyFactoryInterface $driver
* @param \Closure|FlasherFactoryInterface $driver
*
* @return $this
*/
+2 -2
View File
@@ -41,8 +41,8 @@ abstract class AbstractManager
{
$name = $name ?: $this->getDefaultDriver();
if (is_array($name)) {
$context = $name;
if (!is_string($name)) {
$context = is_array($name) ? $name : array($name);
$name = null;
}
-27
View File
@@ -1,27 +0,0 @@
<?php
namespace Flasher\Prime\Manager;
interface ManagerInterface
{
/**
* Get a driver instance.
*
* @param string|null $driver
*
* @return object
*
* @throws \InvalidArgumentException
*/
public function make($driver = null);
/**
* Register a custom driver creator.
*
* @param string $alias
* @param \Closure|object $driver
*
* @return $this
*/
public function addDriver($alias, $driver);
}
@@ -3,22 +3,16 @@
namespace Flasher\Prime\Middleware;
use Flasher\Prime\Envelope;
use Flasher\Prime\Notification\NotificationInterface;
final class NotifyBus
final class FlasherBus implements FlasherBusInterface
{
/**
* @var MiddlewareInterface[]
*/
private $middlewares;
private $middlewares = array();
/**
* Executes the given command and optionally returns a value
*
* @param Envelope|NotificationInterface $envelope
* @param array $stamps
*
* @return mixed
* @inheritDoc
*/
public function dispatch($envelope, $stamps = array())
{
@@ -32,9 +26,7 @@ final class NotifyBus
}
/**
* @param MiddlewareInterface $middleware
*
* @return $this
* @inheritDoc
*/
public function addMiddleware(MiddlewareInterface $middleware)
{
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace Flasher\Prime\Middleware;
use Flasher\Prime\Envelope;
use Flasher\Prime\Notification\NotificationInterface;
interface FlasherBusInterface
{
/**
* Executes the given command and optionally returns a value
*
* @param Envelope|NotificationInterface $envelope
* @param array $stamps
*
* @return mixed
*/
public function dispatch($envelope, $stamps = array());
/**
* @param MiddlewareInterface $middleware
*
* @return $this
*/
public function addMiddleware(MiddlewareInterface $middleware);
}
+72 -5
View File
@@ -3,6 +3,9 @@
namespace Flasher\Prime\Notification;
use Flasher\Prime\Envelope;
use Flasher\Prime\EventDispatcher\Event\EnvelopeDispatchedEvent;
use Flasher\Prime\EventDispatcher\EventDispatcherInterface;
use Flasher\Prime\Stamp\HandlerStamp;
use Flasher\Prime\Stamp\HopsStamp;
use Flasher\Prime\Stamp\PriorityStamp;
@@ -14,13 +17,27 @@ class NotificationBuilder implements NotificationBuilderInterface
protected $envelope;
/**
* @param NotificationInterface|null $notification
* @var EventDispatcherInterface
*/
public function __construct(NotificationInterface $notification = null)
{
$notification = $notification ?: new Notification();
protected $eventDispatcher;
/**
* @param EventDispatcherInterface $eventDispatcher
* @param NotificationInterface|null $notification
* @param string $handler
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
NotificationInterface $notification = null,
$handler = null
) {
$this->eventDispatcher = $eventDispatcher;
$notification = $notification ?: new Notification();
$this->envelope = Envelope::wrap($notification);
$handler = $handler ?: get_class($notification);
$this->handler($handler);
}
/**
@@ -123,6 +140,16 @@ class NotificationBuilder implements NotificationBuilderInterface
return $this->type(NotificationInterface::TYPE_WARNING, $message, $options);
}
/**
* @inheritDoc
*/
public function handler($handler)
{
$this->envelope->withStamp(new HandlerStamp($handler));
return $this;
}
/**
* @inheritDoc
*/
@@ -149,10 +176,50 @@ class NotificationBuilder implements NotificationBuilderInterface
public function keep()
{
$hopsStamp = $this->envelope->get('Flasher\Prime\Stamp\HopsStamp');
$amount = $hopsStamp instanceof HopsStamp ? $hopsStamp->getAmount() : 1;
$amount = $hopsStamp instanceof HopsStamp ? $hopsStamp->getAmount() : 1;
$this->envelope->withStamp(new HopsStamp($amount + 1));
return $this;
}
/**
* @inheritDoc
*/
public function with($stamps = array())
{
$this->envelope->with($stamps);
return $this;
}
/**
* @inheritDoc
*/
public function withStamp($stamps = array())
{
$this->envelope->withStamp($stamps);
return $this;
}
/**
* Dispatch the notification to the flasher bus
*
* @param array $stamps
*
* @return Envelope|mixed
*/
public function dispatch($stamps = array())
{
if (!empty($stamps)) {
$this->with($stamps);
}
$envelope = $this->getEnvelope();
$event = new EnvelopeDispatchedEvent($envelope);
return $this->eventDispatcher->dispatch($event);
}
}
+19 -19
View File
@@ -6,12 +6,12 @@ use Flasher\Prime\Config\ConfigInterface;
use Flasher\Prime\Envelope;
use Flasher\Prime\EventDispatcher\Event\PostFilterEvent;
use Flasher\Prime\EventDispatcher\EventDispatcherInterface;
use Flasher\Prime\Filter\FilterManager;
use Flasher\Prime\Filter\FilterManagerInterface;
use Flasher\Prime\Renderer\HasOptionsInterface;
use Flasher\Prime\Renderer\HasScriptsInterface;
use Flasher\Prime\Renderer\HasStylesInterface;
use Flasher\Prime\Renderer\RendererManager;
use Flasher\Prime\Storage\StorageInterface;
use Flasher\Prime\Renderer\RendererManagerInterface;
use Flasher\Prime\Storage\StorageManagerInterface;
abstract class AbstractPresenter implements PresenterInterface
{
@@ -26,17 +26,17 @@ abstract class AbstractPresenter implements PresenterInterface
protected $config;
/**
* @var StorageInterface
* @var StorageManagerInterface
*/
protected $storage;
protected $storageManager;
/**
* @var FilterManager
* @var FilterManagerInterface
*/
protected $filterManager;
/**
* @var RendererManager
* @var RendererManagerInterface
*/
protected $rendererManager;
@@ -45,20 +45,20 @@ abstract class AbstractPresenter implements PresenterInterface
*
* @param EventDispatcherInterface $eventDispatcher
* @param ConfigInterface $config
* @param StorageInterface $storage
* @param FilterManager $filterManager
* @param RendererManager $rendererManager
* @param StorageManagerInterface $storageManager
* @param FilterManagerInterface $filterManager
* @param RendererManagerInterface $rendererManager
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
ConfigInterface $config,
StorageInterface $storage,
FilterManager $filterManager,
RendererManager $rendererManager
StorageManagerInterface $storageManager,
FilterManagerInterface $filterManager,
RendererManagerInterface $rendererManager
) {
$this->eventDispatcher = $eventDispatcher;
$this->config = $config;
$this->storage = $storage;
$this->storageManager = $storageManager;
$this->filterManager = $filterManager;
$this->rendererManager = $rendererManager;
}
@@ -81,7 +81,7 @@ abstract class AbstractPresenter implements PresenterInterface
{
$filter = $this->filterManager->make($filterName);
$envelopes = $this->storage->all();
$envelopes = $this->storageManager->all();
$event = new PostFilterEvent($envelopes);
$this->eventDispatcher->dispatch($event);
@@ -133,18 +133,18 @@ abstract class AbstractPresenter implements PresenterInterface
$renderers = array();
foreach ($envelopes as $envelope) {
$rendererStamp = $envelope->get('Flasher\Prime\Stamp\HandlerStamp');
if (in_array($rendererStamp->getHandler(), $renderers)) {
$handlerStamp = $envelope->get('Flasher\Prime\Stamp\HandlerStamp');
if (in_array($handlerStamp->getHandler(), $renderers)) {
continue;
}
$renderer = $this->rendererManager->make($rendererStamp->getHandler());
$renderer = $this->rendererManager->make($handlerStamp->getHandler());
if (!$renderer instanceof HasScriptsInterface) {
continue;
}
$files = array_merge($files, $renderer->getScripts());
$renderers[] = $rendererStamp->getHandler();
$renderers[] = $handlerStamp->getHandler();
}
return array_values(array_filter(array_unique($files)));
+5 -5
View File
@@ -35,20 +35,20 @@ final class HtmlPresenter extends AbstractPresenter
$html = <<<HTML
{$scripts}
<script type="text/javascript">
var renderPHPNotifyNotifications = function () {
var renderPHPFlasherNotifications = function () {
{$options}
{$notifications}
}
if ("undefined" !== typeof PHPNotify) {
PHPNotify.addStyles({$styles}, renderPHPNotifyNotifications);
if ("undefined" !== typeof PHPFlasher) {
PHPFlasher.addStyles({$styles}, renderPHPFlasherNotifications);
} else {
renderPHPNotifyNotifications();
renderPHPFlasherNotifications();
}
</script>
HTML;
$this->storage->flush($envelopes);
$this->storageManager->flush($envelopes);
return $html;
}
+1 -1
View File
@@ -34,7 +34,7 @@ final class JsonPresenter extends AbstractPresenter
'notifications' => $this->renderEnvelopes($envelopes),
);
$this->storage->flush($envelopes);
$this->storageManager->flush($envelopes);
return $response;
}
+1 -4
View File
@@ -4,9 +4,6 @@ namespace Flasher\Prime\Presenter;
use Flasher\Prime\Manager\AbstractManager;
/**
* @method PresenterInterface make($name = null, array $context = array())
*/
final class PresenterManager extends AbstractManager
final class PresenterManager extends AbstractManager implements PresenterManagerInterface
{
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Flasher\Prime\Presenter;
interface PresenterManagerInterface
{
/**
* Get a driver instance.
*
* @param string|null $name
* @param array $context
*
* @return PresenterInterface
*
* @throws \InvalidArgumentException
*/
public function make($name = null, array $context = array());
/**
* Register a custom driver creator.
*
* @param \Closure|PresenterInterface $driver
*
* @return $this
*/
public function addDriver($driver);
}
+8
View File
@@ -12,4 +12,12 @@ interface RendererInterface
* @return string
*/
public function render(Envelope $envelope);
/**
* @param string $name
* @param array $context
*
* @return bool
*/
public function supports($name = null, array $context = array());
}
+1 -4
View File
@@ -4,9 +4,6 @@ namespace Flasher\Prime\Renderer;
use Flasher\Prime\Manager\AbstractManager;
/**
* @method RendererInterface make($name = null, array $context = array())
*/
class RendererManager extends AbstractManager
final class RendererManager extends AbstractManager implements RendererManagerInterface
{
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace Flasher\Prime\Renderer;
interface RendererManagerInterface
{
/**
* Get a driver instance.
*
* @param string|null $name
* @param array $context
*
* @return RendererInterface
*
* @throws \InvalidArgumentException
*/
public function make($name = null, array $context = array());
/**
* Register a custom driver creator.
*
* @param \Closure|RendererInterface $driver
*
* @return $this
*/
public function addDriver($driver);
}
-1
View File
@@ -6,7 +6,6 @@ use Flasher\Prime\Envelope;
use Flasher\Prime\EventDispatcher\Event\PostFlushEvent;
use Flasher\Prime\EventDispatcher\Event\PreFlushEvent;
use Flasher\Prime\EventDispatcher\EventDispatcherInterface;
use Flasher\Prime\Stamp\HopsStamp;
final class StorageManager implements StorageManagerInterface
{
+2 -2
View File
@@ -31,8 +31,8 @@ final class ConfigTest extends TestCase
),
$config->get('drivers.notify')
);
$this->assertEquals(array('styles.css'), $config->get('drivers.notify.styles'));
$this->assertEquals(array(), $config->get('drivers.notify.options'));
$this->assertEquals(array('styles.css'), $config->get('drivers.flasher.styles'));
$this->assertEquals(array(), $config->get('drivers.flasher.options'));
$this->assertEquals(null, $config->get('drivers.not_exists.options'));
$this->assertEquals('now_it_exists', $config->get('drivers.not_exists.options', 'now_it_exists'));
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Flasher\Prime\Tests\Producer;
namespace Flasher\Prime\Tests\Factory;
use Flasher\Prime\EventDispatcher\Event\BeforeFilter;
use Flasher\Prime\EventDispatcher\EventDispatcher;
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Flasher\Prime\Tests\Stubs\Producer;
namespace Flasher\Prime\Tests\Stubs\Factory;
use Flasher\Prime\AbstractFlasher;