-
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #2357 [TwigComponent] Store mount methods in compiler pass (s…
…mnandre) This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] Store mount methods in compiler pass | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Issues | Fix #... | License | MIT (internal optimization) * collection mount metadata (premount, mount, postmount) in compiler pass * store in ComponentMetadata object * start removing methods from AsTwigComponent Note: i had to update an internal test method in LiveComponent Commits ------- c67028f [TwigComponent] Store mount methods in compiler pass
- Loading branch information
Showing
13 changed files
with
105 additions
and
166 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
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 |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\UX\TwigComponent\Attribute\PostMount; | ||
use Symfony\UX\TwigComponent\Attribute\PreMount; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -68,7 +70,7 @@ public function process(ContainerBuilder $container): void | |
$tag['service_id'] = $id; | ||
$tag['class'] = $definition->getClass(); | ||
$tag['template'] = $tag['template'] ?? $this->calculateTemplate($tag['key'], $defaults); | ||
$componentConfig[$tag['key']] = $tag; | ||
$componentConfig[$tag['key']] = [...$tag, ...$this->getMountMethods($tag['class'])]; | ||
$componentReferences[$tag['key']] = new Reference($id); | ||
$componentNames[] = $tag['key']; | ||
$componentClassMap[$tag['class']] = $tag['key']; | ||
|
@@ -109,4 +111,35 @@ private function calculateTemplate(string $componentName, ?array $defaults): str | |
|
||
return \sprintf('%s/%s.html.twig', rtrim($directory, '/'), str_replace(':', '/', $componentName)); | ||
} | ||
|
||
/** | ||
* @param class-string $component | ||
* | ||
* @return array{preMount: string[], mount: string[], postMount: string[]} | ||
*/ | ||
private function getMountMethods(string $component): array | ||
{ | ||
$preMount = $mount = $postMount = []; | ||
foreach ((new \ReflectionClass($component))->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { | ||
foreach ($method->getAttributes(PreMount::class) as $attribute) { | ||
$preMount[$method->getName()] = $attribute->newInstance()->priority; | ||
} | ||
foreach ($method->getAttributes(PostMount::class) as $attribute) { | ||
$postMount[$method->getName()] = $attribute->newInstance()->priority; | ||
} | ||
if ('mount' === $method->getName()) { | ||
$mount['mount'] = 0; | ||
} | ||
} | ||
|
||
arsort($preMount, \SORT_NUMERIC); | ||
arsort($mount, \SORT_NUMERIC); | ||
arsort($postMount, \SORT_NUMERIC); | ||
|
||
return [ | ||
'pre_mount' => array_keys($preMount), | ||
'mount' => array_keys($mount), | ||
'post_mount' => array_keys($postMount), | ||
]; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -7,5 +7,4 @@ | |
#[AsTwigComponent] | ||
class AcmeRootComponent | ||
{ | ||
|
||
} |
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 |
---|---|---|
|
@@ -7,5 +7,4 @@ | |
#[AsTwigComponent] | ||
class AcmeOtherComponent | ||
{ | ||
|
||
} |
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 |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
class Conflict | ||
{ | ||
public string $name; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -7,5 +7,4 @@ | |
#[AsTwigComponent] | ||
class ComponentInSubDirectory | ||
{ | ||
|
||
} |
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
Oops, something went wrong.