Skip to content

Commit

Permalink
Fix injection for stack and context
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jan 22, 2023
1 parent 74d872e commit b0aefcd
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 92 deletions.
7 changes: 5 additions & 2 deletions src/Pug/PugSymfonyEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
use Pug\Symfony\Traits\Options;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;
use Twig\Environment as TwigEnvironment;
Expand Down Expand Up @@ -50,11 +52,12 @@ class PugSymfonyEngine implements EngineInterface, InstallerInterface
protected $defaultTemplateDirectory;

public function __construct(
KernelInterface $kernel,
protected readonly KernelInterface $kernel,
TwigEnvironment $twig,
private readonly ?RequestStack $stack = null,
private readonly ?RequestContext $context = null,
) {
$container = $kernel->getContainer();
$this->kernel = $kernel;
$this->container = $container;
$this->userOptions = ($this->container->hasParameter('pug') ? $this->container->getParameter('pug') : null) ?: [];
$this->enhanceTwig($twig);
Expand Down
11 changes: 4 additions & 7 deletions src/Pug/Symfony/Traits/HelpersHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\UrlHelper;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RequestContext;
use Twig\Environment as TwigEnvironment;
use Twig\Extension\ExtensionInterface;
Expand All @@ -40,8 +38,6 @@ trait HelpersHandler

protected Environment $twig;

protected Kernel|KernelInterface $kernel;

protected ?Pug $pug = null;

protected array $userOptions = [];
Expand Down Expand Up @@ -353,12 +349,13 @@ protected function copyTwigFunctions(): void
protected function getHttpFoundationExtension(): HttpFoundationExtension
{
/* @var RequestStack $stack */
$stack = $this->container->get('request_stack');
$stack = $this->stack ?? $this->container->get('request_stack');

/* @var RequestContext $context */
$context = $this->container->has('router.request_context')
$context = $this->context ?? ($this->container->has('router.request_context')
? $this->container->get('router.request_context')
: $this->container->get('router')->getContext();
: $this->container->get('router')->getContext()
);

return new HttpFoundationExtension(new UrlHelper($stack, $context));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pug/Twig/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getRuntime(string $class)
try {
return parent::getRuntime($class);
} catch (RuntimeError $error) {
if (!$this->rootEnv) {
if (!($this->rootEnv ?? null)) {
throw $error;
}

Expand Down
7 changes: 0 additions & 7 deletions tests/Pug/AbstractController.php

This file was deleted.

39 changes: 27 additions & 12 deletions tests/Pug/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,53 @@
use Psr\Container\ContainerInterface;
use Pug\PugSymfonyEngine;
use Pug\Twig\Environment;
use Symfony\Bridge\Twig\Extension\CsrfExtension;
use Symfony\Bridge\Twig\Extension\CsrfRuntime;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RequestContext;
use Twig\Loader\FilesystemLoader;
use Twig\RuntimeLoader\ContainerRuntimeLoader;
use Twig\RuntimeLoader\FactoryRuntimeLoader;

abstract class AbstractTestCase extends KernelTestCase
{
protected static $originalFiles = [];
protected static array $originalFiles = [];

protected static $cachePath = __DIR__.'/../project-s5/var/cache/test';
protected static string $cachePath = __DIR__.'/../project-s5/var/cache/test';

protected ?Environment $twig = null;

protected ?PugSymfonyEngine $pugSymfony = null;

protected function getTwigEnvironment(): Environment
{
return $this->twig ??= new Environment(new FilesystemLoader(
__DIR__.'/../project-s5/templates/',
));
if (!isset($this->twig)) {
$this->twig = new Environment(new FilesystemLoader(
__DIR__.'/../project-s5/templates/',
));
$this->twig->addExtension(new CsrfExtension());
$this->twig->addRuntimeLoader(new FactoryRuntimeLoader([
CsrfRuntime::class => static fn () => new CsrfRuntime(new TestCsrfTokenManager()),
]));
}

return $this->twig;
}

protected function getPugSymfonyEngine(?KernelInterface $kernel = null): PugSymfonyEngine
{
$twig = $this->getTwigEnvironment();
$this->pugSymfony ??= new PugSymfonyEngine($kernel ?? self::$kernel, $twig);
$this->pugSymfony ??= new PugSymfonyEngine(
$kernel ?? self::$kernel,
$twig,
new RequestStack(),
new RequestContext(),
);
$twig->setPugSymfonyEngine($this->pugSymfony);

return $this->pugSymfony;
Expand Down Expand Up @@ -100,19 +118,16 @@ public function setUp(): void
$this->addFormRenderer();
}

protected function addFormRenderer(?ContainerInterface $container = null)
protected function addFormRenderer()
{
require_once __DIR__.'/TestCsrfTokenManager.php';

/** @var Environment $twig */
$twig = ($container ?? self::getContainer())->get('twig');
$twig = $this->getTwigEnvironment();
$csrfManager = new TestCsrfTokenManager();
$formEngine = new TwigRendererEngine(['form_div_layout.html.twig'], $twig);

$twig->addRuntimeLoader(new FactoryRuntimeLoader([
FormRenderer::class => static function () use ($formEngine, $csrfManager) {
return new FormRenderer($formEngine, $csrfManager);
},
FormRenderer::class => static fn () => new FormRenderer($formEngine, $csrfManager),
]));
}
}
Loading

0 comments on commit b0aefcd

Please sign in to comment.