Skip to content

Commit 2cd691a

Browse files
committed
👻 make the handler agnostic of twig to avoid circular reference
1 parent 0384203 commit 2cd691a

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

EventListener/AlertifyListener.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ class AlertifyListener implements EventSubscriberInterface
3232
* @var Session
3333
*/
3434
private $session;
35+
private $twig;
3536

3637
/**
3738
* AlertifyListener constructor.
3839
*
3940
* @param Session $session
4041
* @param AlertifySessionHandler $alertifySessionHandler
4142
*/
42-
public function __construct(Session $session, AlertifySessionHandler $alertifySessionHandler)
43+
public function __construct(\Twig_Environment $twig, Session $session, AlertifySessionHandler $alertifySessionHandler)
4344
{
44-
$this->alertifySessionHandler = $alertifySessionHandler;
45+
$this->twig = $twig;
4546
$this->session = $session;
47+
$this->alertifySessionHandler = $alertifySessionHandler;
4648
}
4749

4850
public function onKernelResponse(FilterResponseEvent $event)
@@ -69,7 +71,7 @@ protected function injectAlertify(Response $response, Request $request)
6971
$isRedirectResponse = $response instanceof RedirectResponse;
7072

7173
if ($hasBody && !$hasMetaRefresh && !$isRedirectResponse) {
72-
$alertify = $this->alertifySessionHandler->handle($this->session);
74+
$alertify = $this->alertifySessionHandler->handle($this->session, $this->twig);
7375
$content = substr($content, 0, $endBodyPos).$alertify.substr($content, $endBodyPos);
7476
$response->setContent($content);
7577
}

Handler/AlertifySessionHandler.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ class AlertifySessionHandler
2525
* @param \Twig_Environment $twig
2626
* @param array $defaultParameters
2727
*/
28-
public function __construct(\Twig_Environment $twig, array $defaultParameters)
28+
public function __construct(array $defaultParameters)
2929
{
30-
$this->twig = $twig;
3130
$this->defaultParameters = $defaultParameters;
3231
}
3332

@@ -39,7 +38,7 @@ public function __construct(\Twig_Environment $twig, array $defaultParameters)
3938
*
4039
* @return string
4140
*/
42-
public function handle($session)
41+
public function handle($session, \Twig_Environment $twig)
4342
{
4443
$flashes = $session->getFlashBag()->all();
4544

@@ -56,7 +55,7 @@ public function handle($session)
5655
}
5756

5857
$parameters['type'] = $type;
59-
$renders[$type.$key] = $this->twig->render('TroopersAlertifyBundle::'.$parameters['engine'].'.html.twig', $parameters);
58+
$renders[$type.$key] = $twig->render('TroopersAlertifyBundle::'.$parameters['engine'].'.html.twig', $parameters);
6059
}
6160
}
6261

Resources/config/services.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<argument type="service" id="session" />
1616
</service>
1717
<service id="troopers_alertifybundle.session_handler" class="%alertify.handler.session.class%">
18-
<argument type="service" id="twig" />
1918
<argument>%troopers_alertify%</argument>
2019
</service>
2120
<service id="troopers_alertifybundle.event_listener" class="%alertify.event_listener%">
2221
<tag name="kernel.event_subscriber" />
22+
<argument type="service" id="twig" />
2323
<argument type="service" id="session" />
2424
<argument type="service" id="troopers_alertifybundle.session_handler" />
2525
</service>

Twig/Extension/AlertifyExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public function getFilters()
6161
*/
6262
public function alertifyFilter($environment, Session $session)
6363
{
64-
return $this->alertifySessionHandler->handle($session);
64+
return $this->alertifySessionHandler->handle($session, $environment);
6565
}
6666
}

0 commit comments

Comments
 (0)