Skip to content

Commit

Permalink
Merge pull request #71 from Troopers/bugfix/circular-reference
Browse files Browse the repository at this point in the history
make the handler agnostic of twig to avoid circular reference
  • Loading branch information
paulandrieux authored Jun 23, 2017
2 parents 0384203 + e103470 commit 087fb02
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
8 changes: 5 additions & 3 deletions EventListener/AlertifyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ class AlertifyListener implements EventSubscriberInterface
* @var Session
*/
private $session;
private $twig;

/**
* AlertifyListener constructor.
*
* @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)
Expand All @@ -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);
}
Expand Down
7 changes: 3 additions & 4 deletions Handler/AlertifySessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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();

Expand 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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<argument type="service" id="session" />
</service>
<service id="troopers_alertifybundle.session_handler" class="%alertify.handler.session.class%">
<argument type="service" id="twig" />
<argument>%troopers_alertify%</argument>
</service>
<service id="troopers_alertifybundle.event_listener" class="%alertify.event_listener%">
<tag name="kernel.event_subscriber" />
<argument type="service" id="twig" />
<argument type="service" id="session" />
<argument type="service" id="troopers_alertifybundle.session_handler" />
</service>
Expand Down
7 changes: 4 additions & 3 deletions Tests/Handler/AlertifySessionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
}

/**
Expand All @@ -52,6 +52,7 @@ protected function mockSession()
protected function getTwigEnvironmentMock()
{
$twigEnvironment = $this->getMockBuilder('Twig_Environment')
->disableOriginalConstructor()
->setMethods(['render'])
->getMock();

Expand Down
2 changes: 1 addition & 1 deletion Twig/Extension/AlertifyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public function getFilters()
*/
public function alertifyFilter($environment, Session $session)
{
return $this->alertifySessionHandler->handle($session);
return $this->alertifySessionHandler->handle($session, $environment);
}
}

0 comments on commit 087fb02

Please sign in to comment.