diff --git a/EventDispatcher/Event/EnvelopeDispatchedEvent.php b/EventDispatcher/Event/EnvelopeDispatchedEvent.php new file mode 100644 index 00000000..6ec59de6 --- /dev/null +++ b/EventDispatcher/Event/EnvelopeDispatchedEvent.php @@ -0,0 +1,37 @@ +envelope = $envelope; + } + + /** + * @return Envelope + */ + public function getEnvelope() + { + return $this->envelope; + } + + /** + * @param Envelope $envelope + */ + public function setEnvelope(Envelope $envelope) + { + $this->envelope = $envelope; + } +} diff --git a/EventDispatcher/EventDispatcher.php b/EventDispatcher/EventDispatcher.php index 874de6b6..a6d953f0 100644 --- a/EventDispatcher/EventDispatcher.php +++ b/EventDispatcher/EventDispatcher.php @@ -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) { diff --git a/EventDispatcher/EventDispatcherInterface.php b/EventDispatcher/EventDispatcherInterface.php index a4c5c4e9..9bb56387 100644 --- a/EventDispatcher/EventDispatcherInterface.php +++ b/EventDispatcher/EventDispatcherInterface.php @@ -2,7 +2,7 @@ namespace Flasher\Prime\EventDispatcher; -use Flasher\Prime\EventDispatcher\EventSubscriber\EventSubscriberInterface; +use Flasher\Prime\EventDispatcher\EventListener\EventSubscriberInterface; interface EventDispatcherInterface { diff --git a/EventDispatcher/EventSubscriber/EventSubscriberInterface.php b/EventDispatcher/EventListener/EventSubscriberInterface.php similarity index 71% rename from EventDispatcher/EventSubscriber/EventSubscriberInterface.php rename to EventDispatcher/EventListener/EventSubscriberInterface.php index 1c136437..7780faaa 100644 --- a/EventDispatcher/EventSubscriber/EventSubscriberInterface.php +++ b/EventDispatcher/EventListener/EventSubscriberInterface.php @@ -1,6 +1,6 @@ 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'; + } +} diff --git a/EventDispatcher/EventSubscriber/FilterEnvelopesByHopsListener.php b/EventDispatcher/EventListener/PostFilterListener.php similarity index 55% rename from EventDispatcher/EventSubscriber/FilterEnvelopesByHopsListener.php rename to EventDispatcher/EventListener/PostFilterListener.php index 23c99a01..d91853ab 100644 --- a/EventDispatcher/EventSubscriber/FilterEnvelopesByHopsListener.php +++ b/EventDispatcher/EventListener/PostFilterListener.php @@ -1,11 +1,11 @@ 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); } diff --git a/EventDispatcher/EventSubscriber/RemoveRenderedEnvelopesSubscriber.php b/EventDispatcher/EventListener/PostFlushListener.php similarity index 88% rename from EventDispatcher/EventSubscriber/RemoveRenderedEnvelopesSubscriber.php rename to EventDispatcher/EventListener/PostFlushListener.php index e5ea6a0a..aa6864ec 100644 --- a/EventDispatcher/EventSubscriber/RemoveRenderedEnvelopesSubscriber.php +++ b/EventDispatcher/EventListener/PostFlushListener.php @@ -1,12 +1,12 @@ storage = $storage; + } + + public function __invoke(EnvelopeDispatchedEvent $event) + { + $this->storage->add($event->getEnvelope()); + } + + public static function getSubscribedEvents() + { + return 'Flasher\Prime\EventDispatcher\Event\EnvelopeDispatchedEvent'; + } +} diff --git a/Factory/AbstractFlasher.php b/Factory/AbstractFactory.php similarity index 70% rename from Factory/AbstractFlasher.php rename to Factory/AbstractFactory.php index 7ddc4fc1..b8bf4d9f 100644 --- a/Factory/AbstractFlasher.php +++ b/Factory/AbstractFactory.php @@ -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; + } } diff --git a/Factory/FactoryInterface.php b/Factory/FlasherFactoryInterface.php similarity index 94% rename from Factory/FactoryInterface.php rename to Factory/FlasherFactoryInterface.php index 3d9663e0..dbd68b17 100644 --- a/Factory/FactoryInterface.php +++ b/Factory/FlasherFactoryInterface.php @@ -5,7 +5,7 @@ namespace Flasher\Prime\Factory; use Flasher\Prime\Notification\NotificationBuilderInterface; use Flasher\Prime\Notification\NotificationInterface; -interface FactoryInterface +interface FlasherFactoryInterface { /** * @return NotificationBuilderInterface diff --git a/Filter/DefaultFilter.php b/Filter/DefaultFilter.php index 2f8a49c9..281e7364 100644 --- a/Filter/DefaultFilter.php +++ b/Filter/DefaultFilter.php @@ -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')); + } } diff --git a/Filter/FilterManager.php b/Filter/FilterManager.php index 856763cb..0d1b8078 100644 --- a/Filter/FilterManager.php +++ b/Filter/FilterManager.php @@ -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() { diff --git a/Filter/FilterManagerInterface.php b/Filter/FilterManagerInterface.php new file mode 100644 index 00000000..592f3952 --- /dev/null +++ b/Filter/FilterManagerInterface.php @@ -0,0 +1,27 @@ +getDefaultDriver(); - if (is_array($name)) { - $context = $name; + if (!is_string($name)) { + $context = is_array($name) ? $name : array($name); $name = null; } diff --git a/Manager/ManagerInterface.php b/Manager/ManagerInterface.php deleted file mode 100644 index 4fdcd898..00000000 --- a/Manager/ManagerInterface.php +++ /dev/null @@ -1,27 +0,0 @@ -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); + } } diff --git a/Presenter/AbstractPresenter.php b/Presenter/AbstractPresenter.php index bf37e041..7f368736 100644 --- a/Presenter/AbstractPresenter.php +++ b/Presenter/AbstractPresenter.php @@ -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))); diff --git a/Presenter/Adapter/HtmlPresenter.php b/Presenter/Adapter/HtmlPresenter.php index 40d52e81..d11b47ce 100644 --- a/Presenter/Adapter/HtmlPresenter.php +++ b/Presenter/Adapter/HtmlPresenter.php @@ -35,20 +35,20 @@ final class HtmlPresenter extends AbstractPresenter $html = << -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(); } HTML; - $this->storage->flush($envelopes); + $this->storageManager->flush($envelopes); return $html; } diff --git a/Presenter/Adapter/JsonPresenter.php b/Presenter/Adapter/JsonPresenter.php index e8eae938..3a5a88f0 100644 --- a/Presenter/Adapter/JsonPresenter.php +++ b/Presenter/Adapter/JsonPresenter.php @@ -34,7 +34,7 @@ final class JsonPresenter extends AbstractPresenter 'notifications' => $this->renderEnvelopes($envelopes), ); - $this->storage->flush($envelopes); + $this->storageManager->flush($envelopes); return $response; } diff --git a/Presenter/PresenterManager.php b/Presenter/PresenterManager.php index e7c1ccb3..6c4d2886 100644 --- a/Presenter/PresenterManager.php +++ b/Presenter/PresenterManager.php @@ -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 { } diff --git a/Presenter/PresenterManagerInterface.php b/Presenter/PresenterManagerInterface.php new file mode 100644 index 00000000..70e36067 --- /dev/null +++ b/Presenter/PresenterManagerInterface.php @@ -0,0 +1,27 @@ +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')); } diff --git a/Tests/Producer/ProducerManagerTest.php b/Tests/Producer/ProducerManagerTest.php index 64544c6b..ab0874a5 100644 --- a/Tests/Producer/ProducerManagerTest.php +++ b/Tests/Producer/ProducerManagerTest.php @@ -1,6 +1,6 @@