-
-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new ComponentPropertiesExtractor to extract properties from TwigC…
…omponent
- Loading branch information
Showing
5 changed files
with
122 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Symfony\UX\TwigComponent; | ||
|
||
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; | ||
use Symfony\UX\TwigComponent\Twig\PropsNode; | ||
use Twig\Environment; | ||
|
||
final class ComponentPropertiesExtractor | ||
{ | ||
public function __construct( | ||
private readonly Environment $twig | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function getComponentProperties(ComponentMetadata $medata) | ||
{ | ||
if ($medata->isAnonymous()) { | ||
return $this->getAnonymousComponentProperties($medata); | ||
} | ||
|
||
return $this->getNonAnonymousComponentProperties($medata); | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private function getNonAnonymousComponentProperties(ComponentMetadata $metadata): array | ||
{ | ||
$properties = []; | ||
$reflectionClass = new \ReflectionClass($metadata->getClass()); | ||
foreach ($reflectionClass->getProperties() as $property) { | ||
$propertyName = $property->getName(); | ||
|
||
if ($metadata->isPublicPropsExposed() && $property->isPublic()) { | ||
$type = $property->getType(); | ||
if ($type instanceof \ReflectionNamedType) { | ||
$typeName = $type->getName(); | ||
} else { | ||
$typeName = (string)$type; | ||
} | ||
$value = $property->getDefaultValue(); | ||
$propertyDisplay = $typeName . ' $' . $propertyName . (null !== $value ? ' = ' . json_encode( | ||
$value | ||
) : ''); | ||
$properties[$property->name] = $propertyDisplay; | ||
} | ||
|
||
foreach ($property->getAttributes(ExposeInTemplate::class) as $exposeAttribute) { | ||
/** @var ExposeInTemplate $attribute */ | ||
$attribute = $exposeAttribute->newInstance(); | ||
$properties[$property->name] = $attribute->name ?? $property->name; | ||
} | ||
} | ||
|
||
return $properties; | ||
} | ||
|
||
/** | ||
* Extract properties from {% props %} tag in anonymous template. | ||
* | ||
* @return array<string, string> | ||
*/ | ||
private function getAnonymousComponentProperties(ComponentMetadata $metadata): array | ||
{ | ||
$source = $this->twig->load($metadata->getTemplate())->getSourceContext(); | ||
$tokenStream = $this->twig->tokenize($source); | ||
$moduleNode = $this->twig->parse($tokenStream); | ||
|
||
$propsNode = null; | ||
foreach ($moduleNode->getNode('body') as $bodyNode) { | ||
foreach ($bodyNode as $node) { | ||
if (PropsNode::class === $node::class) { | ||
$propsNode = $node; | ||
break 2; | ||
} | ||
} | ||
} | ||
if (!$propsNode instanceof PropsNode) { | ||
return []; | ||
} | ||
|
||
$propertyNames = $propsNode->getAttribute('names'); | ||
$properties = array_combine($propertyNames, $propertyNames); | ||
foreach ($propertyNames as $propName) { | ||
if ($propsNode->hasNode($propName) | ||
&& ($valueNode = $propsNode->getNode($propName)) | ||
&& $valueNode->hasAttribute('value') | ||
) { | ||
$value = $valueNode->getAttribute('value'); | ||
if (\is_bool($value)) { | ||
$value = $value ? 'true' : 'false'; | ||
} else { | ||
$value = json_encode($value); | ||
} | ||
$properties[$propName] = $propName . ' = ' . $value; | ||
} | ||
} | ||
|
||
return $properties; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters