This repository has been archived by the owner on Feb 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #681 Fetch Twig lazily in TemplateListener (nicolas-grekas)
This PR was merged into the 5.5.x-dev branch. Discussion ---------- Fetch Twig lazily in TemplateListener I spotted this when playing with a controller that doesn't use Twig: `TemplateListener` always instantiate twig. But twig is kinda heavy to create. This makes its instantiation lazy. Commits ------- d10fce5 Fetch Twig lazily in TemplateListener
- Loading branch information
Showing
3 changed files
with
25 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
namespace Sensio\Bundle\FrameworkExtraBundle\EventListener; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | ||
use Sensio\Bundle\FrameworkExtraBundle\Templating\TemplateGuesser; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
@@ -19,6 +20,7 @@ | |
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
use Symfony\Component\HttpKernel\Event\KernelEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Contracts\Service\ServiceSubscriberInterface; | ||
use Twig\Environment; | ||
|
||
/** | ||
|
@@ -28,17 +30,23 @@ | |
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class TemplateListener implements EventSubscriberInterface | ||
class TemplateListener implements EventSubscriberInterface, ServiceSubscriberInterface | ||
{ | ||
private $templateGuesser; | ||
private $twig; | ||
private $container; | ||
|
||
public function __construct(TemplateGuesser $templateGuesser, Environment $twig = null) | ||
{ | ||
$this->templateGuesser = $templateGuesser; | ||
$this->twig = $twig; | ||
} | ||
|
||
public function setContainer(ContainerInterface $container): void | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* Guesses the template name to render and its variables and adds them to | ||
* the request object. | ||
|
@@ -79,7 +87,11 @@ public function onKernelView(KernelEvent $event) | |
} | ||
|
||
if (null === $this->twig) { | ||
throw new \LogicException('You can not use the "@Template" annotation if the Twig Bundle is not available.'); | ||
if (!$this->container || !$this->container->has('twig')) { | ||
throw new \LogicException('You can not use the "@Template" annotation if the Twig Bundle is not available.'); | ||
} | ||
|
||
$this->twig = $this->container->get('twig'); | ||
} | ||
|
||
$parameters = $event->getControllerResult(); | ||
|
@@ -118,6 +130,11 @@ public static function getSubscribedEvents() | |
]; | ||
} | ||
|
||
public static function getSubscribedServices(): array | ||
{ | ||
return ['twig' => '?'.Environment::class]; | ||
} | ||
|
||
private function resolveDefaultParameters(Request $request, Template $template, $controller, $action) | ||
{ | ||
$parameters = []; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters