Skip to content

Commit

Permalink
🎨 Listen kernel.response event to inject alertify into the response
Browse files Browse the repository at this point in the history
  • Loading branch information
Leny BERNARD authored and lenybernard committed Oct 19, 2016
1 parent c8a1586 commit 5a8bc86
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 35 deletions.
78 changes: 78 additions & 0 deletions EventListener/AlertifyListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
*/
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 </body> tag.
*/
protected function injectAlertify(Response $response, Request $request)
{
$content = $response->getContent();
$pos = strripos($content, '</body>');

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'),
];
}
}
Original file line number Diff line number Diff line change
@@ -1,70 +1,55 @@
<?php

namespace Troopers\AlertifyBundle\Twig\Extension;
namespace Troopers\AlertifyBundle\Handler;

use Symfony\Component\HttpFoundation\Session\Session;

/**
* AlertifyExtension.
* AlertifySessionHandler.
*/
class AlertifyExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
class AlertifySessionHandler
{
protected $environment;
protected $defaultParameters;

/**
* {@inheritdoc}
* @var \Twig_Environment
*/
public function __construct($defaultParameters)
{
$this->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();

$renders = [];
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) {
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 5a8bc86

Please sign in to comment.