From 2cd691a8d9be23cd836b34a30865ae38e6fd14d7 Mon Sep 17 00:00:00 2001 From: Paul Andrieux Date: Fri, 23 Jun 2017 10:33:10 +0200 Subject: [PATCH 1/2] :ghost: make the handler agnostic of twig to avoid circular reference --- EventListener/AlertifyListener.php | 8 +++++--- Handler/AlertifySessionHandler.php | 7 +++---- Resources/config/services.xml | 2 +- Twig/Extension/AlertifyExtension.php | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) 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/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); } } From e103470a0f707bdda88d398c23e93e07fa5e6a1d Mon Sep 17 00:00:00 2001 From: Paul Andrieux Date: Fri, 23 Jun 2017 10:53:56 +0200 Subject: [PATCH 2/2] fix unit tests --- Tests/Handler/AlertifySessionHandlerTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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();