mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-03-31 15:07:47 +01:00
resolve circular dependecy between event_dispatcher and storage_manager
This commit is contained in:
+3
-6
@@ -14,14 +14,11 @@
|
||||
"require": {
|
||||
"php": ">=5.3",
|
||||
"ext-json": "*",
|
||||
"symfony/config": "^2.7|^3.0|^4.0|^5.0",
|
||||
"symfony/dependency-injection": "^2.7|^3.0|^4.0|^5.0",
|
||||
"symfony/http-kernel": "^2.7|^3.0|^4.0|^5.0",
|
||||
"symfony/yaml": "^2.7|^3.0|^4.0|^5.0",
|
||||
"twig/twig": "^1.34|^2.0|^3.0"
|
||||
"illuminate/support": "^4.0|^5.0|^6.0|^7.0|^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "4.8.*"
|
||||
"phpunit/phpunit": "4.8.*",
|
||||
"orchestra/testbench": "^2.0|^3.0|^4.0|^5.0|^6.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
@@ -78,19 +78,20 @@ class Laravel implements ServiceProviderInterface
|
||||
return new StorageManager($app['flasher.storage'], $app['flasher.event_dispatcher']);
|
||||
});
|
||||
|
||||
$this->app->singleton('flasher.filter', function (Application $app) {
|
||||
return new Filter();
|
||||
});
|
||||
|
||||
$this->app->singleton('flasher.event_dispatcher', function (Application $app) {
|
||||
$eventDispatcher = new EventDispatcher();
|
||||
|
||||
$eventDispatcher->addSubscriber(new FilterListener($app['flasher.filter']));
|
||||
$eventDispatcher->addSubscriber(new RemoveListener($app['flasher.storage_manager']));
|
||||
$eventDispatcher->addSubscriber(new RemoveListener());
|
||||
$eventDispatcher->addSubscriber(new StampsListener());
|
||||
|
||||
return $eventDispatcher;
|
||||
});
|
||||
|
||||
$this->app->singleton('flasher.filter', function (Application $app) {
|
||||
return new Filter();
|
||||
});
|
||||
|
||||
$this->app->alias('flasher.config', 'Flasher\Laravel\Config\Config');
|
||||
$this->app->alias('flasher', 'Flasher\Prime\Flasher');
|
||||
$this->app->alias('flasher.renderer', 'Flasher\Prime\Renderer\Renderer');
|
||||
@@ -111,7 +112,10 @@ class Laravel implements ServiceProviderInterface
|
||||
|
||||
public function registerBladeDirectives()
|
||||
{
|
||||
Blade::directive('flasher_render', function ($criteria = null) {
|
||||
Blade::directive('flasher_render', function ($criteria = array()) {
|
||||
if (empty($criteria)) {
|
||||
$criteria = "array()";
|
||||
}
|
||||
return "<?php echo app('flasher.renderer')->render($criteria, 'html'); ?>";
|
||||
});
|
||||
}
|
||||
|
||||
@@ -88,6 +88,6 @@ final class Storage implements StorageInterface
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->session->set(self::ENVELOPES_NAMESPACE, array());
|
||||
$this->session->put(self::ENVELOPES_NAMESPACE, array());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,12 @@ final class FlasherServiceProviderTest extends TestCase
|
||||
public function testNotifyServiceExists()
|
||||
{
|
||||
$this->assertTrue($this->app->bound('flasher'));
|
||||
$this->assertTrue($this->app->bound('flasher.renderer'));
|
||||
$this->assertTrue($this->app->bound('flasher.storage'));
|
||||
$this->assertTrue($this->app->bound('flasher.storage_manager'));
|
||||
$this->assertTrue($this->app->bound('flasher.event_dispatcher'));
|
||||
$this->assertTrue($this->app->bound('flasher.filter'));
|
||||
$this->assertTrue($this->app->bound('flasher.config'));
|
||||
}
|
||||
|
||||
public function testNotifyManagerGetConfig()
|
||||
@@ -27,6 +33,6 @@ final class FlasherServiceProviderTest extends TestCase
|
||||
/** @var BladeCompiler $blade */
|
||||
$blade = $this->app->make('view')->getEngineResolver()->resolve('blade')->getCompiler();
|
||||
|
||||
$this->assertEquals("<?php echo app('flasher.presenter.html')->render(); ?>", $blade->compileString('@flasher_render'));
|
||||
$this->assertEquals("<?php echo app('flasher.renderer')->render(array(), 'html'); ?>", $blade->compileString('@flasher_render()'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,16 +6,115 @@ use Flasher\Laravel\Storage\Storage;
|
||||
use Flasher\Laravel\Tests\TestCase;
|
||||
use Flasher\Prime\Envelope;
|
||||
use Flasher\Prime\Notification\Notification;
|
||||
use Flasher\Prime\Stamp\PriorityStamp;
|
||||
use Flasher\Prime\Stamp\UuidStamp;
|
||||
|
||||
final class StorageTest extends TestCase
|
||||
{
|
||||
public function testSimple()
|
||||
public function testInitialState()
|
||||
{
|
||||
$session = new Storage($this->app->make('session'));
|
||||
$storage = new Storage($this->app->make('session'));
|
||||
$this->assertSame(array(), $storage->all());
|
||||
}
|
||||
|
||||
$envelope = new Envelope(new Notification('success', 'message'));
|
||||
$session->add($envelope);
|
||||
public function testAddEnvelope()
|
||||
{
|
||||
$storage = new Storage($this->app->make('session'));
|
||||
$envelope = new Envelope(new Notification());
|
||||
$storage->add($envelope);
|
||||
|
||||
$this->assertEquals(array($envelope), $session->all());
|
||||
$this->assertSame(array($envelope), $storage->all());
|
||||
}
|
||||
|
||||
public function testAddMultipleEnvelopes()
|
||||
{
|
||||
$storage = new Storage($this->app->make('session'));
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification()),
|
||||
new Envelope(new Notification()),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertSame($envelopes, $storage->all());
|
||||
}
|
||||
|
||||
public function testUpdateEnvelopes()
|
||||
{
|
||||
$storage = new Storage($this->app->make('session'));
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertSame($envelopes, $storage->all());
|
||||
|
||||
$envelopes[1]->withStamp(new PriorityStamp(1));
|
||||
$storage->update($envelopes[1]);
|
||||
|
||||
$this->assertSame($envelopes, $storage->all());
|
||||
$this->assertInstanceOf('Flasher\Prime\Stamp\PriorityStamp',
|
||||
$envelopes[1]->get('Flasher\Prime\Stamp\PriorityStamp'));
|
||||
}
|
||||
|
||||
public function testRemoveEnvelopes()
|
||||
{
|
||||
$storage = new Storage($this->app->make('session'));
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertSame($envelopes, $storage->all());
|
||||
|
||||
$storage->remove($envelopes[1]);
|
||||
$this->assertSame(array($envelopes[0]), $storage->all());
|
||||
}
|
||||
|
||||
public function testRemoveMultipleEnvelopes()
|
||||
{
|
||||
$storage = new Storage($this->app->make('session'));
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertSame($envelopes, $storage->all());
|
||||
|
||||
$storage->remove($envelopes);
|
||||
$this->assertSame(array(), $storage->all());
|
||||
}
|
||||
|
||||
public function testClearAllEnvelopes()
|
||||
{
|
||||
$storage = new Storage($this->app->make('session'));
|
||||
$envelopes = array(
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
new Envelope(new Notification(), array(
|
||||
new UuidStamp(),
|
||||
)),
|
||||
);
|
||||
|
||||
$storage->add($envelopes);
|
||||
$this->assertSame($envelopes, $storage->all());
|
||||
|
||||
$storage->clear();
|
||||
$this->assertSame(array(), $storage->all());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class Laravel implements ServiceProviderInterface
|
||||
$this->app->alias('flasher.noty', 'Flasher\Noty\Prime\NotyFactory');
|
||||
|
||||
$this->app->extend('flasher', function (Flasher $flasher, Container $app) {
|
||||
$flasher->addFactory('toastr', $app['flasher.noty']);
|
||||
$flasher->addFactory('noty', $app['flasher.noty']);
|
||||
|
||||
return $flasher;
|
||||
});
|
||||
|
||||
@@ -6,38 +6,54 @@ class FlasherNotyServiceProviderTest extends TestCase
|
||||
{
|
||||
public function testContainerContainNotifyServices()
|
||||
{
|
||||
$this->assertTrue($this->app->bound('flasher'));
|
||||
$this->assertTrue($this->app->bound('flasher.factory.noty'));
|
||||
$this->assertTrue($this->app->bound('flasher.noty'));
|
||||
$this->assertInstanceOf('Flasher\Noty\Prime\NotyFactory', $this->app->make('flasher.noty'));
|
||||
}
|
||||
|
||||
public function testNotifyFactoryIsAddedToExtensionsArray()
|
||||
{
|
||||
$flasher = $this->app->make('flasher');
|
||||
$adapter = $flasher->create('noty');
|
||||
|
||||
$reflection = new \ReflectionClass($flasher);
|
||||
$property = $reflection->getProperty('drivers');
|
||||
$property->setAccessible(true);
|
||||
|
||||
$extensions = $property->getValue($flasher);
|
||||
|
||||
$this->assertCount(1, $extensions);
|
||||
$this->assertInstanceOf('Flasher\Prime\Factory\FactoryInterface', $extensions[0]);
|
||||
$this->assertInstanceOf('Flasher\Noty\Prime\NotyFactory', $adapter);
|
||||
}
|
||||
|
||||
public function testConfigNotyInjectedInGlobalNotifyConfig()
|
||||
{
|
||||
$flasher = $this->app->make('flasher');
|
||||
$config = $this->app->make('flasher.config');
|
||||
|
||||
$reflection = new \ReflectionClass($flasher);
|
||||
$property = $reflection->getProperty('config');
|
||||
$property->setAccessible(true);
|
||||
$expected = array(
|
||||
'noty' => array(
|
||||
'scripts' => array(
|
||||
'https://cdnjs.cloudflare.com/ajax/libs/noty/3.1.4/noty.min.js',
|
||||
),
|
||||
'styles' => array(
|
||||
'https://cdnjs.cloudflare.com/ajax/libs/noty/3.1.4/noty.min.css',
|
||||
),
|
||||
'options' => array(
|
||||
'layout' => 'topRight',
|
||||
'theme' => 'mint',
|
||||
'timeout' => 5000,
|
||||
'progressBar' => true,
|
||||
'animation.open' => 'noty_effects_open',
|
||||
'animation.close' => 'noty_effects_close',
|
||||
'sounds.sources' => array(),
|
||||
'closeWith' => array('click'),
|
||||
'sounds.volume' => 1,
|
||||
'sounds.conditions' => array(),
|
||||
'docTitle.conditions' => array(),
|
||||
'modal' => false,
|
||||
'id' => false,
|
||||
'force' => false,
|
||||
'queue' => 'global',
|
||||
'killer' => false,
|
||||
'container' => false,
|
||||
'buttons' => array(),
|
||||
'visibilityControl' => false,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$config = $property->getValue($flasher);
|
||||
|
||||
$this->assertArrayHasKey('noty', $config->get('adapters'));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'noty' => array('scripts' => array('jquery.js'), 'styles' => array('styles.css'), 'options' => array()),
|
||||
), $config->get('adapters'));
|
||||
$this->assertEquals($expected, $config->get('adapters'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,9 +32,6 @@ class TestCase extends Orchestra
|
||||
$separator = $this->isLaravel4() ? '::config' : '';
|
||||
|
||||
$app['config']->set('session'.$separator.'.driver', 'array');
|
||||
$app['config']->set('flasher'.$separator.'.adapters', array(
|
||||
'noty' => array('scripts' => array('jquery.js'), 'styles' => array('styles.css'), 'options' => array()),
|
||||
));
|
||||
}
|
||||
|
||||
private function isLaravel4()
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Flasher\Prime\EventDispatcher\Event;
|
||||
|
||||
use Flasher\Prime\Envelope;
|
||||
|
||||
final class PreFlushEvent
|
||||
{
|
||||
/**
|
||||
* @var Envelope[]
|
||||
*/
|
||||
private $envelopes;
|
||||
|
||||
/**
|
||||
* @param Envelope[] $envelopes
|
||||
*/
|
||||
public function __construct(array $envelopes)
|
||||
{
|
||||
$this->envelopes = $envelopes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Envelope[]
|
||||
*/
|
||||
public function getEnvelopes()
|
||||
{
|
||||
return $this->envelopes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Envelope[] $envelopes
|
||||
*/
|
||||
public function setEnvelopes(array $envelopes)
|
||||
{
|
||||
$this->envelopes = $envelopes;
|
||||
}
|
||||
}
|
||||
@@ -9,29 +9,50 @@ final class RemoveEvent
|
||||
/**
|
||||
* @var Envelope[]
|
||||
*/
|
||||
private $envelopes;
|
||||
private $envelopesToRemove = array();
|
||||
|
||||
/**
|
||||
* @param Envelope[] $envelopes
|
||||
* @var Envelope[]
|
||||
*/
|
||||
public function __construct(array $envelopes)
|
||||
private $envelopesToKeep = array();
|
||||
|
||||
/**
|
||||
* @param Envelope[] $envelopesToRemove
|
||||
*/
|
||||
public function __construct(array $envelopesToRemove)
|
||||
{
|
||||
$this->envelopes = $envelopes;
|
||||
$this->envelopesToRemove = $envelopesToRemove;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Envelope[]
|
||||
*/
|
||||
public function getEnvelopes()
|
||||
public function getEnvelopesToRemove()
|
||||
{
|
||||
return $this->envelopes;
|
||||
return $this->envelopesToRemove;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Envelope[] $envelopes
|
||||
* @param Envelope[] $envelopesToRemove
|
||||
*/
|
||||
public function setEnvelopes(array $envelopes)
|
||||
public function setEnvelopesToRemove($envelopesToRemove)
|
||||
{
|
||||
$this->envelopes = $envelopes;
|
||||
$this->envelopesToRemove = $envelopesToRemove;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Envelope[]
|
||||
*/
|
||||
public function getEnvelopesToKeep()
|
||||
{
|
||||
return $this->envelopesToKeep;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Envelope[] $envelopesToKeep
|
||||
*/
|
||||
public function setEnvelopesToKeep($envelopesToKeep)
|
||||
{
|
||||
$this->envelopesToKeep = $envelopesToKeep;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,34 +2,20 @@
|
||||
|
||||
namespace Flasher\Prime\EventDispatcher\EventListener;
|
||||
|
||||
use Flasher\Prime\EventDispatcher\Event\PreFlushEvent;
|
||||
use Flasher\Prime\EventDispatcher\Event\RemoveEvent;
|
||||
use Flasher\Prime\Stamp\HopsStamp;
|
||||
use Flasher\Prime\Storage\StorageManagerInterface;
|
||||
|
||||
final class RemoveListener implements EventSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var StorageManagerInterface
|
||||
* @param RemoveEvent $event
|
||||
*/
|
||||
private $storageManager;
|
||||
|
||||
/**
|
||||
* @param StorageManagerInterface $storageManager
|
||||
*/
|
||||
public function __construct(StorageManagerInterface $storageManager)
|
||||
public function __invoke(RemoveEvent $event)
|
||||
{
|
||||
$this->storageManager = $storageManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PreFlushEvent $event
|
||||
*/
|
||||
public function __invoke(PreFlushEvent $event)
|
||||
{
|
||||
$envelopesToKeep = array();
|
||||
$envelopesToKeep = $event->getEnvelopesToKeep();
|
||||
$envelopesToRemove = array();
|
||||
|
||||
foreach ($event->getEnvelopes() as $envelope) {
|
||||
foreach ($event->getEnvelopesToRemove() as $envelope) {
|
||||
$hopsStamp = $envelope->get('Flasher\Prime\Stamp\HopsStamp');
|
||||
|
||||
if (1 === $hopsStamp->getAmount()) {
|
||||
@@ -41,8 +27,8 @@ final class RemoveListener implements EventSubscriberInterface
|
||||
$envelopesToKeep[] = $envelope;
|
||||
}
|
||||
|
||||
$event->setEnvelopes($envelopesToRemove);
|
||||
$this->storageManager->update($envelopesToKeep);
|
||||
$event->setEnvelopesToKeep($envelopesToKeep);
|
||||
$event->setEnvelopesToRemove($envelopesToRemove);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,6 +36,6 @@ final class RemoveListener implements EventSubscriberInterface
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return 'Flasher\Prime\EventDispatcher\Event\PreFlushEvent';
|
||||
return 'Flasher\Prime\EventDispatcher\Event\RemoveEvent';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,11 +14,11 @@ abstract class AbstractFactory implements FactoryInterface
|
||||
protected $storageManager;
|
||||
|
||||
/**
|
||||
* @param StorageManagerInterface $eventDispatcher
|
||||
* @param StorageManagerInterface $storageManager
|
||||
*/
|
||||
public function __construct(StorageManagerInterface $eventDispatcher)
|
||||
public function __construct(StorageManagerInterface $storageManager)
|
||||
{
|
||||
$this->storageManager = $eventDispatcher;
|
||||
$this->storageManager = $storageManager;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -73,7 +73,8 @@ final class StorageManager implements StorageManagerInterface
|
||||
$event = new RemoveEvent($envelopes);
|
||||
$this->eventDispatcher->dispatch($event);
|
||||
|
||||
$this->storage->remove($event->getEnvelopes());
|
||||
$this->storage->update($event->getEnvelopesToKeep());
|
||||
$this->storage->remove($event->getEnvelopesToRemove());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,7 +25,7 @@ final class CreatedAtStampTest extends TestCase
|
||||
$createdAt1 = new CreatedAtStamp(new \DateTime('+2 h'));
|
||||
$createdAt2 = new CreatedAtStamp(new \DateTime('+1 h'));
|
||||
|
||||
$this->assertFalse($createdAt1->compare($createdAt2));
|
||||
$this->assertSame(false, $createdAt1->compare(new HopsStamp(1)));
|
||||
$this->assertNotNull($createdAt1->compare($createdAt2));
|
||||
$this->assertSame(1, $createdAt1->compare(new HopsStamp(1)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ final class PriorityStampTest extends TestCase
|
||||
$stamp1 = new PriorityStamp(1);
|
||||
$stamp2 = new PriorityStamp(2);
|
||||
|
||||
$this->assertFalse($stamp1->compare($stamp2));
|
||||
$this->assertSame(false, $stamp1->compare(new HopsStamp(1)));
|
||||
$this->assertNotNull($stamp1->compare($stamp2));
|
||||
$this->assertSame(1, $stamp1->compare(new HopsStamp(1)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +46,6 @@ services:
|
||||
flasher.event_listener.post_flush_listener:
|
||||
class: Flasher\Prime\EventDispatcher\EventListener\RemoveListener
|
||||
public: false
|
||||
arguments:
|
||||
- '@flasher.storage_manager'
|
||||
tags:
|
||||
- { name: 'flasher.event_subscriber' }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user