-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed named of PropertiesExtractor, and uses typed ComponentPropert…
…yReflection
- Loading branch information
Showing
7 changed files
with
226 additions
and
159 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,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent; | ||
|
||
/** | ||
* @author Jean-François Lépine <[email protected]> | ||
*/ | ||
class ComponentPropertyReflection | ||
{ | ||
public function __construct( | ||
private readonly ComponentMetadata $metadata, | ||
private readonly string $name, | ||
private readonly string $type = 'mixed', | ||
private readonly mixed $defaultValue = null, | ||
) { | ||
} | ||
|
||
public function getCode(): string | ||
{ | ||
if (null === $this->defaultValue) { | ||
return \sprintf('%s $%s = ""', $this->type, $this->name); | ||
} | ||
|
||
if (\is_bool($this->defaultValue)) { | ||
return \sprintf('%s $%s = %s', $this->type, $this->name, $this->defaultValue ? 'true' : 'false'); | ||
} | ||
|
||
return \sprintf('%s $%s = %s', $this->type, $this->name, json_encode($this->defaultValue)); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function getDefaultValue(): mixed | ||
{ | ||
return $this->defaultValue; | ||
} | ||
|
||
public function getMetadata(): ComponentMetadata | ||
{ | ||
return $this->metadata; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,17 +15,20 @@ | |
use Symfony\UX\TwigComponent\Twig\PropsNode; | ||
use Twig\Environment; | ||
|
||
final class ComponentPropertiesExtractor | ||
/** | ||
* @author Jean-François Lépine <[email protected]> | ||
*/ | ||
final class ComponentReflection | ||
{ | ||
public function __construct( | ||
private readonly Environment $twig, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<array{display: string, name: string, type: string, default: mixed}> | ||
* @return ComponentPropertyReflection[] | ||
*/ | ||
public function getComponentProperties(ComponentMetadata $medata) | ||
public function getProperties(ComponentMetadata $medata): array | ||
{ | ||
if ($medata->isAnonymous()) { | ||
return $this->getAnonymousComponentProperties($medata); | ||
|
@@ -34,8 +37,15 @@ public function getComponentProperties(ComponentMetadata $medata) | |
return $this->getNonAnonymousComponentProperties($medata); | ||
} | ||
|
||
public function getProperty(ComponentMetadata $medata, string $name): ?ComponentPropertyReflection | ||
{ | ||
$properties = $this->getProperties($medata); | ||
|
||
return $properties[$name] ?? null; | ||
} | ||
|
||
/** | ||
* @return array<array{display: string, name: string, type: string, default: mixed}> | ||
* @return ComponentPropertyReflection[] | ||
*/ | ||
private function getNonAnonymousComponentProperties(ComponentMetadata $metadata): array | ||
{ | ||
|
@@ -52,26 +62,13 @@ private function getNonAnonymousComponentProperties(ComponentMetadata $metadata) | |
$typeName = (string) $type; | ||
} | ||
$value = $property->getDefaultValue(); | ||
$propertyDisplay = $typeName.' $'.$propertyName.(null !== $value ? ' = '.json_encode( | ||
$value | ||
) : ''); | ||
$properties[$property->name] = [ | ||
'name' => $propertyName, | ||
'display' => $propertyDisplay, | ||
'type' => $typeName, | ||
'default' => $value, | ||
]; | ||
$properties[$propertyName] = new ComponentPropertyReflection($metadata, $propertyName, $typeName, $value); | ||
} | ||
|
||
foreach ($property->getAttributes(ExposeInTemplate::class) as $exposeAttribute) { | ||
/** @var ExposeInTemplate $attribute */ | ||
$attribute = $exposeAttribute->newInstance(); | ||
$properties[$property->name] = [ | ||
'name' => $attribute->name ?? $property->name, | ||
'display' => $attribute->name ?? $property->name, | ||
'type' => 'mixed', | ||
'default' => null, | ||
]; | ||
$properties[$property->name] = new ComponentPropertyReflection($metadata, $attribute->name ?? $property->name); | ||
} | ||
} | ||
|
||
|
@@ -81,7 +78,7 @@ private function getNonAnonymousComponentProperties(ComponentMetadata $metadata) | |
/** | ||
* Extract properties from {% props %} tag in anonymous template. | ||
* | ||
* @return array<array{display: string, name: string, type: string, default: mixed}> | ||
* @return ComponentPropertyReflection[] | ||
*/ | ||
private function getAnonymousComponentProperties(ComponentMetadata $metadata): array | ||
{ | ||
|
@@ -103,36 +100,18 @@ private function getAnonymousComponentProperties(ComponentMetadata $metadata): a | |
} | ||
|
||
$propertyNames = $propsNode->getAttribute('names'); | ||
$properties = array_combine($propertyNames, $propertyNames); | ||
$properties = []; | ||
foreach ($propertyNames as $propName) { | ||
$properties[$propName] = new ComponentPropertyReflection($metadata, $propName, 'mixed'); | ||
} | ||
|
||
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); | ||
} | ||
$display = $propName.' = '.$value; | ||
$properties[$propName] = [ | ||
'name' => $propName, | ||
'display' => $display, | ||
'type' => \is_bool($value) ? 'bool' : 'mixed', | ||
'default' => $value, | ||
]; | ||
} | ||
} | ||
|
||
foreach ($properties as $propertyData) { | ||
if (\is_string($propertyData)) { | ||
$properties[$propertyData] = [ | ||
'name' => $propertyData, | ||
'display' => $propertyData, | ||
'type' => 'mixed', | ||
'default' => null, | ||
]; | ||
$properties[$propName] = new ComponentPropertyReflection($metadata, $propName, 'mixed', $value); | ||
} | ||
} | ||
|
||
|
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
106 changes: 0 additions & 106 deletions
106
src/TwigComponent/tests/Unit/ComponentPropertiesExtractorTest.php
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.