mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-01-15 05:41:25 +00:00
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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user