From 09563979a2a0591263c981d13610405dabff1fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 12 Sep 2024 11:17:54 +0200 Subject: [PATCH] Refactor entity rendering with manager pattern Introduce ChillEntityRenderManager to centralize entity rendering logic, reducing redundancy and improving code organization. Update dependencies and service configuration to support the new manager pattern, enhancing maintainability and flexibility of entity rendering in templates. --- .../Entity/ChillEntityRenderExtension.php | 48 ++------------- .../Entity/ChillEntityRenderInterface.php | 6 +- .../Entity/ChillEntityRenderManager.php | 59 +++++++++++++++++++ .../ChillEntityRenderManagerInterface.php | 19 ++++++ .../config/services/templating.yaml | 9 ++- 5 files changed, 93 insertions(+), 48 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManager.php create mode 100644 src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManagerInterface.php diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtension.php b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtension.php index 9c8d4599e..8a9e70229 100644 --- a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtension.php +++ b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtension.php @@ -19,24 +19,7 @@ use Twig\TwigFilter; */ class ChillEntityRenderExtension extends AbstractExtension { - /** - * @var ChillEntityRender - */ - protected $defaultRender; - - /** - * @var iterable|ChillEntityRenderInterface[] - */ - protected $renders = []; - - /** - * ChillEntityRenderExtension constructor. - */ - public function __construct(iterable $renders) - { - $this->defaultRender = new ChillEntityRender(); - $this->renders = $renders; - } + public function __construct(private ChillEntityRenderManagerInterface $renderManager) {} /** * @return array|TwigFilter[] @@ -53,34 +36,13 @@ class ChillEntityRenderExtension extends AbstractExtension ]; } - public function renderBox($entity, array $options = []): string + public function renderBox(?object $entity, array $options = []): string { - if (null === $entity) { - return ''; - } - - return $this->getRender($entity, $options) - ->renderBox($entity, $options); + return $this->renderManager->renderBox($entity, $options); } - public function renderString($entity, array $options = []): string + public function renderString(?object $entity, array $options = []): string { - if (null === $entity) { - return ''; - } - - return $this->getRender($entity, $options) - ->renderString($entity, $options); - } - - protected function getRender($entity, $options): ?ChillEntityRenderInterface - { - foreach ($this->renders as $render) { - if ($render->supports($entity, $options)) { - return $render; - } - } - - return $this->defaultRender; + return $this->renderManager->renderString($entity, $options); } } diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderInterface.php b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderInterface.php index d628420c7..7a7a5e1fe 100644 --- a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderInterface.php +++ b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderInterface.php @@ -15,7 +15,7 @@ namespace Chill\MainBundle\Templating\Entity; * Interface to implement which will render an entity in template on a custom * manner. * - * @template T + * @template T of object */ interface ChillEntityRenderInterface { @@ -31,7 +31,7 @@ interface ChillEntityRenderInterface * * ``` * - * @param T $entity + * @param T|null $entity * * @phpstan-pure */ @@ -42,7 +42,7 @@ interface ChillEntityRenderInterface * * Example: returning the name of a person. * - * @param T $entity + * @param T|null $entity * * @phpstan-pure */ diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManager.php b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManager.php new file mode 100644 index 000000000..6437a9c24 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManager.php @@ -0,0 +1,59 @@ + + */ + private iterable $renders; + + public function __construct(iterable $renders) + { + $this->defaultRender = new ChillEntityRender(); + $this->renders = $renders; + } + + public function renderBox($entity, array $options = []): string + { + if (null === $entity) { + return ''; + } + + return $this->getRender($entity, $options) + ->renderBox($entity, $options); + } + + public function renderString($entity, array $options = []): string + { + if (null === $entity) { + return ''; + } + + return $this->getRender($entity, $options) + ->renderString($entity, $options); + } + + private function getRender($entity, $options): ChillEntityRenderInterface + { + foreach ($this->renders as $render) { + if ($render->supports($entity, $options)) { + return $render; + } + } + + return $this->defaultRender; + } +} diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManagerInterface.php b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManagerInterface.php new file mode 100644 index 000000000..44b996d73 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManagerInterface.php @@ -0,0 +1,19 @@ +