mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +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:
parent
0ee91800ab
commit
09563979a2
@ -19,24 +19,7 @@ use Twig\TwigFilter;
|
|||||||
*/
|
*/
|
||||||
class ChillEntityRenderExtension extends AbstractExtension
|
class ChillEntityRenderExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
/**
|
public function __construct(private ChillEntityRenderManagerInterface $renderManager) {}
|
||||||
* @var ChillEntityRender
|
|
||||||
*/
|
|
||||||
protected $defaultRender;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var iterable|ChillEntityRenderInterface[]
|
|
||||||
*/
|
|
||||||
protected $renders = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ChillEntityRenderExtension constructor.
|
|
||||||
*/
|
|
||||||
public function __construct(iterable $renders)
|
|
||||||
{
|
|
||||||
$this->defaultRender = new ChillEntityRender();
|
|
||||||
$this->renders = $renders;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|TwigFilter[]
|
* @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 $this->renderManager->renderBox($entity, $options);
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->getRender($entity, $options)
|
public function renderString(?object $entity, array $options = []): string
|
||||||
->renderBox($entity, $options);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function renderString($entity, array $options = []): string
|
|
||||||
{
|
{
|
||||||
if (null === $entity) {
|
return $this->renderManager->renderString($entity, $options);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ namespace Chill\MainBundle\Templating\Entity;
|
|||||||
* Interface to implement which will render an entity in template on a custom
|
* Interface to implement which will render an entity in template on a custom
|
||||||
* manner.
|
* manner.
|
||||||
*
|
*
|
||||||
* @template T
|
* @template T of object
|
||||||
*/
|
*/
|
||||||
interface ChillEntityRenderInterface
|
interface ChillEntityRenderInterface
|
||||||
{
|
{
|
||||||
@ -31,7 +31,7 @@ interface ChillEntityRenderInterface
|
|||||||
* </span>
|
* </span>
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @param T $entity
|
* @param T|null $entity
|
||||||
*
|
*
|
||||||
* @phpstan-pure
|
* @phpstan-pure
|
||||||
*/
|
*/
|
||||||
@ -42,7 +42,7 @@ interface ChillEntityRenderInterface
|
|||||||
*
|
*
|
||||||
* Example: returning the name of a person.
|
* Example: returning the name of a person.
|
||||||
*
|
*
|
||||||
* @param T $entity
|
* @param T|null $entity
|
||||||
*
|
*
|
||||||
* @phpstan-pure
|
* @phpstan-pure
|
||||||
*/
|
*/
|
||||||
|
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Templating\Entity;
|
||||||
|
|
||||||
|
final readonly class ChillEntityRenderManager implements ChillEntityRenderManagerInterface
|
||||||
|
{
|
||||||
|
private ChillEntityRender $defaultRender;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var iterable<ChillEntityRenderInterface>
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Templating\Entity;
|
||||||
|
|
||||||
|
interface ChillEntityRenderManagerInterface
|
||||||
|
{
|
||||||
|
public function renderBox(?object $entity, array $options = []): string;
|
||||||
|
|
||||||
|
public function renderString(?object $entity, array $options = []): string;
|
||||||
|
}
|
@ -32,11 +32,16 @@ services:
|
|||||||
- { name: twig.extension }
|
- { name: twig.extension }
|
||||||
|
|
||||||
Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension:
|
Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension:
|
||||||
arguments:
|
|
||||||
$renders: !tagged_iterator chill.render_entity
|
|
||||||
tags:
|
tags:
|
||||||
- { name: twig.extension }
|
- { name: twig.extension }
|
||||||
|
|
||||||
|
Chill\MainBundle\Templating\Entity\ChillEntityRenderManager:
|
||||||
|
arguments:
|
||||||
|
$renders: !tagged_iterator chill.render_entity
|
||||||
|
|
||||||
|
Chill\MainBundle\Templating\Entity\ChillEntityRenderManagerInterface:
|
||||||
|
alias: 'Chill\MainBundle\Templating\Entity\ChillEntityRenderManager'
|
||||||
|
|
||||||
Chill\MainBundle\Templating\Entity\CommentRender:
|
Chill\MainBundle\Templating\Entity\CommentRender:
|
||||||
tags:
|
tags:
|
||||||
- { name: 'chill.render_entity' }
|
- { name: 'chill.render_entity' }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user