Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Normalize template name using regex to allow capitalized prefixes #26

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<!-- There should always be "Unreleased" section at the beginning. -->
## Unreleased
- Feat: Normalize template name using regex to allow capitalized prefixes

## 3.2.1 - 2023-03-30
- Fix: Negative lookahead breaks parsing of element closing bracket
Expand Down
15 changes: 14 additions & 1 deletion src/Compiler/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ function (array $matches) {

protected function componentStartString(string $component, string $attributes): string
{
return "{% embed \"@" . $this->twigPathAlias . "/" . lcfirst($component) . ".twig\" with { props: $attributes } %}";
return sprintf(
// another `%` is used for escaping the `%`, e. g. `%%` -> `%`
"{%% embed \"@%s/%s.twig\" with { props: %s } %%}",
$this->twigPathAlias,
$this->normalizeComponentPathName($component),
$attributes
);
}

/**
Expand Down Expand Up @@ -146,6 +152,13 @@ function (array $matches) {
);
}

private function normalizeComponentPathName(string $name): string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we test this too? At least it would help me understand it what it does :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test was modified to cover this change. However, the private methods cannot be tested directly. 🤷

{
return preg_replace_callback('/^[A-Z]+_?/', function ($matches) {
return strtolower($matches[0]);
}, $name) ?: $name;
}

private function valueParser(?string $value, string $attribute): string
{
if ($value === null) {
Expand Down
7 changes: 7 additions & 0 deletions tests/Compiler/ComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public function testShouldCompile(): void
$this->assertSame('{% embed "@alias/alert.twig" with { props: {\'color\': "primary"} } %}{% endembed %}', $compiler->compile());
}

public function testShouldCompileUnstable(): void
{
$compiler = new ComponentTagCompiler('<UNSTABLE_Alert color="primary" />', 'alias');

$this->assertSame('{% embed "@alias/unstable_Alert.twig" with { props: {\'color\': "primary"} } %}{% endembed %}', $compiler->compile());
}

public function testShouldCompileClosingTag(): void
{
$compiler = new ComponentTagCompiler('<Alert color="primary">test</Alert>', 'alias');
Expand Down
Loading