From f845d19c3f90f6011be7d2711dade206faa09cca Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Sun, 6 Dec 2020 04:03:14 +0100 Subject: [PATCH] fix event dispatcher and call the storage service --- .../Compiler/EventSubscriberCompilerPass.php | 28 +++++ .../Compiler/FactoryCompilerPass.php | 28 +++++ .../Compiler/FilterCompilerPass.php | 16 +-- .../Compiler/MiddlewareCompilerPass.php | 28 +++++ .../Compiler/PresenterCompilerPass.php | 16 +-- .../Compiler/ProducerCompilerPass.php | 32 ----- .../Compiler/RendererCompilerPass.php | 16 +-- DependencyInjection/Configuration.php | 10 +- ...tifyExtension.php => FlasherExtension.php} | 7 +- NotifyBundle.php => FlasherBundle.php | 10 +- Resources/config/aliases.yaml | 27 +++++ Resources/config/config.yaml | 35 ++++++ Resources/config/event_subscribers.yaml | 29 +++++ Resources/config/filters.yaml | 14 +++ Resources/config/middlewares.yaml | 23 ++++ Resources/config/presenters.yaml | 26 ++++ Resources/config/services.yaml | 92 ++------------ Resources/config/storage.yaml | 11 ++ Resources/config/twig.yaml | 7 ++ Resources/public/js/flasher.js | 114 ++++++++++++++++++ Storage/Storage.php | 98 ++++++++------- ...Extension.php => FlasherTwigExtension.php} | 7 +- 22 files changed, 465 insertions(+), 209 deletions(-) create mode 100644 DependencyInjection/Compiler/EventSubscriberCompilerPass.php create mode 100644 DependencyInjection/Compiler/FactoryCompilerPass.php create mode 100644 DependencyInjection/Compiler/MiddlewareCompilerPass.php delete mode 100644 DependencyInjection/Compiler/ProducerCompilerPass.php rename DependencyInjection/{NotifyExtension.php => FlasherExtension.php} (78%) rename NotifyBundle.php => FlasherBundle.php (53%) create mode 100644 Resources/config/aliases.yaml create mode 100644 Resources/config/config.yaml create mode 100644 Resources/config/event_subscribers.yaml create mode 100644 Resources/config/filters.yaml create mode 100644 Resources/config/middlewares.yaml create mode 100644 Resources/config/presenters.yaml create mode 100644 Resources/config/storage.yaml create mode 100644 Resources/config/twig.yaml create mode 100644 Resources/public/js/flasher.js rename Twig/{NotifyTwigExtension.php => FlasherTwigExtension.php} (71%) diff --git a/DependencyInjection/Compiler/EventSubscriberCompilerPass.php b/DependencyInjection/Compiler/EventSubscriberCompilerPass.php new file mode 100644 index 00000000..7632a88b --- /dev/null +++ b/DependencyInjection/Compiler/EventSubscriberCompilerPass.php @@ -0,0 +1,28 @@ +has('flasher.event_dispatcher')) { + return; + } + + /** @var EventDispatcherInterface $flasherBus */ + $eventDispatcher = $container->findDefinition('flasher.event_dispatcher'); + + foreach ($container->findTaggedServiceIds('flasher.event_subscriber') as $id => $tags) { + $eventDispatcher->addMethodCall('addSubscriber', array(new Reference($id))); + } + } +} diff --git a/DependencyInjection/Compiler/FactoryCompilerPass.php b/DependencyInjection/Compiler/FactoryCompilerPass.php new file mode 100644 index 00000000..3fbc2a6f --- /dev/null +++ b/DependencyInjection/Compiler/FactoryCompilerPass.php @@ -0,0 +1,28 @@ +has('flasher.factory_manager')) { + return; + } + + /** @var Flasher $manager */ + $manager = $container->findDefinition('flasher.factory_manager'); + + foreach ($container->findTaggedServiceIds('flasher.factory') as $id => $tags) { + $manager->addMethodCall('addDriver', array(new Reference($id))); + } + } +} diff --git a/DependencyInjection/Compiler/FilterCompilerPass.php b/DependencyInjection/Compiler/FilterCompilerPass.php index 38b44a55..34fc1bf7 100644 --- a/DependencyInjection/Compiler/FilterCompilerPass.php +++ b/DependencyInjection/Compiler/FilterCompilerPass.php @@ -2,6 +2,7 @@ namespace Flasher\Symfony\DependencyInjection\Compiler; +use Flasher\Prime\Filter\FilterManager; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -13,20 +14,15 @@ final class FilterCompilerPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - if (!$container->has('notify.filter')) { + if (!$container->has('flasher.filter_manager')) { return; } - /** @var \Flasher\Prime\Filter\FilterManager $manager */ - $manager = $container->findDefinition('notify.filter'); + /** @var FilterManager $manager */ + $manager = $container->findDefinition('flasher.filter_manager'); - foreach ($container->findTaggedServiceIds('notify.filter') as $id => $tags) { - foreach ($tags as $attributes) { - $manager->addMethodCall('addDriver', array( - $attributes['alias'], - new Reference($id), - )); - } + foreach ($container->findTaggedServiceIds('flasher.filter') as $id => $tags) { + $manager->addMethodCall('addDriver', array(new Reference($id))); } } } diff --git a/DependencyInjection/Compiler/MiddlewareCompilerPass.php b/DependencyInjection/Compiler/MiddlewareCompilerPass.php new file mode 100644 index 00000000..6fe28fd5 --- /dev/null +++ b/DependencyInjection/Compiler/MiddlewareCompilerPass.php @@ -0,0 +1,28 @@ +has('flasher.flasher_bus')) { + return; + } + + /** @var FlasherBus $flasherBus */ + $flasherBus = $container->findDefinition('flasher.flasher_bus'); + + foreach ($container->findTaggedServiceIds('flasher.middleware') as $id => $tags) { + $flasherBus->addMethodCall('addMiddleware', array(new Reference($id))); + } + } +} diff --git a/DependencyInjection/Compiler/PresenterCompilerPass.php b/DependencyInjection/Compiler/PresenterCompilerPass.php index e7874b10..c60b6812 100644 --- a/DependencyInjection/Compiler/PresenterCompilerPass.php +++ b/DependencyInjection/Compiler/PresenterCompilerPass.php @@ -2,6 +2,7 @@ namespace Flasher\Symfony\DependencyInjection\Compiler; +use Flasher\Prime\Presenter\PresenterManager; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -13,20 +14,15 @@ final class PresenterCompilerPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - if (!$container->has('notify.presenter')) { + if (!$container->has('flasher.presenter_manager')) { return; } - /** @var \Flasher\Prime\Presenter\PresenterManager $manager */ - $manager = $container->findDefinition('notify.presenter'); + /** @var PresenterManager $manager */ + $manager = $container->findDefinition('flasher.presenter_manager'); - foreach ($container->findTaggedServiceIds('notify.presenter') as $id => $tags) { - foreach ($tags as $attributes) { - $manager->addMethodCall('addDriver', array( - $attributes['alias'], - new Reference($id), - )); - } + foreach ($container->findTaggedServiceIds('flasher.presenter') as $id => $tags) { + $manager->addMethodCall('addDriver', array(new Reference($id))); } } } diff --git a/DependencyInjection/Compiler/ProducerCompilerPass.php b/DependencyInjection/Compiler/ProducerCompilerPass.php deleted file mode 100644 index 8356c75b..00000000 --- a/DependencyInjection/Compiler/ProducerCompilerPass.php +++ /dev/null @@ -1,32 +0,0 @@ -has('notify.producer')) { - return; - } - - /** @var \Flasher\Prime\Flasher $manager */ - $manager = $container->findDefinition('notify.producer'); - - foreach ($container->findTaggedServiceIds('notify.producer') as $id => $tags) { - foreach ($tags as $attributes) { - $manager->addMethodCall('addDriver', array( - $attributes['alias'], - new Reference($id), - )); - } - } - } -} diff --git a/DependencyInjection/Compiler/RendererCompilerPass.php b/DependencyInjection/Compiler/RendererCompilerPass.php index 2939f256..5f1b29b1 100644 --- a/DependencyInjection/Compiler/RendererCompilerPass.php +++ b/DependencyInjection/Compiler/RendererCompilerPass.php @@ -2,6 +2,7 @@ namespace Flasher\Symfony\DependencyInjection\Compiler; +use Flasher\Prime\Renderer\RendererManager; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -13,20 +14,15 @@ final class RendererCompilerPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - if (!$container->has('notify.renderer')) { + if (!$container->has('flasher.renderer_manager')) { return; } - /** @var \Flasher\Prime\Renderer\RendererManager $manager */ - $manager = $container->findDefinition('notify.renderer'); + /** @var RendererManager $manager */ + $manager = $container->findDefinition('flasher.renderer_manager'); - foreach ($container->findTaggedServiceIds('notify.renderer') as $id => $tags) { - foreach ($tags as $attributes) { - $manager->addMethodCall('addDriver', array( - $attributes['alias'], - new Reference($id), - )); - } + foreach ($container->findTaggedServiceIds('flasher.renderer') as $id => $tags) { + $manager->addMethodCall('addDriver', array(new Reference($id))); } } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index befd1f23..09be3337 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -12,13 +12,13 @@ final class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder('notify'); + $treeBuilder = new TreeBuilder('flasher'); if (\method_exists($treeBuilder, 'getRootNode')) { $rootNode = $treeBuilder->getRootNode(); } else { // BC layer for symfony/config 4.1 and older - $rootNode = $treeBuilder->root('notify'); + $rootNode = $treeBuilder->root('flasher'); } $rootNode @@ -30,7 +30,7 @@ final class Configuration implements ConfigurationInterface ->arrayNode('scripts') ->prototype('scalar')->end() ->defaultValue(array( - '/vendor/php-flasher/flasher/assets/js/notify.js' + '/bundles/flasher/js/flasher.js' )) ->end() ->arrayNode('styles') @@ -42,10 +42,6 @@ final class Configuration implements ConfigurationInterface ->useAttributeAsKey('name') ->prototype('variable')->end() ->end() - ->arrayNode('stamps_middlewares') - ->ignoreExtraKeys(false) - ->prototype('variable')->end() - ->end() ->end() ; diff --git a/DependencyInjection/NotifyExtension.php b/DependencyInjection/FlasherExtension.php similarity index 78% rename from DependencyInjection/NotifyExtension.php rename to DependencyInjection/FlasherExtension.php index ba33db63..7661e4ab 100644 --- a/DependencyInjection/NotifyExtension.php +++ b/DependencyInjection/FlasherExtension.php @@ -6,9 +6,8 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; -use Flasher\Symfony\Config\Config; -final class NotifyExtension extends Extension +final class FlasherExtension extends Extension { /** * {@inheritdoc} @@ -18,12 +17,12 @@ final class NotifyExtension extends Extension public function load(array $configs, ContainerBuilder $container) { $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - $loader->load('services.yaml'); + $loader->load('config.yaml'); $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $notifyConfig = $container->getDefinition('notify.config'); + $notifyConfig = $container->getDefinition('flasher.config'); $notifyConfig->replaceArgument(0, $config); } } diff --git a/NotifyBundle.php b/FlasherBundle.php similarity index 53% rename from NotifyBundle.php rename to FlasherBundle.php index cbaa07df..531fafd7 100644 --- a/NotifyBundle.php +++ b/FlasherBundle.php @@ -2,21 +2,25 @@ namespace Flasher\Symfony; +use Flasher\Symfony\DependencyInjection\Compiler\EventSubscriberCompilerPass; use Flasher\Symfony\DependencyInjection\Compiler\FilterCompilerPass; -use Flasher\Symfony\DependencyInjection\Compiler\ProducerCompilerPass; +use Flasher\Symfony\DependencyInjection\Compiler\FactoryCompilerPass; +use Flasher\Symfony\DependencyInjection\Compiler\MiddlewareCompilerPass; use Flasher\Symfony\DependencyInjection\Compiler\RendererCompilerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; -class NotifyBundle extends Bundle +class FlasherBundle extends Bundle { /** * {@inheritdoc} */ public function build(ContainerBuilder $container) { - $container->addCompilerPass(new ProducerCompilerPass()); + $container->addCompilerPass(new FactoryCompilerPass()); $container->addCompilerPass(new RendererCompilerPass()); $container->addCompilerPass(new FilterCompilerPass()); + $container->addCompilerPass(new MiddlewareCompilerPass()); + $container->addCompilerPass(new EventSubscriberCompilerPass()); } } diff --git a/Resources/config/aliases.yaml b/Resources/config/aliases.yaml new file mode 100644 index 00000000..0f601d68 --- /dev/null +++ b/Resources/config/aliases.yaml @@ -0,0 +1,27 @@ +services: + Flasher\Prime\Config\Config: '@flasher.config' + Flasher\Prime\Manager\AbstractManager: '@flasher.abstract_manager' + Flasher\Prime\Flasher: '@flasher' + Flasher\Prime\Presenter\PresenterManager: '@flasher.presenter_manager' + Flasher\Prime\Renderer\RendererManager: '@flasher.renderer_manager' + Flasher\Prime\Middleware\FlasherBus: '@flasher.flasher_bus' + Flasher\Prime\EventDispatcher\EventDispatcher: '@flasher.event_dispatcher' + Flasher\Symfony\Storage\Storage: '@flasher.storage' + Flasher\Prime\Storage\StorageManager: '@flasher.storage_manager' + Flasher\Prime\Filter\FilterManager: '@flasher.filter_manager' + Flasher\Prime\Filter\FilterBuilder: '@flasher.filter_builder' + Flasher\Prime\Filter\DefaultFilter: '@flasher.filter.default' + Flasher\Prime\Factory\AbstractFactory: '@flasher.abstract_factory' + Flasher\Prime\Presenter\AbstractPresenter: '@flasher.abstract_presenter' + Flasher\Prime\Presenter\Adapter\JsonPresenter: '@flasher.presenter.json' + Flasher\Prime\Presenter\Adapter\HtmlPresenter: '@flasher.presenter.html' + + flasher.factory_manager: '@flasher' + Flasher\Prime\FlasherInterface: '@flasher' + Flasher\Prime\Storage\StorageManagerInterface: '@flasher.storage_manager' + Flasher\Prime\Renderer\RendererManagerInterface: '@flasher.renderer_manager' + Flasher\Prime\Presenter\PresenterManagerInterface: '@flasher.presenter_manager' + Flasher\Prime\Middleware\FlasherBusInterface: '@flasher.flasher_bus' + Flasher\Prime\Filter\FilterManagerInterface: '@flasher.filter_manager' + Flasher\Prime\EventDispatcher\EventDispatcherInterface: '@flasher.event_dispatcher' + Flasher\Prime\Storage\StorageInterface: '@flasher.storage' diff --git a/Resources/config/config.yaml b/Resources/config/config.yaml new file mode 100644 index 00000000..103377f2 --- /dev/null +++ b/Resources/config/config.yaml @@ -0,0 +1,35 @@ +imports: +# - { resource: 'services.yaml' } + - { resource: 'middlewares.yaml' } + - { resource: 'storage.yaml' } + - { resource: 'presenters.yaml' } + - { resource: 'filters.yaml' } + - { resource: 'twig.yaml' } + - { resource: 'event_subscribers.yaml' } + - { resource: 'aliases.yaml' } + +services: + flasher.config: + class: Flasher\Prime\Config\Config + arguments: + - null + + flasher.abstract_manager: + class: Flasher\Prime\Manager\AbstractManager + abstract: true + arguments: + - '@flasher.config' + + flasher: + class: Flasher\Prime\Flasher + parent: 'flasher.abstract_manager' + + flasher.abstract_factory: + class: Flasher\Prime\Factory\AbstractFactory + abstract: true + arguments: + - '@flasher.event_dispatcher' + + flasher.renderer_manager: + class: Flasher\Prime\Renderer\RendererManager + parent: 'flasher.abstract_manager' diff --git a/Resources/config/event_subscribers.yaml b/Resources/config/event_subscribers.yaml new file mode 100644 index 00000000..32679a92 --- /dev/null +++ b/Resources/config/event_subscribers.yaml @@ -0,0 +1,29 @@ +services: + flasher.event_dispatcher: + class: Flasher\Prime\EventDispatcher\EventDispatcher + + Flasher\Prime\EventDispatcher\EventListener\PostFilterListener: + public: false + tags: + - 'flasher.event_subscriber' + + Flasher\Prime\EventDispatcher\EventListener\PostFlushListener: + public: false + arguments: + - '@flasher.storage' + tags: + - 'flasher.event_subscriber' + + Flasher\Prime\EventDispatcher\EventListener\MiddlewareListener: + public: false + arguments: + - '@flasher.flasher_bus' + tags: + - 'flasher.event_subscriber' + + Flasher\Prime\EventDispatcher\EventListener\StorageListener: + public: false + arguments: + - '@flasher.storage' + tags: + - 'flasher.event_subscriber' diff --git a/Resources/config/filters.yaml b/Resources/config/filters.yaml new file mode 100644 index 00000000..b3b036e9 --- /dev/null +++ b/Resources/config/filters.yaml @@ -0,0 +1,14 @@ +services: + flasher.filter_manager: + class: Flasher\Prime\Filter\FilterManager + parent: 'flasher.abstract_manager' + + flasher.filter_builder: + class: Flasher\Prime\Filter\FilterBuilder + + flasher.filter.default: + class: Flasher\Prime\Filter\DefaultFilter + arguments: + - '@flasher.filter_builder' + tags: + - 'flasher.filter' diff --git a/Resources/config/middlewares.yaml b/Resources/config/middlewares.yaml new file mode 100644 index 00000000..f628eff4 --- /dev/null +++ b/Resources/config/middlewares.yaml @@ -0,0 +1,23 @@ +services: + flasher.flasher_bus: + class: Flasher\Prime\Middleware\FlasherBus + + Flasher\Prime\Middleware\AddCreatedAtStampMiddleware: + public: false + tags: + - 'flasher.middleware' + + Flasher\Prime\Middleware\AddHopsStampMiddleware: + public: false + tags: + - 'flasher.middleware' + + Flasher\Prime\Middleware\AddPriorityStampMiddleware: + public: false + tags: + - 'flasher.middleware' + + Flasher\Prime\Middleware\AddRenderedAtStampMiddleware: + public: false + tags: + - 'flasher.middleware' diff --git a/Resources/config/presenters.yaml b/Resources/config/presenters.yaml new file mode 100644 index 00000000..7af2bff1 --- /dev/null +++ b/Resources/config/presenters.yaml @@ -0,0 +1,26 @@ +services: + flasher.presenter_manager: + class: Flasher\Prime\Presenter\PresenterManager + parent: 'flasher.abstract_manager' + + flasher.abstract_presenter: + class: Flasher\Prime\Presenter\AbstractPresenter + abstract: true + arguments: + - '@flasher.event_dispatcher' + - '@flasher.config' + - '@flasher.storage_manager' + - '@flasher.filter_manager' + - '@flasher.renderer_manager' + + flasher.presenter.html: + class: Flasher\Prime\Presenter\Adapter\HtmlPresenter + parent: 'flasher.abstract_presenter' + tags: + - 'flasher.presenter' + + flasher.presenter.json: + class: Flasher\Prime\Presenter\Adapter\JsonPresenter + parent: 'flasher.abstract_presenter' + tags: + - 'flasher.presenter' diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index f6bf8abb..e967a885 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -1,91 +1,25 @@ services: - notify.config: + flasher.config: class: Flasher\Prime\Config\Config arguments: - null - notify.producer: - class: Flasher\Prime\Producer\ProducerManager + flasher.abstract_manager: + class: Flasher\Prime\Manager\AbstractManager + abstract: true arguments: - - '@notify.config' + - '@flasher.config' - notify.presenter: - class: Flasher\Prime\Presenter\PresenterManager + flasher: + class: Flasher\Prime\Flasher + parent: 'flasher.abstract_manager' - notify.renderer: + flasher.renderer_manager: class: Flasher\Prime\Renderer\RendererManager - arguments: - - '@notify.config' + parent: 'flasher.abstract_manager' - notify.middleware: - class: Flasher\Prime\Middleware\MiddlewareManager - arguments: - - '@notify.config' - - notify.storage: - class: Flasher\Symfony\Storage\Storage - arguments: - - '@session' - - notify.filter: - class: Flasher\Prime\Filter\FilterManager - arguments: - - '@notify.config' - - notify.producer.abstract: - class: Flasher\Prime\Producer\AbstractProducer + flasher.abstract_factory: + class: Flasher\Prime\Factory\AbstractFactory abstract: true arguments: - - '@notify.storage' - - '@notify.middleware' - - notify.presenter.abstract: - class: Flasher\Prime\Presenter\AbstractPresenter - abstract: true - arguments: - - '@notify.config' - - '@notify.storage' - - '@notify.filter' - - '@notify.renderer' - - notify.presenter.html: - class: Flasher\Prime\Presenter\Adapter\HtmlPresenter - parent: 'notify.presenter.abstract' - tags: - - { name: 'notify.presenter', alias: 'html' } - - notify.presenter.json: - class: Flasher\Prime\Presenter\Adapter\JsonPresenter - parent: 'notify.presenter.abstract' - tags: - - { name: 'notify.presenter', alias: 'json' } - - notify.twig_extension: - class: Flasher\Symfony\TwigFlasher\PrimeTwigExtension - arguments: - - '@notify.presenter.html' - public: false - tags: - - { name: twig.extension } - - notify.filter_builder: - class: Flasher\Prime\Filter\FilterBuilder - - notify.filter.default: - class: Flasher\Prime\Filter\DefaultFilter - arguments: - - '@notify.filter_builder' - tags: - - { name: 'notify.filter', alias: 'default' } - - Flasher\Prime\Config\Config: '@notify.config' - Flasher\Prime\Producer\ProducerManager: '@notify.producer' - Flasher\Prime\Presenter\PresenterManager: '@notify.presenter' - Flasher\Prime\Renderer\RendererManager: '@notify.renderer' - Flasher\Prime\Middleware\MiddlewareManager: '@notify.middleware' - Flasher\Symfony\Storage\Storage: '@notify.storage' - Flasher\Prime\Filter\FilterManager: '@notify.filter' - Flasher\Prime\Filter\DefaultFilter: '@notify.filter.default' - Flasher\Prime\Filter\FilterBuilder: '@notify.filter_builder' - Flasher\Prime\Presenter\Json\JsonPresenter: '@notify.presenter.json' - Flasher\Prime\Presenter\Html\HtmlPresenter: '@notify.presenter.html' + - '@flasher.flasher_bus' diff --git a/Resources/config/storage.yaml b/Resources/config/storage.yaml new file mode 100644 index 00000000..6ffa1ced --- /dev/null +++ b/Resources/config/storage.yaml @@ -0,0 +1,11 @@ +services: + flasher.storage: + class: Flasher\Symfony\Storage\Storage + arguments: + - '@session' + + flasher.storage_manager: + class: Flasher\Prime\Storage\StorageManager + arguments: + - '@flasher.storage' + - '@flasher.event_dispatcher' diff --git a/Resources/config/twig.yaml b/Resources/config/twig.yaml new file mode 100644 index 00000000..3fa176ea --- /dev/null +++ b/Resources/config/twig.yaml @@ -0,0 +1,7 @@ +services: + Flasher\Symfony\Twig\FlasherTwigExtension: + class: + arguments: + - '@flasher.presenter.html' + tags: + - 'twig.extension' diff --git a/Resources/public/js/flasher.js b/Resources/public/js/flasher.js new file mode 100644 index 00000000..074beb38 --- /dev/null +++ b/Resources/public/js/flasher.js @@ -0,0 +1,114 @@ +/*! + * PHPFlasher js 1.0.0 + * https://github.com/php-flasher/flasher + * @license MIT licensed + * + * Copyright (C) 2020 Younes KHOUBZA + */ +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + define([], factory(root)); + } else if (typeof exports === 'object') { + module.exports = factory(root); + } else { + root.PHPFlasher = factory(root); + } +})(typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : this, function (window) { + + 'use strict'; + + var exports = {}; + + exports.render = function (_settings) { + var settings = Object.assign({}, { + scripts: [], + styles: [], + options: [], + notifications: [], + }, _settings); + + exports.addStyles(settings.styles, function () { + exports.addScripts(settings.scripts, function () { + var script = ''; + + settings.options.forEach(function (option) { + script += option + "\n"; + }); + + script += "\n\n"; + + settings.notifications.forEach(function (notification) { + script += notification.code + "\n"; + }); + + exports.parseScript(script); + }); + }); + }; + + exports.addStyles = function (urls, callback) { + if (0 === urls.length) { + if ("function" === typeof callback) { + callback(); + } + + return this; + } + + if (null !== document.querySelector(`link[href='${urls[0]}']`)) { + return exports.addStyles(urls.slice(1), callback); + } + + var tag = document.createElement('link'); + + tag.href = urls[0]; + tag.type = 'text/css'; + tag.rel = 'stylesheet'; + tag.onload = function () { + exports.addStyles(urls.slice(1), callback); + }; + + document.head.appendChild(tag); + + return this; + }; + + exports.addScripts = function (urls, callback) { + if (0 === urls.length) { + if ("function" === typeof callback) { + callback(); + } + + return this; + } + + if (null !== document.querySelector(`script[src='${urls[0]}']`)) { + return exports.addScripts(urls.slice(1), callback); + } + + var tag = document.createElement('script'); + + tag.src = urls[0]; + tag.type = 'text/javascript'; + tag.onload = function () { + exports.addScripts(urls.slice(1), callback); + }; + + document.body.appendChild(tag); + + return this; + }; + + exports.parseScript = function (script) { + var tag = document.createElement('script'); + + tag.type = "text/javascript"; + tag.text = script; + + document.body.appendChild(tag); + + return this; + }; + + return exports; +}); diff --git a/Storage/Storage.php b/Storage/Storage.php index 5913906c..6e243edb 100644 --- a/Storage/Storage.php +++ b/Storage/Storage.php @@ -3,79 +3,48 @@ namespace Flasher\Symfony\Storage; use Flasher\Prime\Envelope; -use Flasher\Prime\Stamp\CreatedAtStamp; -use Flasher\Prime\Stamp\LifeStamp; use Flasher\Prime\Stamp\UuidStamp; use Flasher\Prime\Storage\StorageInterface; use Symfony\Component\HttpFoundation\Session\Session; final class Storage implements StorageInterface { - const ENVELOPES_NAMESPACE = 'notify::envelopes'; + const ENVELOPES_NAMESPACE = 'flasher::envelopes'; + /** + * @var Session + */ private $session; + /** + * @param Session $session + */ public function __construct(Session $session) { $this->session = $session; } - public function get() + /** + * @inheritDoc + */ + public function all() { return $this->session->get(self::ENVELOPES_NAMESPACE, array()); } - public function add(Envelope $envelope) - { - if (null === $envelope->get('Flasher\Prime\Stamp\UuidStamp')) { - $envelope->withStamp(new UuidStamp()); - } - - if (null === $envelope->get('Flasher\Prime\Stamp\UuidStamp')) { - $envelope->withStamp(new UuidStamp()); - } - - if (null === $envelope->get('Flasher\Prime\Stamp\LifeStamp')) { - $envelope->withStamp(new LifeStamp(1)); - } - - if (null === $envelope->get('Flasher\Prime\Stamp\CreatedAtStamp')) { - $envelope->withStamp(new CreatedAtStamp()); - } - - $envelopes = $this->get(); - $envelopes[] = $envelope; - - $this->session->set(self::ENVELOPES_NAMESPACE, $envelopes); - } - /** - * @param Envelope[] $envelopes + * @inheritDoc */ - public function flush($envelopes) + public function add($envelopes) { - $envelopesMap = array(); + $envelopes = is_array($envelopes) ? $envelopes : func_get_args(); + $envelopes = array_merge($envelopes, $this->all()); + + $store = $this->all(); foreach ($envelopes as $envelope) { - $life = $envelope->get('Flasher\Prime\Stamp\LifeStamp')->getLife(); - $uuid = $envelope->get('Flasher\Prime\Stamp\UuidStamp')->getUuid(); - - $envelopesMap[$uuid] = $life; - } - - $store = array(); - - foreach ($this->session->get(self::ENVELOPES_NAMESPACE, array()) as $envelope) { - $uuid = $envelope->get('Flasher\Prime\Stamp\UuidStamp')->getUuid(); - - if(isset($envelopesMap[$uuid])) { - $life = $envelopesMap[$uuid] - 1; - - if ($life <= 0) { - continue; - } - - $envelope->with(new LifeStamp($life)); + if (null === $envelope->get('Flasher\Prime\Stamp\UuidStamp')) { + $envelope->withStamp(new UuidStamp()); } $store[] = $envelope; @@ -83,4 +52,33 @@ final class Storage implements StorageInterface $this->session->set(self::ENVELOPES_NAMESPACE, $store); } + + /** + * @inheritDoc + */ + public function remove($envelopes) + { + $envelopes = is_array($envelopes) ? $envelopes : func_get_args(); + + $map = UuidStamp::indexWithUuid($envelopes); + + $store = array_filter( + $this->all(), + function (Envelope $envelope) use ($map) { + $uuid = $envelope->get('Flasher\Prime\Stamp\UuidStamp')->getUuid(); + + return !isset($map[$uuid]); + } + ); + + $this->session->set(self::ENVELOPES_NAMESPACE, $store); + } + + /** + * @inheritDoc + */ + public function clear() + { + $this->session->set(self::ENVELOPES_NAMESPACE, array()); + } } diff --git a/Twig/NotifyTwigExtension.php b/Twig/FlasherTwigExtension.php similarity index 71% rename from Twig/NotifyTwigExtension.php rename to Twig/FlasherTwigExtension.php index 9cc18061..a7af9906 100644 --- a/Twig/NotifyTwigExtension.php +++ b/Twig/FlasherTwigExtension.php @@ -3,11 +3,10 @@ namespace Flasher\Symfony\Twig; use Flasher\Prime\Presenter\Adapter\HtmlPresenter; -use Flasher\Prime\Presenter\PresenterManager; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; -final class NotifyTwigExtension extends AbstractExtension +final class FlasherTwigExtension extends AbstractExtension { private $htmlPresenter; @@ -21,7 +20,7 @@ final class NotifyTwigExtension extends AbstractExtension $options = array('is_safe' => array('html')); return array( - new TwigFunction('notify_render', array($this, 'notifyRender'), $options), + new TwigFunction('flasher_render', array($this, 'flasherRender'), $options), ); } @@ -30,7 +29,7 @@ final class NotifyTwigExtension extends AbstractExtension * * @return string */ - public function notifyRender($criteria = null) + public function flasherRender($criteria = null) { return $this->htmlPresenter->render($criteria); }