Skip to content

Commit

Permalink
fix: load twig extension dependencies dinamically
Browse files Browse the repository at this point in the history
  • Loading branch information
kpicaza committed Oct 25, 2022
1 parent f09fdc2 commit 2d21600
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/DependencyInjection/TwigExtensionPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Pheature\Community\Symfony\DependencyInjection;

use Pheature\Community\Symfony\Twig\PheatureFlagsExtension;
use Pheature\Community\Symfony\Twig\PheatureFlagsRuntime;
use Pheature\Core\Toggle\Read\Toggle;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -18,10 +20,12 @@ public function process(ContainerBuilder $container): void
return;
}

$container->register(PheatureFlagsExtension::class, PheatureFlagsExtension::class);
$container->register(PheatureFlagsRuntime::class, PheatureFlagsRuntime::class)
->addArgument(new Reference(Toggle::class));
$container->register(PheatureFlagsExtension::class, PheatureFlagsExtension::class)
->addArgument(new Reference(PheatureFlagsRuntime::class));

$container->findDefinition('twig')
->addMethodCall('addExtension', [new Reference(PheatureFlagsExtension::class)])
;
->addMethodCall('addExtension', [new Reference(PheatureFlagsExtension::class)]);
}
}
13 changes: 10 additions & 3 deletions src/Twig/PheatureFlagsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@

final class PheatureFlagsExtension extends AbstractExtension
{
private PheatureFlagsRuntime $runtime;

public function __construct(PheatureFlagsRuntime $runtime)
{
$this->runtime = $runtime;
}

/**
* @return TwigTest[]
*/
public function getTests(): array
{
return [
new TwigTest('enabled', [PheatureFlagsRuntime::class, 'isEnabled']),
new TwigTest('enabled_for', [PheatureFlagsRuntime::class, 'isEnabled'], ['one_mandatory_argument' => true]),
new TwigTest('enabled', [$this->runtime, 'isEnabled']),
new TwigTest('enabled_for', [$this->runtime, 'isEnabled'], ['one_mandatory_argument' => true]),
];
}

Expand All @@ -27,7 +34,7 @@ public function getTests(): array
public function getFunctions(): array
{
return [
new TwigFunction('is_feature_enabled', [PheatureFlagsRuntime::class, 'isFeatureEnabled']),
new TwigFunction('is_feature_enabled', [$this->runtime, 'isFeatureEnabled']),
];
}
}
Empty file removed test.sqlite
Empty file.
19 changes: 15 additions & 4 deletions test/Twig/PheatureFlagsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ final class PheatureFlagsExtensionTest extends TestCase
{
public function testItShouldExposeTwoTests(): void
{
$extension = new PheatureFlagsExtension(new Toggle($this->createStub(FeatureFinder::class)));
$extension = new PheatureFlagsExtension(
new PheatureFlagsRuntime(
new Toggle($this->createStub(FeatureFinder::class))
)
);
$this->assertCount(2, $extension->getTests());
}

public function testItShouldExposeOneFunction(): void
{
$extension = new PheatureFlagsExtension(new Toggle($this->createStub(FeatureFinder::class)));
$extension = new PheatureFlagsExtension(
new PheatureFlagsRuntime(
new Toggle($this->createStub(FeatureFinder::class))
)
);
$this->assertCount(1, $extension->getFunctions());
}

Expand All @@ -38,9 +46,12 @@ public function testRendering($template, $variables)
{
$loader = new ArrayLoader(['index' => $template]);
$twig = new Environment($loader, ['debug' => true, 'cache' => false]);
$twig->addExtension(new PheatureFlagsExtension());
$toggle = $this->createAllFeaturesEnabledToggle();
$twig->addRuntimeLoader(new FactoryRuntimeLoader([PheatureFlagsRuntime::class => fn() => new PheatureFlagsRuntime($toggle)]));
$twig->addExtension(new PheatureFlagsExtension(
new PheatureFlagsRuntime(
$toggle
)
));

$this->assertSame('yes', $twig->load('index')->render($variables));
}
Expand Down

0 comments on commit 2d21600

Please sign in to comment.