From 219722714e1989ca4eebc2e946a06e72f0bd52a1 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Sun, 6 Dec 2020 16:34:11 +0100 Subject: [PATCH] update composer.json --- Config/Config.php | 2 +- ...Provider.php => FlasherServiceProvider.php | 2 +- Resources/config/config.php | 4 - Resources/public/{notify.js => flasher.js} | 4 +- ServiceProvider/Providers/Laravel.php | 121 +++++++++++------- ServiceProvider/Providers/Laravel4.php | 11 +- ServiceProvider/Providers/Laravel50.php | 2 +- ServiceProvider/Providers/Lumen.php | 4 +- .../Providers/ServiceProviderInterface.php | 8 +- ServiceProvider/ServiceProviderManager.php | 5 +- Storage/Storage.php | 110 +++++++++------- composer.json | 14 +- helpers.php | 4 +- 13 files changed, 167 insertions(+), 124 deletions(-) rename NotifyServiceProvider.php => FlasherServiceProvider.php (95%) rename Resources/public/{notify.js => flasher.js} (97%) diff --git a/Config/Config.php b/Config/Config.php index f2e391bc..52ac8e3e 100644 --- a/Config/Config.php +++ b/Config/Config.php @@ -19,6 +19,6 @@ final class Config implements ConfigInterface public function get($key, $default = null) { - return $this->config->get('notify'.$this->separator.$key, $default); + return $this->config->get('flasher'.$this->separator.$key, $default); } } diff --git a/NotifyServiceProvider.php b/FlasherServiceProvider.php similarity index 95% rename from NotifyServiceProvider.php rename to FlasherServiceProvider.php index df899f58..7f2d953c 100644 --- a/NotifyServiceProvider.php +++ b/FlasherServiceProvider.php @@ -5,7 +5,7 @@ namespace Flasher\Laravel; use Illuminate\Support\ServiceProvider; use Flasher\Laravel\ServiceProvider\ServiceProviderManager; -final class NotifyServiceProvider extends ServiceProvider +final class FlasherServiceProvider extends ServiceProvider { /** * Bootstrap the application events. diff --git a/Resources/config/config.php b/Resources/config/config.php index 3b6fa8af..31eff3ed 100644 --- a/Resources/config/config.php +++ b/Resources/config/config.php @@ -14,8 +14,4 @@ return array( 'adapters' => array( ), - - 'stamps_middlewares' => array( - - ), ); diff --git a/Resources/public/notify.js b/Resources/public/flasher.js similarity index 97% rename from Resources/public/notify.js rename to Resources/public/flasher.js index 0e78e7b5..1e19623d 100644 --- a/Resources/public/notify.js +++ b/Resources/public/flasher.js @@ -1,5 +1,5 @@ /*! - * PHPNotify js 1.0.0 + * PHPFlasher js 0.1.0 * https://github.com/php-flasher/flasher * @license MIT licensed * @@ -11,7 +11,7 @@ } else if (typeof exports === 'object') { module.exports = factory(root); } else { - root.PHPNotify = factory(root); + root.PHPFlasher = factory(root); } })(typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : this, function (window) { diff --git a/ServiceProvider/Providers/Laravel.php b/ServiceProvider/Providers/Laravel.php index b5a1b172..c8829687 100644 --- a/ServiceProvider/Providers/Laravel.php +++ b/ServiceProvider/Providers/Laravel.php @@ -3,17 +3,27 @@ namespace Flasher\Laravel\ServiceProvider\Providers; use Flasher\Laravel\Config\Config; -use Flasher\LaravelFlasher\PrimeServiceProvider; +use Flasher\Laravel\FlasherServiceProvider; use Flasher\Laravel\Storage\Storage; +use Flasher\Prime\EventDispatcher\EventDispatcher; +use Flasher\Prime\EventDispatcher\EventListener\MiddlewareListener; +use Flasher\Prime\EventDispatcher\EventListener\PostFilterListener; +use Flasher\Prime\EventDispatcher\EventListener\PostFlushListener; +use Flasher\Prime\EventDispatcher\EventListener\StorageListener; use Flasher\Prime\Filter\DefaultFilter; use Flasher\Prime\Filter\FilterBuilder; use Flasher\Prime\Filter\FilterManager; use Flasher\Prime\Flasher; -use Flasher\Prime\Middleware\MiddlewareManager; +use Flasher\Prime\Middleware\AddCreatedAtStampMiddleware; +use Flasher\Prime\Middleware\AddDelayStampMiddleware; +use Flasher\Prime\Middleware\AddHopsStampMiddleware; +use Flasher\Prime\Middleware\AddPriorityStampMiddleware; +use Flasher\Prime\Middleware\FlasherBus; use Flasher\Prime\Presenter\Adapter\HtmlPresenter; use Flasher\Prime\Presenter\Adapter\JsonPresenter; use Flasher\Prime\Presenter\PresenterManager; use Flasher\Prime\Renderer\RendererManager; +use Flasher\Prime\Storage\StorageManager; use Illuminate\Container\Container; use Illuminate\Foundation\Application; use Illuminate\Support\Facades\Blade; @@ -32,21 +42,21 @@ class Laravel implements ServiceProviderInterface return $this->app instanceof Application; } - public function publishConfig(NotifyServiceProvider $provider) + public function publishConfig(FlasherServiceProvider $provider) { $source = realpath($raw = __DIR__.'/../../../resources/config/config.php') ?: $raw; $provider->publishes(array($source => config_path('flasher.php')), 'config'); - $provider->mergeConfigFrom($source, 'notify'); + $provider->mergeConfigFrom($source, 'flasher'); } - public function publishAssets(NotifyServiceProvider $provider) + public function publishAssets(FlasherServiceProvider $provider) { $provider->publishes(array(__DIR__.'/../../../public' => public_path('vendor/php-flasher/flasher/assets/js')), 'public'); } - public function registerNotifyServices() + public function registerServices() { $this->app->singleton('flasher.config', function (Application $app) { return new Config($app['config'], '.'); @@ -57,74 +67,93 @@ class Laravel implements ServiceProviderInterface public function registerCommonServices() { - $this->app->singleton('flasher.factory', function (Application $app) { + $this->app->singleton('flasher', function (Application $app) { return new Flasher($app['flasher.config']); }); + $this->app->singleton('flasher.renderer_manager', function (Application $app) { + return new RendererManager($app['flasher.config']); + }); + + $this->app->singleton('flasher.flasher_bus', function (Application $app) { + $bus = new FlasherBus(); + $bus->addMiddleware(new AddCreatedAtStampMiddleware()); + $bus->addMiddleware(new AddHopsStampMiddleware()); + $bus->addMiddleware(new AddPriorityStampMiddleware()); + $bus->addMiddleware(new AddDelayStampMiddleware()); + + return $bus; + }); + $this->app->singleton('flasher.storage', function (Application $app) { return new Storage($app['session']); }); - $this->app->singleton('flasher.filter', function (Application $app) { - return new FilterManager($app['flasher.config']); + $this->app->singleton('flasher.storage_manager', function (Application $app) { + return new StorageManager($app['flasher.storage'], $app['flasher.event_dispatcher']); }); - $this->app->singleton('flasher.renderer', function (Application $app) { - return new RendererManager($app['flasher.config']); + $this->app->singleton('flasher.event_dispatcher', function (Application $app) { + $eventDispatcher = new EventDispatcher(); + $eventDispatcher->addSubscriber(new PostFilterListener($app['flasher.storage'])); + $eventDispatcher->addSubscriber(new PostFlushListener($app['flasher.storage'])); + $eventDispatcher->addSubscriber(new MiddlewareListener($app['flasher.flasher_bus'])); + $eventDispatcher->addSubscriber(new StorageListener($app['flasher.storage'])); + + return $eventDispatcher; }); - $this->app->singleton('flasher.presenter', function (Application $app) { - return new PresenterManager(); - }); + $this->app->singleton('flasher.filter_manager', function (Application $app) { + $filterManager = new FilterManager($app['flasher.config']); + $filterManager->addDriver(new DefaultFilter($app['flasher.filter_builder'])); - $this->app->singleton('flasher.presenter.html', function (Application $app) { - return new HtmlPresenter($app['flasher.config'], $app['flasher.storage'], $app['flasher.filter'], $app['flasher.renderer']); - }); - - $this->app->singleton('flasher.presenter.json', function (Application $app) { - return new JsonPresenter($app['flasher.config'], $app['flasher.storage'], $app['flasher.filter'], $app['flasher.renderer']); + return $filterManager; }); $this->app->singleton('flasher.filter_builder', function (Application $app) { return new FilterBuilder(); }); - $this->app->singleton('flasher.filter.default', function (Application $app) { - return new DefaultFilter($app['flasher.filter_builder']); + $this->app->singleton('flasher.presenter_manager', function (Application $app) { + $presenterManager = new PresenterManager($app['flasher.config']); + $presenterManager->addDriver($app['flasher.presenter.html']); + $presenterManager->addDriver($app['flasher.presenter.json']); + + return $presenterManager; }); - $this->app->singleton('flasher.middleware', function (Application $app) { - return new MiddlewareManager($app['flasher.config']); + $this->app->singleton('flasher.presenter.html', function (Application $app) { + return new HtmlPresenter($app['flasher.event_dispatcher'], $app['flasher.config'], $app['flasher.storage_manager'], $app['flasher.filter_manager'], $app['flasher.renderer_manager']); }); - $this->app->extend('flasher.presenter', function (PresenterManager $manager, Container $app) { - $manager->addDriver('html', $app['flasher.presenter.html']); - - return $manager; - }); - - $this->app->extend('flasher.presenter', function (PresenterManager $manager, Container $app) { - $manager->addDriver('json', $app['flasher.presenter.json']); - - return $manager; - }); - - $this->app->extend('flasher.filter', function (FilterManager $manager, Container $app) { - $manager->addDriver('default', $app['flasher.filter.default']); - - return $manager; + $this->app->singleton('flasher.presenter.json', function (Application $app) { + return new JsonPresenter($app['flasher.event_dispatcher'], $app['flasher.config'], $app['flasher.storage_manager'], $app['flasher.filter_manager'], $app['flasher.renderer_manager']); }); $this->app->alias('flasher.config', 'Flasher\Laravel\Config\Config'); - $this->app->alias('flasher.factory', 'Flasher\Prime\Flasher'); - $this->app->alias('flasher.presenter', 'Flasher\Prime\Presenter\PresenterManager'); - $this->app->alias('flasher.middleware', 'Flasher\Prime\Middleware\MiddlewareManager'); + $this->app->alias('flasher', 'Flasher\Prime\Flasher'); + $this->app->alias('flasher.presenter_manager', 'Flasher\Prime\Presenter\PresenterManager'); + $this->app->alias('flasher.renderer_manager', 'Flasher\Prime\Renderer\RendererManager'); + $this->app->alias('flasher.flasher_bus', 'Flasher\Prime\Middleware\FlasherBus'); + $this->app->alias('flasher.event_dispatcher', 'Flasher\Prime\EventDispatcher\EventDispatcher'); $this->app->alias('flasher.storage', 'Flasher\Laravel\Storage\Storage'); - $this->app->alias('flasher.filter', 'Flasher\Prime\Filter\FilterManager'); - $this->app->alias('flasher.presenter.html', 'Flasher\Prime\Presenter\Adapter\HtmlPresenter'); - $this->app->alias('flasher.presenter.json', 'Flasher\Prime\Presenter\Adapter\JsonPresenter'); + $this->app->alias('flasher.storage_manager', 'Flasher\Laravel\Storage\StorageManager'); + $this->app->alias('flasher.filter_manager', 'Flasher\Prime\Filter\FilterManager'); $this->app->alias('flasher.filter_builder', 'Flasher\Prime\Filter\FilterBuilder'); $this->app->alias('flasher.filter.default', 'Flasher\Prime\Filter\DefaultFilter'); + $this->app->alias('flasher.presenter.html', 'Flasher\Prime\Presenter\Adapter\HtmlPresenter'); + $this->app->alias('flasher.presenter.json', 'Flasher\Prime\Presenter\Adapter\JsonPresenter'); + + $this->app->alias('flasher', 'flasher.factory_manager'); + + $this->app->bind('Flasher\Prime\FlasherInterface', 'flasher'); + $this->app->bind('Flasher\Prime\Storage\StorageManagerInterface', 'flasher.storage_manager'); + $this->app->bind('Flasher\Prime\Renderer\RendererManagerInterface', 'flasher.renderer_manager'); + $this->app->bind('Flasher\Prime\Presenter\PresenterManagerInterface', 'flasher.presenter_manager'); + $this->app->bind('Flasher\Prime\Middleware\FlasherBusInterface', 'flasher.flasher_bus'); + $this->app->bind('Flasher\Prime\Filter\FilterManagerInterface', 'flasher.filter_manager'); + $this->app->bind('Flasher\Prime\EventDispatcher\EventDispatcherInterface', 'flasher.event_dispatcher'); + $this->app->bind('Flasher\Prime\Storage\StorageInterface', 'flasher.storage'); } public function registerBladeDirectives() diff --git a/ServiceProvider/Providers/Laravel4.php b/ServiceProvider/Providers/Laravel4.php index 1adab6a1..50a2f2b3 100644 --- a/ServiceProvider/Providers/Laravel4.php +++ b/ServiceProvider/Providers/Laravel4.php @@ -2,11 +2,10 @@ namespace Flasher\Laravel\ServiceProvider\Providers; +use Flasher\Laravel\FlasherServiceProvider; use Illuminate\Foundation\Application; use Illuminate\Support\Facades\Blade; use Flasher\Laravel\Config\Config; -use Flasher\LaravelFlasher\PrimeServiceProvider; -use Flasher\Prime\Middleware\MiddlewareManager; final class Laravel4 extends Laravel { @@ -15,16 +14,16 @@ final class Laravel4 extends Laravel return $this->app instanceof Application && 0 === strpos(Application::VERSION, '4.'); } - public function publishConfig(NotifyServiceProvider $provider) + public function publishConfig(FlasherServiceProvider $provider) { $provider->package('php-flasher/flasher-laravel', 'notify', __DIR__.'/../../../resources'); } - public function publishAssets(NotifyServiceProvider $provider) + public function publishAssets(FlasherServiceProvider $provider) { } - public function registerNotifyServices() + public function registerServices() { $this->app->singleton('flasher.config', function (Application $app) { return new Config($app['config'], '::'); @@ -36,7 +35,7 @@ final class Laravel4 extends Laravel public function registerBladeDirectives() { Blade::extend(function ($view, $compiler) { - $pattern = $compiler->createPlainMatcher('notify_render(.*)'); + $pattern = $compiler->createPlainMatcher('flasher_render(.*)'); return preg_replace($pattern, '$1render($2); ?>', $view); }); diff --git a/ServiceProvider/Providers/Laravel50.php b/ServiceProvider/Providers/Laravel50.php index d129a0ae..0a3f4397 100644 --- a/ServiceProvider/Providers/Laravel50.php +++ b/ServiceProvider/Providers/Laravel50.php @@ -15,7 +15,7 @@ final class Laravel50 extends Laravel public function registerBladeDirectives() { Blade::extend(function ($view, $compiler) { - $pattern = $compiler->createPlainMatcher('notify_render(.*)'); + $pattern = $compiler->createPlainMatcher('flasher_render(.*)'); return preg_replace($pattern, '$1render($2); ?>', $view); }); diff --git a/ServiceProvider/Providers/Lumen.php b/ServiceProvider/Providers/Lumen.php index 7844872b..b36dbfd1 100644 --- a/ServiceProvider/Providers/Lumen.php +++ b/ServiceProvider/Providers/Lumen.php @@ -21,11 +21,11 @@ final class Lumen extends Laravel $provider->mergeConfigFrom($source, 'notify'); } - public function registerNotifyServices() + public function registerServices() { $this->app->register('\Illuminate\Session\SessionServiceProvider'); $this->app->configure('session'); - parent::registerNotifyServices(); + parent::registerServices(); } } diff --git a/ServiceProvider/Providers/ServiceProviderInterface.php b/ServiceProvider/Providers/ServiceProviderInterface.php index 533f703e..898c05a8 100644 --- a/ServiceProvider/Providers/ServiceProviderInterface.php +++ b/ServiceProvider/Providers/ServiceProviderInterface.php @@ -2,17 +2,17 @@ namespace Flasher\Laravel\ServiceProvider\Providers; -use Flasher\LaravelFlasher\PrimeServiceProvider; +use Flasher\Laravel\FlasherServiceProvider; interface ServiceProviderInterface { public function shouldBeUsed(); - public function publishConfig(NotifyServiceProvider $provider); + public function publishConfig(FlasherServiceProvider $provider); - public function publishAssets(NotifyServiceProvider $provider); + public function publishAssets(FlasherServiceProvider $provider); - public function registerNotifyServices(); + public function registerServices(); public function registerBladeDirectives(); } diff --git a/ServiceProvider/ServiceProviderManager.php b/ServiceProvider/ServiceProviderManager.php index b5ed7f2d..cfb2a43e 100644 --- a/ServiceProvider/ServiceProviderManager.php +++ b/ServiceProvider/ServiceProviderManager.php @@ -2,6 +2,7 @@ namespace Flasher\Laravel\ServiceProvider; +use Flasher\Laravel\FlasherServiceProvider; use Flasher\LaravelFlasher\PrimeServiceProvider; use Flasher\Laravel\ServiceProvider\Providers\ServiceProviderInterface; @@ -21,7 +22,7 @@ final class ServiceProviderManager private $notifyServiceProvider; - public function __construct(NotifyServiceProvider $notifyServiceProvider) + public function __construct(FlasherServiceProvider $notifyServiceProvider) { $this->notifyServiceProvider = $notifyServiceProvider; } @@ -38,7 +39,7 @@ final class ServiceProviderManager public function register() { $provider = $this->resolveServiceProvider(); - $provider->registerNotifyServices(); + $provider->registerServices(); } /** diff --git a/Storage/Storage.php b/Storage/Storage.php index c4a493be..b1557b3d 100644 --- a/Storage/Storage.php +++ b/Storage/Storage.php @@ -3,78 +3,45 @@ namespace Flasher\Laravel\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; final class Storage implements StorageInterface { - const ENVELOPES_NAMESPACE = 'notify::envelopes'; + const ENVELOPES_NAMESPACE = 'flasher::envelopes'; /** * @var \Illuminate\Session\SessionManager|\Illuminate\Session\Store */ private $session; + /** + * @param \Illuminate\Session\SessionManager|\Illuminate\Session\Store $session + */ public function __construct($session) { $this->session = $session; } + /** + * @inheritDoc + */ public function get() { 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\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->put(self::ENVELOPES_NAMESPACE, $envelopes); - } - /** - * @param \Flasher\Prime\Envelope[] $envelopes + * @inheritDoc */ - public function flush($envelopes) + public function add($envelopes) { - $envelopesMap = array(); + $envelopes = is_array($envelopes) ? $envelopes : func_get_args(); + $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; @@ -82,4 +49,55 @@ final class Storage implements StorageInterface $this->session->put(self::ENVELOPES_NAMESPACE, $store); } + + /** + * @inheritDoc + */ + public function update($envelopes) + { + $envelopes = is_array($envelopes) ? $envelopes : func_get_args(); + $map = UuidStamp::indexWithUuid($envelopes); + + $store = $this->all(); + foreach ($store as $index => $envelope) { + $uuid = $envelope->get('Flasher\Prime\Stamp\UuidStamp')->getUuid(); + + if (!isset($map[$uuid])) { + continue; + } + + $store[$index] = $map[$uuid]; + } + + $this->session->put(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->put(self::ENVELOPES_NAMESPACE, $store); + } + + /** + * @inheritDoc + */ + public function clear() + { + $this->session->set(self::ENVELOPES_NAMESPACE, array()); + } } diff --git a/composer.json b/composer.json index ed03140d..22fdd5a4 100644 --- a/composer.json +++ b/composer.json @@ -28,16 +28,16 @@ "license": "MIT", "require": { "php": ">=5.3", - "illuminate/support": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0", - "php-flasher/flasher": "^1.0" + "illuminate/support": "^4.0|^5.0|^6.0|^7.0|^8.0", + "php-flasher/flasher": "dev-main" }, "require-dev": { - "phpunit/phpunit": "^4.8 || ^5.7 || ^6.0 || ^7.0 || ^8.3 || ^9.0", - "orchestra/testbench": "^2.0 || ^3.0 || ^4.0 || ^5.0 || ^6.0" + "phpunit/phpunit": "^4.8|^5.7|^6.0|^7.0|^8.3|^9.0", + "orchestra/testbench": "^2.0|^3.0|^4.0|^5.0|^6.0" }, "autoload": { "psr-4": { - "Flasher\\Laravel\\": "src/" + "Flasher\\Laravel\\": "" }, "files": [ "helpers.php" @@ -45,13 +45,13 @@ }, "autoload-dev": { "psr-4": { - "Flasher\\Laravel\\Tests\\": "tests/" + "Flasher\\Laravel\\Tests\\": "Tests/" } }, "extra": { "laravel": { "providers": [ - "Flasher\\Laravel\Flasher\PrimeServiceProvider" + "Flasher\\Laravel\\FlasherServiceProvider" ], "aliases": { "Flasher": "Flasher\\Laravel\\Facades\\Flasher" diff --git a/helpers.php b/helpers.php index 683e4a49..b61789ae 100644 --- a/helpers.php +++ b/helpers.php @@ -1,6 +1,6 @@