From 5a8bc860932494e1cf4dd398ff873d3607a0a205 Mon Sep 17 00:00:00 2001 From: Leny BERNARD Date: Thu, 20 Oct 2016 00:26:30 +0200 Subject: [PATCH] :art: Listen kernel.response event to inject alertify into the response --- EventListener/AlertifyListener.php | 78 +++++++++++++++++++ .../AlertifySessionHandler.php | 55 +++++-------- 2 files changed, 98 insertions(+), 35 deletions(-) create mode 100644 EventListener/AlertifyListener.php rename Twig/Extension/AlertifyExtension.php => Handler/AlertifySessionHandler.php (63%) diff --git a/EventListener/AlertifyListener.php b/EventListener/AlertifyListener.php new file mode 100644 index 0000000..1952931 --- /dev/null +++ b/EventListener/AlertifyListener.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Troopers\AlertifyBundle\EventListener; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpKernel\Event\FilterResponseEvent; +use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Troopers\AlertifyBundle\Handler\AlertifySessionHandler; + +/** + * AlertifyListener append the alertify views to the response. + * + * @author Leny Bernard + */ +class AlertifyListener implements EventSubscriberInterface +{ + protected $alertifySessionHandler; + /** + * @var Session + */ + private $session; + + /** + * AlertifyListener constructor. + * + * @param Session $session + * @param AlertifySessionHandler $alertifySessionHandler + */ + public function __construct(Session $session, AlertifySessionHandler $alertifySessionHandler) + { + $this->alertifySessionHandler = $alertifySessionHandler; + $this->session = $session; + } + + public function onKernelResponse(FilterResponseEvent $event) + { + $response = $event->getResponse(); + $request = $event->getRequest(); + + $this->injectAlertify($response, $request); + } + + /** + * Injects the alertify view into the given Response just before tag. + */ + protected function injectAlertify(Response $response, Request $request) + { + $content = $response->getContent(); + $pos = strripos($content, ''); + + if (false !== $pos) { + $alertify = $this->alertifySessionHandler->handle($this->session); + $content = substr($content, 0, $pos).$alertify.substr($content, $pos); + $response->setContent($content); + } + } + + public static function getSubscribedEvents() + { + return [ + KernelEvents::RESPONSE => array('onKernelResponse'), + ]; + } +} diff --git a/Twig/Extension/AlertifyExtension.php b/Handler/AlertifySessionHandler.php similarity index 63% rename from Twig/Extension/AlertifyExtension.php rename to Handler/AlertifySessionHandler.php index 43c491b..5cd914f 100644 --- a/Twig/Extension/AlertifyExtension.php +++ b/Handler/AlertifySessionHandler.php @@ -1,60 +1,45 @@ defaultParameters = $defaultParameters; - } + protected $twig; /** - * {@inheritdoc} + * @var array */ - public function initRuntime(\Twig_Environment $environment) - { - $this->environment = $environment; - } + private $defaultParameters; /** - * {@inheritdoc} - */ - public function getName() - { - return 'alertify'; - } - - /** - * {@inheritdoc} + * AlertifySessionHandler constructor. + * + * @param \Twig_Environment $twig + * @param array $defaultParameters */ - public function getFilters() + public function __construct(\Twig_Environment $twig, array $defaultParameters) { - return [ - new \Twig_SimpleFilter('alertify', [$this, 'alertifyFilter'], ['needs_environment' => true, 'is_safe' => ['html']]), - ]; + $this->twig = $twig; + $this->defaultParameters = $defaultParameters; } /** - * Alertify filter. + * Alertify . * - * @param \Twig_Environment $environment + * @param \Twig_Environment $this-twig * @param Session $session * * @return string */ - public function alertifyFilter($environment, Session $session) + public function handle($session) { $flashes = $session->getFlashBag()->all(); @@ -62,9 +47,9 @@ public function alertifyFilter($environment, Session $session) foreach ($flashes as $type => $flash) { if ($type == 'callback') { foreach ($flash as $key => $currentFlash) { - $currentFlash['body'] .= $environment->render('TroopersAlertifyBundle::callback.html.twig', $currentFlash); + $currentFlash['body'] .= $this->twig->render('TroopersAlertifyBundle::callback.html.twig', $currentFlash); $session->getFlashBag()->add($currentFlash['engine'], $currentFlash); - $renders[$type.$key] = $this->alertifyFilter($environment, $session); + $renders[$type.$key] = $this->handle($session); } } else { foreach ($flash as $key => $content) { @@ -78,7 +63,7 @@ public function alertifyFilter($environment, Session $session) } $parameters['type'] = $type; - $renders[$type.$key] = $environment->render('TroopersAlertifyBundle::'.$parameters['engine'].'.html.twig', $parameters); + $renders[$type.$key] = $this->twig->render('TroopersAlertifyBundle::'.$parameters['engine'].'.html.twig', $parameters); } } }