Skip to content

Commit 2555d26

Browse files
author
matheo
committed
Revert "props tag remove props from attributes"
This reverts commit eb6e3a2.
1 parent a9d422e commit 2555d26

File tree

6 files changed

+11
-49
lines changed

6 files changed

+11
-49
lines changed

src/TwigComponent/src/AnonymousComponent.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,8 @@ final class AnonymousComponent
2222
{
2323
private array $props;
2424

25-
public function mount(&$data = [], $propNames = []): void
25+
public function mount($props = []): void
2626
{
27-
$props = [];
28-
foreach ($propNames as $propName) {
29-
if (isset($data[$propName])) {
30-
$props[$propName] = $data[$propName];
31-
unset($data[$propName]);
32-
}
33-
}
34-
3527
$this->props = $props;
3628
}
3729

src/TwigComponent/src/ComponentFactory.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class ComponentFactory
3030
* @param array<class-string, string> $classMap
3131
*/
3232
public function __construct(
33-
private AnonymousComponentTemplateParserInterface $componentTemplateFinder,
33+
private ComponentTemplateFinderInterface $componentTemplateFinder,
3434
private ServiceLocator $components,
3535
private PropertyAccessorInterface $propertyAccessor,
3636
private EventDispatcherInterface $eventDispatcher,
@@ -77,7 +77,7 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
7777
$originalData = $data;
7878
$data = $this->preMount($component, $data);
7979

80-
$this->mount($component, $data, $componentMetadata);
80+
$this->mount($component, $data);
8181

8282
// set data that wasn't set in mount on the component directly
8383
foreach ($data as $property => $value) {
@@ -103,10 +103,6 @@ public function mountFromObject(object $component, array $data, ComponentMetadat
103103
}
104104

105105
$data[$key] = $value;
106-
107-
if (!\is_scalar($value) && null !== $value) {
108-
throw new \LogicException(sprintf('A "%s" prop was passed when creating the "%s" component. No matching %s property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a %s). Did you mean to pass this to your component or is there a typo on its name?', $key, $componentMetadata->getName(), $key, get_debug_type($value)));
109-
}
110106
}
111107

112108
return new MountedComponent(
@@ -125,7 +121,7 @@ public function get(string $name): object
125121
return $this->getComponent($name);
126122
}
127123

128-
private function mount(object $component, array &$data, ComponentMetadata $metadata): void
124+
private function mount(object $component, array &$data): void
129125
{
130126
try {
131127
$method = (new \ReflectionClass($component))->getMethod('mount');
@@ -135,7 +131,7 @@ private function mount(object $component, array &$data, ComponentMetadata $metad
135131
}
136132

137133
if ($component instanceof AnonymousComponent) {
138-
$component->mount($data, $this->componentTemplateFinder->findComponentProps($metadata->getTemplate()));
134+
$component->mount($data);
139135

140136
return;
141137
}

src/TwigComponent/src/AnonymousAnonymousComponentTemplateParser.php renamed to src/TwigComponent/src/ComponentTemplateFinder.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @author Matheo Daninos <[email protected]>
1818
*/
19-
final class AnonymousAnonymousComponentTemplateParser implements AnonymousComponentTemplateParserInterface
19+
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface
2020
{
2121
public function __construct(
2222
private Environment $environment
@@ -46,23 +46,4 @@ public function findAnonymousComponentTemplate(string $name): ?string
4646

4747
return null;
4848
}
49-
50-
public function findComponentProps(string $name): array
51-
{
52-
$loader = $this->environment->getLoader();
53-
$templateContent = $loader->getSourceContext($this->findAnonymousComponentTemplate($name))->getCode();
54-
55-
$pattern = '/{%\s*props\s*(.*?)\s*%}/';
56-
preg_match($pattern, $templateContent, $matches);
57-
58-
if (isset($matches[1])) {
59-
$props = $matches[1];
60-
$props = preg_replace('/\s/', '', $props);
61-
$propsArray = explode(',', $props);
62-
63-
return array_map(fn (string $propName) => strtok($propName, '='), $propsArray);
64-
}
65-
66-
return [];
67-
}
6849
}

src/TwigComponent/src/AnonymousComponentTemplateParserInterface.php renamed to src/TwigComponent/src/ComponentTemplateFinderInterface.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@
1414
/**
1515
* @author Matheo Daninos <[email protected]>
1616
*/
17-
interface AnonymousComponentTemplateParserInterface
17+
interface ComponentTemplateFinderInterface
1818
{
1919
public function findAnonymousComponentTemplate(string $name): ?string;
20-
21-
/**
22-
* @return array<string>
23-
*/
24-
public function findComponentProps(string $name): array;
2520
}

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
use Symfony\Component\DependencyInjection\Exception\LogicException;
1818
use Symfony\Component\DependencyInjection\Extension\Extension;
1919
use Symfony\Component\DependencyInjection\Reference;
20-
use Symfony\UX\TwigComponent\AnonymousAnonymousComponentTemplateParser;
2120
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
2221
use Symfony\UX\TwigComponent\ComponentFactory;
2322
use Symfony\UX\TwigComponent\ComponentRenderer;
2423
use Symfony\UX\TwigComponent\ComponentRendererInterface;
2524
use Symfony\UX\TwigComponent\ComponentStack;
25+
use Symfony\UX\TwigComponent\ComponentTemplateFinder;
2626
use Symfony\UX\TwigComponent\DependencyInjection\Compiler\TwigComponentPass;
2727
use Symfony\UX\TwigComponent\Twig\ComponentExtension;
2828
use Symfony\UX\TwigComponent\Twig\ComponentLexer;
@@ -41,7 +41,7 @@ public function load(array $configs, ContainerBuilder $container): void
4141
throw new LogicException('The TwigBundle is not registered in your application. Try running "composer require symfony/twig-bundle".');
4242
}
4343

44-
$container->register('ux.twig_component.component_template_finder', AnonymousAnonymousComponentTemplateParser::class)
44+
$container->register('ux.twig_component.component_template_finder', ComponentTemplateFinder::class)
4545
->setArguments([
4646
new Reference('twig'),
4747
])
@@ -78,7 +78,7 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %
7878
])
7979
;
8080

81-
$container->register(AnonymousAnonymousComponentTemplateParser::class, 'ux.twig_component.component_template_finder');
81+
$container->register(ComponentTemplateFinder::class, 'ux.twig_component.component_template_finder');
8282

8383
$container->register('ux.twig_component.twig.component_extension', ComponentExtension::class)
8484
->addTag('twig.extension')
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
{% props user %}
2-
3-
<div {{ attributes }} class='user-card'>
1+
<div class='user-card'>
42
<p>{{ user.name }}</p>
53
<p>{{ user.email }}</p>
64
</div>

0 commit comments

Comments
 (0)