You've already forked php-flasher
mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-04-05 12:32:55 +01:00
convert session flash messages into flasher messages
This commit is contained in:
+2
-12
@@ -13,22 +13,12 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3",
|
"php": ">=5.3",
|
||||||
"ext-json": "*",
|
"ext-json": "*"
|
||||||
"illuminate/support": "4.0.*"
|
|
||||||
},
|
|
||||||
"require-dev": {
|
|
||||||
"phpunit/phpunit": "4.8.36",
|
|
||||||
"orchestra/testbench": "^2.0|^3.0|^4.0|^5.0|^6.0"
|
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Flasher\\": "src/"
|
"Flasher\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev"
|
||||||
"config": {
|
|
||||||
"platform": {
|
|
||||||
"php": "5.4"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flasher\Laravel\Middleware;
|
||||||
|
|
||||||
|
use Flasher\Prime\Config\ConfigInterface;
|
||||||
|
use Flasher\Prime\FlasherInterface;
|
||||||
|
use Flasher\Prime\Presenter\Adapter\HtmlPresenter;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
|
||||||
|
final class SessionMiddleware
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ConfigInterface
|
||||||
|
*/
|
||||||
|
private $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var FlasherInterface
|
||||||
|
*/
|
||||||
|
private $flasher;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var HtmlPresenter
|
||||||
|
*/
|
||||||
|
private $htmlPresenter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FlasherInterface $flasher
|
||||||
|
* @param HtmlPresenter $htmlPresenter
|
||||||
|
*/
|
||||||
|
public function __construct(ConfigInterface $config, FlasherInterface $flasher, HtmlPresenter $htmlPresenter)
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
$this->flasher = $flasher;
|
||||||
|
$this->htmlPresenter = $htmlPresenter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the request filter.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param \Closure $next
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle(Request $request, \Closure $next)
|
||||||
|
{
|
||||||
|
if ($request->isXmlHttpRequest() || true !== $this->config->get('auto_create_from_session')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Response $response
|
||||||
|
*/
|
||||||
|
$response = $next($request);
|
||||||
|
|
||||||
|
foreach ($this->typesMapping() as $alias => $type) {
|
||||||
|
if (false === $request->session()->has($alias)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->flasher->type($type, $request->session()->get($alias))->dispatch();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$content = $response->getContent();
|
||||||
|
$pos = strripos($content, '</body>');
|
||||||
|
$content = substr($content, 0, $pos).$this->htmlPresenter->render().substr($content, $pos);
|
||||||
|
$response->setContent($content);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function typesMapping()
|
||||||
|
{
|
||||||
|
$mapping = array();
|
||||||
|
|
||||||
|
foreach ($this->config->get('types_mapping', array()) as $type => $aliases) {
|
||||||
|
if (is_int($type) && is_string($aliases)) {
|
||||||
|
$type = $aliases;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((array)$aliases as $alias) {
|
||||||
|
$mapping[$alias] = $type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $mapping;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,4 +6,13 @@ return array(
|
|||||||
'scripts' => array(
|
'scripts' => array(
|
||||||
'/vendor/php-flasher/flasher/assets/js/flasher.js'
|
'/vendor/php-flasher/flasher/assets/js/flasher.js'
|
||||||
),
|
),
|
||||||
|
|
||||||
|
'auto_create_from_session' => true,
|
||||||
|
|
||||||
|
'types_mapping' => array(
|
||||||
|
'success' => array('success'),
|
||||||
|
'error' => array('error', 'danger'),
|
||||||
|
'warning' => array('warning', 'alarm'),
|
||||||
|
'info' => array('info', 'notice', 'alert'),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ class Laravel implements ServiceProviderInterface
|
|||||||
|
|
||||||
public function publishAssets(FlasherServiceProvider $provider)
|
public function publishAssets(FlasherServiceProvider $provider)
|
||||||
{
|
{
|
||||||
$provider->publishes(array(__DIR__.'/../../public' => public_path('vendor/php-flasher/flasher/assets/js')), 'public');
|
$provider->publishes(array(__DIR__.'/../../Resources/public' => public_path('vendor/php-flasher/flasher/assets/js')), 'public');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function registerServices()
|
public function registerServices()
|
||||||
@@ -146,6 +146,7 @@ class Laravel implements ServiceProviderInterface
|
|||||||
|
|
||||||
$this->app->alias('flasher', 'flasher.factory_manager');
|
$this->app->alias('flasher', 'flasher.factory_manager');
|
||||||
|
|
||||||
|
$this->app->bind('Flasher\Prime\Config\ConfigInterface', 'flasher.config');
|
||||||
$this->app->bind('Flasher\Prime\FlasherInterface', 'flasher');
|
$this->app->bind('Flasher\Prime\FlasherInterface', 'flasher');
|
||||||
$this->app->bind('Flasher\Prime\Storage\StorageManagerInterface', 'flasher.storage_manager');
|
$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\Renderer\RendererManagerInterface', 'flasher.renderer_manager');
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ use Illuminate\Container\Container;
|
|||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Flasher\Noty\Laravel\ServiceProvider\ServiceProviderManager;
|
use Flasher\Noty\Laravel\ServiceProvider\ServiceProviderManager;
|
||||||
|
|
||||||
final class FlasherNotyfServiceProvider extends ServiceProvider
|
final class FlasherNotyServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
@@ -4,7 +4,7 @@ namespace Flasher\Noty\Laravel\ServiceProvider\Providers;
|
|||||||
|
|
||||||
use Flasher\Prime\Flasher;
|
use Flasher\Prime\Flasher;
|
||||||
use Flasher\Prime\Renderer\RendererManager;
|
use Flasher\Prime\Renderer\RendererManager;
|
||||||
use Flasher\Noty\Laravel\FlasherNotyfServiceProvider;
|
use Flasher\Noty\Laravel\FlasherNotyServiceProvider;
|
||||||
use Flasher\Noty\Prime\NotyFactory;
|
use Flasher\Noty\Prime\NotyFactory;
|
||||||
use Flasher\Noty\Prime\NotyRenderer;
|
use Flasher\Noty\Prime\NotyRenderer;
|
||||||
use Illuminate\Container\Container;
|
use Illuminate\Container\Container;
|
||||||
@@ -24,7 +24,7 @@ class Laravel implements ServiceProviderInterface
|
|||||||
return $this->app instanceof Application;
|
return $this->app instanceof Application;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function publishConfig(FlasherNotyfServiceProvider $provider)
|
public function publishConfig(FlasherNotyServiceProvider $provider)
|
||||||
{
|
{
|
||||||
$source = realpath($raw = __DIR__.'/../../Resources/config/config.php') ?: $raw;
|
$source = realpath($raw = __DIR__.'/../../Resources/config/config.php') ?: $raw;
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Flasher\Noty\Laravel\ServiceProvider\Providers;
|
namespace Flasher\Noty\Laravel\ServiceProvider\Providers;
|
||||||
|
|
||||||
use Flasher\Noty\Laravel\FlasherNotyfServiceProvider;
|
use Flasher\Noty\Laravel\FlasherNotyServiceProvider;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
|
|
||||||
final class Laravel4 extends Laravel
|
final class Laravel4 extends Laravel
|
||||||
@@ -12,7 +12,7 @@ final class Laravel4 extends Laravel
|
|||||||
return $this->app instanceof Application && 0 === strpos(Application::VERSION, '4.');
|
return $this->app instanceof Application && 0 === strpos(Application::VERSION, '4.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function publishConfig(FlasherNotyfServiceProvider $provider)
|
public function publishConfig(FlasherNotyServiceProvider $provider)
|
||||||
{
|
{
|
||||||
$provider->package('php-flasher/flasher-noty-laravel', 'flasher_noty', __DIR__.'/../../Resources');
|
$provider->package('php-flasher/flasher-noty-laravel', 'flasher_noty', __DIR__.'/../../Resources');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Flasher\Noty\Laravel\ServiceProvider\Providers;
|
namespace Flasher\Noty\Laravel\ServiceProvider\Providers;
|
||||||
|
|
||||||
use Flasher\Noty\Laravel\FlasherNotyfServiceProvider;
|
use Flasher\Noty\Laravel\FlasherNotyServiceProvider;
|
||||||
use Laravel\Lumen\Application;
|
use Laravel\Lumen\Application;
|
||||||
|
|
||||||
final class Lumen extends Laravel
|
final class Lumen extends Laravel
|
||||||
@@ -12,7 +12,7 @@ final class Lumen extends Laravel
|
|||||||
return $this->app instanceof Application;
|
return $this->app instanceof Application;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function publishConfig(FlasherNotyfServiceProvider $provider)
|
public function publishConfig(FlasherNotyServiceProvider $provider)
|
||||||
{
|
{
|
||||||
$source = realpath($raw = __DIR__.'/../../Resources/config/config.php') ?: $raw;
|
$source = realpath($raw = __DIR__.'/../../Resources/config/config.php') ?: $raw;
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
namespace Flasher\Noty\Laravel\ServiceProvider\Providers;
|
namespace Flasher\Noty\Laravel\ServiceProvider\Providers;
|
||||||
|
|
||||||
use Flasher\Noty\Laravel\FlasherNotyfServiceProvider;
|
use Flasher\Noty\Laravel\FlasherNotyServiceProvider;
|
||||||
|
|
||||||
interface ServiceProviderInterface
|
interface ServiceProviderInterface
|
||||||
{
|
{
|
||||||
public function shouldBeUsed();
|
public function shouldBeUsed();
|
||||||
|
|
||||||
public function publishConfig(FlasherNotyfServiceProvider $provider);
|
public function publishConfig(FlasherNotyServiceProvider $provider);
|
||||||
|
|
||||||
public function registerServices();
|
public function registerServices();
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Flasher\Noty\Laravel\ServiceProvider;
|
namespace Flasher\Noty\Laravel\ServiceProvider;
|
||||||
|
|
||||||
use Flasher\Noty\Laravel\FlasherNotyfServiceProvider;
|
use Flasher\Noty\Laravel\FlasherNotyServiceProvider;
|
||||||
use Flasher\Noty\Laravel\ServiceProvider\Providers\ServiceProviderInterface;
|
use Flasher\Noty\Laravel\ServiceProvider\Providers\ServiceProviderInterface;
|
||||||
|
|
||||||
final class ServiceProviderManager
|
final class ServiceProviderManager
|
||||||
@@ -20,7 +20,7 @@ final class ServiceProviderManager
|
|||||||
|
|
||||||
private $notifyServiceProvider;
|
private $notifyServiceProvider;
|
||||||
|
|
||||||
public function __construct(FlasherNotyfServiceProvider $notifyServiceProvider)
|
public function __construct(FlasherNotyServiceProvider $notifyServiceProvider)
|
||||||
{
|
{
|
||||||
$this->notifyServiceProvider = $notifyServiceProvider;
|
$this->notifyServiceProvider = $notifyServiceProvider;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class TestCase extends Orchestra
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'Flasher\Laravel\FlasherServiceProvider',
|
'Flasher\Laravel\FlasherServiceProvider',
|
||||||
'Flasher\Noty\Laravel\FlasherNotyfServiceProvider',
|
'Flasher\Noty\Laravel\FlasherNotyServiceProvider',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@
|
|||||||
"extra": {
|
"extra": {
|
||||||
"laravel": {
|
"laravel": {
|
||||||
"providers": [
|
"providers": [
|
||||||
"FlasherNotyfServiceProvider"
|
"Flasher\\Noty\\Laravel\\FlasherNotyServiceProvider"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -37,6 +37,18 @@ final class Configuration implements ConfigurationInterface
|
|||||||
->prototype('scalar')->end()
|
->prototype('scalar')->end()
|
||||||
->defaultValue(array())
|
->defaultValue(array())
|
||||||
->end()
|
->end()
|
||||||
|
->booleanNode('auto_create_from_session')
|
||||||
|
->defaultValue(true)
|
||||||
|
->end()
|
||||||
|
->arrayNode('types_mapping')
|
||||||
|
->prototype('variable')->end()
|
||||||
|
->defaultValue(array(
|
||||||
|
'success' => array('success'),
|
||||||
|
'error' => array('error', 'danger'),
|
||||||
|
'warning' => array('warning', 'alarm'),
|
||||||
|
'info' => array('info', 'notice', 'alert'),
|
||||||
|
))
|
||||||
|
->end()
|
||||||
->arrayNode('adapters')
|
->arrayNode('adapters')
|
||||||
->ignoreExtraKeys(false)
|
->ignoreExtraKeys(false)
|
||||||
->useAttributeAsKey('name')
|
->useAttributeAsKey('name')
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flasher\Symfony\EventListener;
|
||||||
|
|
||||||
|
use Flasher\Prime\Config\ConfigInterface;
|
||||||
|
use Flasher\Prime\FlasherInterface;
|
||||||
|
use Flasher\Prime\Presenter\Adapter\HtmlPresenter;
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
use Symfony\Component\HttpKernel\Event\ResponseEvent;
|
||||||
|
|
||||||
|
final class SessionListener implements EventSubscriberInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ConfigInterface
|
||||||
|
*/
|
||||||
|
private $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var FlasherInterface
|
||||||
|
*/
|
||||||
|
private $flasher;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var HtmlPresenter
|
||||||
|
*/
|
||||||
|
private $htmlPresenter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param FlasherInterface $flasher
|
||||||
|
* @param HtmlPresenter $htmlPresenter
|
||||||
|
*/
|
||||||
|
public function __construct(ConfigInterface $config, FlasherInterface $flasher, HtmlPresenter $htmlPresenter)
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
$this->flasher = $flasher;
|
||||||
|
$this->htmlPresenter = $htmlPresenter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onKernelResponse(ResponseEvent $event)
|
||||||
|
{
|
||||||
|
$request = $event->getRequest();
|
||||||
|
|
||||||
|
if (!$event->isMasterRequest() || $request->isXmlHttpRequest() || true !== $this->config->get('auto_create_from_session')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $event->getResponse();
|
||||||
|
|
||||||
|
$mapping = $this->typesMapping();
|
||||||
|
|
||||||
|
foreach ($request->getSession()->getFlashBag()->all() as $type => $messages) {
|
||||||
|
if (!isset($mapping[$type])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($messages as $message) {
|
||||||
|
$this->flasher->type($mapping[$type], $message)->dispatch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$content = $response->getContent();
|
||||||
|
$pos = strripos($content, '</body>');
|
||||||
|
$content = substr($content, 0, $pos).$this->htmlPresenter->render().substr($content, $pos);
|
||||||
|
$response->setContent($content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
'kernel.response' => 'onKernelResponse'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function typesMapping()
|
||||||
|
{
|
||||||
|
$mapping = array();
|
||||||
|
|
||||||
|
foreach ($this->config->get('types_mapping', array()) as $type => $aliases) {
|
||||||
|
if (is_int($type) && is_string($aliases)) {
|
||||||
|
$type = $aliases;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ((array) $aliases as $alias) {
|
||||||
|
$mapping[$alias] = $type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $mapping;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -144,6 +144,15 @@ services:
|
|||||||
tags:
|
tags:
|
||||||
- { name: 'twig.extension' }
|
- { name: 'twig.extension' }
|
||||||
|
|
||||||
|
flasher.session_listener:
|
||||||
|
class: Flasher\Symfony\EventListener\SessionListener
|
||||||
|
arguments:
|
||||||
|
- '@flasher.config'
|
||||||
|
- '@flasher'
|
||||||
|
- '@flasher.presenter.html'
|
||||||
|
tags:
|
||||||
|
- { name: 'kernel.event_subscriber' }
|
||||||
|
|
||||||
Flasher\Prime\Config\Config: '@flasher.config'
|
Flasher\Prime\Config\Config: '@flasher.config'
|
||||||
Flasher\Prime\Flasher: '@flasher'
|
Flasher\Prime\Flasher: '@flasher'
|
||||||
Flasher\Prime\Presenter\PresenterManager: '@flasher.presenter_manager'
|
Flasher\Prime\Presenter\PresenterManager: '@flasher.presenter_manager'
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ return array(
|
|||||||
'onHidden' => null,
|
'onHidden' => null,
|
||||||
'onShown' => null,
|
'onShown' => null,
|
||||||
'positionClass' => 'toast-top-right',
|
'positionClass' => 'toast-top-right',
|
||||||
'preventDuplicates' => true,
|
'preventDuplicates' => false,
|
||||||
'progressBar' => true,
|
'progressBar' => true,
|
||||||
'progressClass' => 'toast-progress',
|
'progressClass' => 'toast-progress',
|
||||||
'rtl' => false,
|
'rtl' => false,
|
||||||
|
|||||||
Reference in New Issue
Block a user