Skip to content

Commit

Permalink
change conflic error message, and fix classprops
Browse files Browse the repository at this point in the history
  • Loading branch information
WebMamba committed Apr 17, 2024
1 parent e3d30c7 commit c40abb1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/TwigComponent/src/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR
{
$component = $mounted->getComponent();
$metadata = $this->factory->metadataFor($mounted->getName());
$classProps = $metadata->isPublicPropsExposed() ? iterator_to_array($this->exposedVariables($component, $metadata->isPublicPropsExposed())) : [];
$isAnonymous = $mounted->getComponent() instanceof AnonymousComponent;

$classProps = $isAnonymous ? [] : iterator_to_array($this->exposedVariables($component, $metadata->isPublicPropsExposed()));

// expose public properties and properties marked with ExposeInTemplate attribute
$props = array_merge($mounted->getInputProps(), $classProps);
Expand Down
4 changes: 3 additions & 1 deletion src/TwigComponent/src/Twig/PropsNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function compile(Compiler $compiler): void
$compiler
->write('if (isset($context[\'__props\'][\''.$name.'\'])) {')
->raw("\n")
->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\');')
->write('$componentClass = isset($context[\'this\']) ? get_debug_type($context[\'this\']) : "";')
->raw("\n")
->write('throw new \Twig\Error\RuntimeError(\'Cannot define prop "'.$name.'" in template "'.$this->getTemplateName().'". Property already defined in component class "\'.$componentClass.\'".\');')
->raw("\n")
->write('}')
->raw("\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public function testComponentWithPropsFromTemplateAndClass(): void
public function testComponentWithConflictBetweenPropsFromTemplateAndClass(): void
{
$this->expectException(RuntimeError::class);
$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');
$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".');

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

0 comments on commit c40abb1

Please sign in to comment.