Skip to content

Commit c40abb1

Browse files
committed
change conflic error message, and fix classprops
1 parent e3d30c7 commit c40abb1

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR
107107
{
108108
$component = $mounted->getComponent();
109109
$metadata = $this->factory->metadataFor($mounted->getName());
110-
$classProps = $metadata->isPublicPropsExposed() ? iterator_to_array($this->exposedVariables($component, $metadata->isPublicPropsExposed())) : [];
110+
$isAnonymous = $mounted->getComponent() instanceof AnonymousComponent;
111+
112+
$classProps = $isAnonymous ? [] : iterator_to_array($this->exposedVariables($component, $metadata->isPublicPropsExposed()));
111113

112114
// expose public properties and properties marked with ExposeInTemplate attribute
113115
$props = array_merge($mounted->getInputProps(), $classProps);

src/TwigComponent/src/Twig/PropsNode.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public function compile(Compiler $compiler): void
3737
$compiler
3838
->write('if (isset($context[\'__props\'][\''.$name.'\'])) {')
3939
->raw("\n")
40-
->write('throw new \Twig\Error\RuntimeError(\'The prop: '.$name.' is defined both in the template and in the class. You need to remove it from the class or the template\');')
40+
->write('$componentClass = isset($context[\'this\']) ? get_debug_type($context[\'this\']) : "";')
41+
->raw("\n")
42+
->write('throw new \Twig\Error\RuntimeError(\'Cannot define prop "'.$name.'" in template "'.$this->getTemplateName().'". Property already defined in component class "\'.$componentClass.\'".\');')
4143
->raw("\n")
4244
->write('}')
4345
->raw("\n")

src/TwigComponent/tests/Integration/ComponentExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function testComponentWithPropsFromTemplateAndClass(): void
354354
public function testComponentWithConflictBetweenPropsFromTemplateAndClass(): void
355355
{
356356
$this->expectException(RuntimeError::class);
357-
$this->expectExceptionMessage('The prop: name is defined both in the template and in the class. You need to remove it from the class or the template');
357+
$this->expectExceptionMessage('Cannot define prop "name" in template "components/Conflict.html.twig". Property already defined in component class "Symfony\UX\TwigComponent\Tests\Fixtures\Component\Conflict".');
358358

359359
self::getContainer()->get(Environment::class)->render('component_with_conflict_between_props_from_template_and_class.html.twig');
360360
}

0 commit comments

Comments
 (0)