Skip to content

[TwigComponent] Add 'require ** as **' to alias components #1209

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

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
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 src/TwigComponent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Deprecate calling `ComponentTemplateFinder` constructor without `directory` argument.
- Add profiler integration: `TwigComponentDataCollector` and debug toolbar templates
- Add search feature in `debug:twig-component` command.
- Add `{% require % as % %}` to reference components via alias.

## 2.12.0

Expand Down
31 changes: 30 additions & 1 deletion src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function preLexComponents(string $input): string
throw new SyntaxError(sprintf('Expected closing tag "</twig:%s>" not found.', $lastComponent), $this->line);
}

return $output;
return $this->processRequires($output);
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about dispatching an event PostTwigPrelexing and putting the processRequires function's content in a Subscriber?

}

private function consumeComponentName(string $customExceptionMessage = null): string
Expand Down Expand Up @@ -495,4 +495,33 @@ private function addDefaultBlock(): string

return '{% block content %}';
}

private function processRequires(string $input)
{
// matches "{% require Some:Component:Name as ComponentAlias %}"
$pattern = '/{%\s*require\s+([A-Za-z0-9_:@.\-]+)\s+as\s+([A-Za-z0-9_:@.\-]+)\s*%}\n?/';
$matches = [];
preg_match_all($pattern, $input, $matches, \PREG_SET_ORDER);
// replace all "{{ component('ComponentAlias' ... ) }}" with "{{ component('Some/Component/Name', ...) }}"
// and all "{% component 'ComponentAlias' ... %}" with "{% component 'Some/Component/Name' ... %}"
foreach ($matches as $match) {
$input = str_replace(
sprintf("{{ component('%s'", $match[2]),
sprintf("{{ component('%s'", $match[1]),
$input
);
$input = str_replace(
sprintf("{%% component '%s'", $match[2]),
sprintf("{%% component '%s'", $match[1]),
$input
);
$input = str_replace(
$match[0],
'',
$input
);
}

return $input;
}
}
61 changes: 61 additions & 0 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,61 @@ public static function getLexTests(): iterable
'{{ component(\'foo\') }}',
];

yield 'simple_component_with_require' => [
'{% require foo as bar %}<twig:bar />',
'{{ component(\'foo\') }}',
];

yield 'simple_component_with_long_name' => [
'{% require long:component:name as foo %}<twig:foo />',
'{{ component(\'long:component:name\') }}',
];

yield 'simple_component_with_alias_substring_of_other_component' => [
'{% require long:component:name as foo %}<twig:foo:bar />',
'{{ component(\'foo:bar\') }}',
];

yield 'simple_component_with_multiple_requires' => [
'{% require long:component:name as foo %}{% require other:long:component:name as bar %}<twig:bar /><twig:foo />',
'{{ component(\'other:long:component:name\') }}{{ component(\'long:component:name\') }}',
];

yield 'simple_component_with_require_not_used' => [
'{% require long:component:name as bar %}<twig:foo />',
'{{ component(\'foo\') }}',
];

yield 'component_with_attributes' => [
'<twig:foo bar="baz" with_quotes="It\'s with quotes" />',
"{{ component('foo', { bar: 'baz', with_quotes: 'It\'s with quotes' }) }}",
];

yield 'component_with_attributes_and_require' => [
'{% require long:component:name as foo %}<twig:foo bar="baz" with_quotes="It\'s with quotes" />',
"{{ component('long:component:name', { bar: 'baz', with_quotes: 'It\'s with quotes' }) }}",
];

yield 'component_with_dynamic_attributes' => [
'<twig:foo dynamic="{{ dynamicVar }}" :otherDynamic="anotherVar" />',
'{{ component(\'foo\', { dynamic: (dynamicVar), otherDynamic: anotherVar }) }}',
];

yield 'component_with_dynamic_attributes_with_require' => [
'{% require long:component:name as foo %}<twig:foo dynamic="{{ dynamicVar }}" :otherDynamic="anotherVar" />',
'{{ component(\'long:component:name\', { dynamic: (dynamicVar), otherDynamic: anotherVar }) }}',
];

yield 'component_with_closing_tag' => [
'<twig:foo></twig:foo>',
'{% component \'foo\' %}{% endcomponent %}',
];

yield 'component_with_closing_tag_with_require' => [
'{% require long:component:name as foo %}<twig:foo></twig:foo>',
'{% component \'long:component:name\' %}{% endcomponent %}',
];

yield 'component_with_block' => [
'<twig:foo><twig:block name="foo_block">Foo</twig:block></twig:foo>',
'{% component \'foo\' %}{% block foo_block %}Foo{% endblock %}{% endcomponent %}',
Expand Down Expand Up @@ -100,18 +140,34 @@ public static function getLexTests(): iterable
'<twig:foo:bar></twig:foo:bar>',
'{% component \'foo:bar\' %}{% endcomponent %}',
];
yield 'component_with_require_with_character_:_on_his_name' => [
'{% require long:component:name as foo:bar %}<twig:foo:bar></twig:foo:bar>',
'{% component \'long:component:name\' %}{% endcomponent %}',
];
yield 'component_with_character_@_on_his_name' => [
'<twig:@foo></twig:@foo>',
'{% component \'@foo\' %}{% endcomponent %}',
];
yield 'component_with_character_@_on_his_name_with_require' => [
'{% require long:component:n@me as @foo %}<twig:@foo></twig:@foo>',
'{% component \'long:component:n@me\' %}{% endcomponent %}',
];
yield 'component_with_character_-_on_his_name' => [
'<twig:foo-bar></twig:foo-bar>',
'{% component \'foo-bar\' %}{% endcomponent %}',
];
yield 'component_with_character_-_on_his_name_with_require' => [
'{% require long:component-name as foo-bar %}<twig:foo-bar></twig:foo-bar>',
'{% component \'long:component-name\' %}{% endcomponent %}',
];
yield 'component_with_character_._on_his_name' => [
'<twig:foo.bar></twig:foo.bar>',
'{% component \'foo.bar\' %}{% endcomponent %}',
];
yield 'component_with_character_._on_his_name_with_require' => [
'{% require long:component.name as foo.bar %}<twig:foo.bar></twig:foo.bar>',
'{% component \'long:component.name\' %}{% endcomponent %}',
];
yield 'nested_component_2_levels' => [
'<twig:foo><twig:block name="child"><twig:bar><twig:block name="message">Hello World!</twig:block></twig:bar></twig:block></twig:foo>',
'{% component \'foo\' %}{% block child %}{% component \'bar\' %}{% block message %}Hello World!{% endblock %}{% endcomponent %}{% endblock %}{% endcomponent %}',
Expand Down Expand Up @@ -202,6 +258,11 @@ public static function getLexTests(): iterable
'{# <twig:Alert/> #} {{ component(\'Alert\') }}',
];

yield 'ignore_twig_comment_with_require' => [
'{% require component:alert as Alert %} {# <twig:Alert/> #} <twig:Alert/>',
' {# <twig:Alert/> #} {{ component(\'component:alert\') }}',
];

yield 'file_ended_with_comments' => [
'{# <twig:Alert/> #}',
'{# <twig:Alert/> #}',
Expand Down