Skip to content

Commit

Permalink
Add Bootstrap 5 support to DI extension
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Dec 28, 2023
1 parent 4be434b commit e20195c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Bridges/FormRendererDI/FormRendererExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Nepada\FormRenderer\Bootstrap3RendererFactory;
use Nepada\FormRenderer\Bootstrap4Renderer;
use Nepada\FormRenderer\Bootstrap4RendererFactory;
use Nepada\FormRenderer\Bootstrap5Renderer;
use Nepada\FormRenderer\Bootstrap5RendererFactory;
use Nepada\FormRenderer\Filters\SafeTranslateFilterFactory;
use Nepada\FormRenderer\TemplateRenderer;
use Nepada\FormRenderer\TemplateRendererFactory;
Expand Down Expand Up @@ -48,10 +50,22 @@ public function getConfigSchema(): Nette\Schema\Schema
'useErrorTooltips' => Nette\Schema\Expect::bool(false),
]);

$bootstrap5 = Nette\Schema\Expect::structure([
'imports' => clone $imports,
'mode' => Nette\Schema\Expect::anyOf(
Bootstrap5Renderer::MODE_BASIC,
Bootstrap5Renderer::MODE_INLINE,
Bootstrap5Renderer::MODE_HORIZONTAL,
)->default(Bootstrap5Renderer::MODE_BASIC),
'renderValidState' => Nette\Schema\Expect::bool(false),
'useErrorTooltips' => Nette\Schema\Expect::bool(false),
]);

return Nette\Schema\Expect::structure([
'default' => $default,
'bootstrap3' => $bootstrap3,
'bootstrap4' => $bootstrap4,
'bootstrap5' => $bootstrap5,
]);
}

Expand All @@ -71,6 +85,7 @@ public function loadConfiguration(): void
$this->setupDefaultRenderer($config->default);
$this->setupBootstrap3Renderer($config->bootstrap3);
$this->setupBootstrap4Renderer($config->bootstrap4);
$this->setupBootstrap5Renderer($config->bootstrap5);
}

private function setupDefaultRenderer(\stdClass $config): void
Expand Down Expand Up @@ -136,4 +151,29 @@ private function setupBootstrap4Renderer(\stdClass $config): void
}
}

private function setupBootstrap5Renderer(\stdClass $config): void
{
$container = $this->getContainerBuilder();

$factory = $container->addFactoryDefinition($this->prefix('bootstrap5RendererFactory'))
->setImplement(Bootstrap5RendererFactory::class);

$resultDefinition = $factory->getResultDefinition();
$resultDefinition->setArguments(['templateRendererFactory' => $this->prefix('@templateRendererFactory')]);
$resultDefinition->addSetup('setRenderValidState', [$config->renderValidState]);
$resultDefinition->addSetup('setUseErrorTooltips', [$config->useErrorTooltips]);
foreach ($config->imports as $templateFile) {
$resultDefinition->addSetup('importTemplate', [$templateFile]);
}
if ($config->mode === Bootstrap5Renderer::MODE_HORIZONTAL) {
$resultDefinition->addSetup('setHorizontalMode');
} elseif ($config->mode === Bootstrap5Renderer::MODE_INLINE) {
$resultDefinition->addSetup('setInlineMode');
} elseif ($config->mode === Bootstrap5Renderer::MODE_BASIC) {
$resultDefinition->addSetup('setBasicMode');
} else {
throw new \InvalidArgumentException("Unsupported bootstrap 5 renderer mode '{$config->mode}'.");
}
}

}
4 changes: 4 additions & 0 deletions tests/Bridges/FormRendererDI/FormRendererExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class FormRendererExtensionTest extends TestCase
Assert::type(FormRenderer\TemplateRendererFactory::class, $this->container->getService('formRenderer.defaultRendererFactory'));
Assert::type(FormRenderer\Bootstrap3RendererFactory::class, $this->container->getService('formRenderer.bootstrap3RendererFactory'));
Assert::type(FormRenderer\Bootstrap4RendererFactory::class, $this->container->getService('formRenderer.bootstrap4RendererFactory'));
Assert::type(FormRenderer\Bootstrap5RendererFactory::class, $this->container->getService('formRenderer.bootstrap5RendererFactory'));
}

public function testRendering(): void
Expand All @@ -41,6 +42,9 @@ class FormRendererExtensionTest extends TestCase

$bootstrap4Renderer = $this->container->getByType(FormRenderer\Bootstrap4RendererFactory::class)->create();
Assert::same("FORM\nhorizontal\nform.latte,bootstrap4.latte\n", $bootstrap4Renderer->render($form));

$bootstrap5Renderer = $this->container->getByType(FormRenderer\Bootstrap5RendererFactory::class)->create();
Assert::same("FORM\nhorizontal\nform.latte,bootstrap5.latte\n", $bootstrap5Renderer->render($form));
}

protected function setUp(): void
Expand Down
7 changes: 7 additions & 0 deletions tests/Bridges/FormRendererDI/fixtures/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ formRenderer:
imports:
- %fixturesDir%/form.latte

bootstrap5:
mode: horizontal
useErrorTooltips: false
renderValidState: false
imports:
- %fixturesDir%/form.latte

application:
scanDirs: false

Expand Down

0 comments on commit e20195c

Please sign in to comment.