diff --git a/EventListener/AlertifyListener.php b/EventListener/AlertifyListener.php
index 51008ba..4133444 100644
--- a/EventListener/AlertifyListener.php
+++ b/EventListener/AlertifyListener.php
@@ -32,6 +32,7 @@ class AlertifyListener implements EventSubscriberInterface
* @var Session
*/
private $session;
+ private $twig;
/**
* AlertifyListener constructor.
@@ -39,10 +40,11 @@ class AlertifyListener implements EventSubscriberInterface
* @param Session $session
* @param AlertifySessionHandler $alertifySessionHandler
*/
- public function __construct(Session $session, AlertifySessionHandler $alertifySessionHandler)
+ public function __construct(\Twig_Environment $twig, Session $session, AlertifySessionHandler $alertifySessionHandler)
{
- $this->alertifySessionHandler = $alertifySessionHandler;
+ $this->twig = $twig;
$this->session = $session;
+ $this->alertifySessionHandler = $alertifySessionHandler;
}
public function onKernelResponse(FilterResponseEvent $event)
@@ -69,7 +71,7 @@ protected function injectAlertify(Response $response, Request $request)
$isRedirectResponse = $response instanceof RedirectResponse;
if ($hasBody && !$hasMetaRefresh && !$isRedirectResponse) {
- $alertify = $this->alertifySessionHandler->handle($this->session);
+ $alertify = $this->alertifySessionHandler->handle($this->session, $this->twig);
$content = substr($content, 0, $endBodyPos).$alertify.substr($content, $endBodyPos);
$response->setContent($content);
}
diff --git a/Handler/AlertifySessionHandler.php b/Handler/AlertifySessionHandler.php
index 8e77568..1a87e4d 100644
--- a/Handler/AlertifySessionHandler.php
+++ b/Handler/AlertifySessionHandler.php
@@ -25,9 +25,8 @@ class AlertifySessionHandler
* @param \Twig_Environment $twig
* @param array $defaultParameters
*/
- public function __construct(\Twig_Environment $twig, array $defaultParameters)
+ public function __construct(array $defaultParameters)
{
- $this->twig = $twig;
$this->defaultParameters = $defaultParameters;
}
@@ -39,7 +38,7 @@ public function __construct(\Twig_Environment $twig, array $defaultParameters)
*
* @return string
*/
- public function handle($session)
+ public function handle($session, \Twig_Environment $twig)
{
$flashes = $session->getFlashBag()->all();
@@ -56,7 +55,7 @@ public function handle($session)
}
$parameters['type'] = $type;
- $renders[$type.$key] = $this->twig->render('TroopersAlertifyBundle::'.$parameters['engine'].'.html.twig', $parameters);
+ $renders[$type.$key] = $twig->render('TroopersAlertifyBundle::'.$parameters['engine'].'.html.twig', $parameters);
}
}
diff --git a/Resources/config/services.xml b/Resources/config/services.xml
index 2d6f339..cd61a17 100644
--- a/Resources/config/services.xml
+++ b/Resources/config/services.xml
@@ -15,11 +15,11 @@
-
%troopers_alertify%
+
diff --git a/Tests/Handler/AlertifySessionHandlerTest.php b/Tests/Handler/AlertifySessionHandlerTest.php
index 96377a3..5938d16 100644
--- a/Tests/Handler/AlertifySessionHandlerTest.php
+++ b/Tests/Handler/AlertifySessionHandlerTest.php
@@ -29,16 +29,16 @@ public function testDefault()
/** @var AlertifyHelper $helper */
$helper = new AlertifyHelper($this->session);
$handler = new AlertifySessionHandler(
- $this->getTwigEnvironmentMock(),
$container->getParameter('troopers_alertify')
);
+ $twig = $this->getTwigEnvironmentMock();
$helper->congrat('Alert1');
- $this->assertEquals(1, count(explode(' ', $handler->handle($this->session))));
+ $this->assertEquals(1, count(explode(' ', $handler->handle($this->session, $twig))));
$helper->congrat('Alert2');
$helper->congrat('Alert3', ['opt1' => 42]);
$helper->congrat('Alert4');
- $this->assertEquals(3, count(explode(' ', $handler->handle($this->session))));
+ $this->assertEquals(3, count(explode(' ', $handler->handle($this->session, $twig))));
}
/**
@@ -52,6 +52,7 @@ protected function mockSession()
protected function getTwigEnvironmentMock()
{
$twigEnvironment = $this->getMockBuilder('Twig_Environment')
+ ->disableOriginalConstructor()
->setMethods(['render'])
->getMock();
diff --git a/Twig/Extension/AlertifyExtension.php b/Twig/Extension/AlertifyExtension.php
index 089d6c2..df20af7 100644
--- a/Twig/Extension/AlertifyExtension.php
+++ b/Twig/Extension/AlertifyExtension.php
@@ -61,6 +61,6 @@ public function getFilters()
*/
public function alertifyFilter($environment, Session $session)
{
- return $this->alertifySessionHandler->handle($session);
+ return $this->alertifySessionHandler->handle($session, $environment);
}
}