vendor/ibexa/twig-components/src/lib/Component/TemplateComponent.php line 45

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\TwigComponents\Component;
  8. use Ibexa\Contracts\TwigComponents\ComponentInterface;
  9. use Twig\Environment;
  10. class TemplateComponent implements ComponentInterface
  11. {
  12. private Environment $twig;
  13. private string $template;
  14. /**
  15. * @var array<mixed>
  16. */
  17. private array $parameters;
  18. /**
  19. * @param array<mixed> $parameters
  20. */
  21. public function __construct(
  22. Environment $twig,
  23. string $template,
  24. array $parameters = []
  25. ) {
  26. $this->twig = $twig;
  27. $this->template = $template;
  28. $this->parameters = $parameters;
  29. }
  30. /**
  31. * @param array<mixed> $parameters
  32. */
  33. public function render(array $parameters = []): string
  34. {
  35. $parameters = $this->getParameters($parameters);
  36. return $this->twig->render($this->template, $parameters);
  37. }
  38. /**
  39. * @param array<mixed> $parameters
  40. *
  41. * @return array<mixed>
  42. */
  43. protected function getParameters(array $parameters): array
  44. {
  45. return $parameters + $this->parameters;
  46. }
  47. }