Skip to content

Commit

Permalink
Allowing config to just be a template dir... and with trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Sep 23, 2023
1 parent 173e53f commit 85e3bef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ private function calculateTemplate(string $componentName, ?array $defaults): str
$componentName = substr($componentName, \strlen($defaults['name_prefix']) + 1);
}

return sprintf('%s/%s.html.twig', $directory, str_replace(':', '/', $componentName));
return sprintf('%s/%s.html.twig', rtrim($directory, '/'), str_replace(':', '/', $componentName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*/
final class TwigComponentExtension extends Extension implements ConfigurationInterface
{
const DEPRECATED_DEFAULT_KEY = '__deprecated__use_old_naming_behavior';
private const DEPRECATED_DEFAULT_KEY = '__deprecated__use_old_naming_behavior';

public function load(array $configs, ContainerBuilder $container): void
{
Expand Down Expand Up @@ -135,7 +135,7 @@ public function getConfigTreeBuilder()
->defaultValue([self::DEPRECATED_DEFAULT_KEY])
->useAttributeAsKey('namespace')
->validate()
->always(function($v) {
->always(function ($v) {
foreach ($v as $namespace => $defaults) {
if (!str_ends_with($namespace, '\\')) {
throw new InvalidConfigurationException(sprintf('The twig_component.defaults namespace "%s" is invalid: it must end in a "\"', $namespace));
Expand All @@ -147,6 +147,12 @@ public function getConfigTreeBuilder()
->end()
// validate that namespace ends in \
->arrayPrototype()
->beforeNormalization()
->ifString()
->then(function (string $v) {
return ['template_directory' => $v];
})
->end()
->children()
->scalarNode('template_directory')
->defaultValue('components')
Expand Down
2 changes: 1 addition & 1 deletion src/TwigComponent/tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function configureContainer(ContainerConfigurator $c): void
}
$c->extension('twig_component', [
'defaults' => [
'Symfony\UX\TwigComponent\Tests\Fixtures\Component\\' => [],
'Symfony\UX\TwigComponent\Tests\Fixtures\Component\\' => 'components/',
'Symfony\UX\TwigComponent\Tests\Fixtures\AcmeComponent\\' => $acmeDefaults,
],
]);
Expand Down

0 comments on commit 85e3bef

Please sign in to comment.