Skip to content

Commit

Permalink
👻 make the handler agnostic of twig to avoid circular reference
Browse files Browse the repository at this point in the history
  • Loading branch information
paulandrieux committed Jun 23, 2017
1 parent 0384203 commit 2cd691a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 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
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 2cd691a

Please sign in to comment.