Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: move functionality to render sub components from trait to class #38

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@

declare(strict_types=1);

namespace Sinso\Webcomponents\DataProviding\Traits;
namespace Sinso\Webcomponents\DataProviding\Helpers;

use Sinso\Webcomponents\DataProviding\AssertionFailedException;
use Sinso\Webcomponents\DataProviding\ComponentInterface;
use Sinso\Webcomponents\DataProviding\Traits\ContentObjectRendererTrait;
use Sinso\Webcomponents\Dto\ComponentRenderingData;
use Sinso\Webcomponents\Rendering\ComponentRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

trait RenderSubComponent
class SubComponentRenderingHelper
{
use ContentObjectRendererTrait;

private ComponentRenderer $componentRenderer;

public function injectComponentRenderer(ComponentRenderer $componentRenderer): void
{
$this->componentRenderer = $componentRenderer;
}

protected function renderSubComponent(string $componentClassName, $additionalInputData = [], ?string $slot = null): ?string
public function __construct(
private readonly ComponentRenderer $componentRenderer,
) {}

/**
* @param class-string<ComponentInterface> $componentClassName
* @param array<string, mixed> $additionalInputData
*/
public function renderSubComponent(string $componentClassName, array $additionalInputData = [], ?string $slot = null): ?string
{
$component = GeneralUtility::makeInstance($componentClassName);
if (!$component instanceof ComponentInterface) {
throw new \RuntimeException('Component must implement ComponentInterface');
}
$component->setContentObjectRenderer($this->contentObjectRenderer);
/** @var ContentObjectRenderer $contentObjectRenderer */
$contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$contentObjectRenderer->start([]);
$component->setContentObjectRenderer($contentObjectRenderer);
$componentRenderingData = GeneralUtility::makeInstance(ComponentRenderingData::class);
$componentRenderingData->setAdditionalInputData($additionalInputData);
try {
Expand All @@ -41,8 +45,13 @@ protected function renderSubComponent(string $componentClassName, $additionalInp
$properties['slot'] = $slot;
}

$tagName = $componentRenderingData->getTagName();
if ($tagName === null) {
return null;
}

return $this->componentRenderer->renderComponent(
$componentRenderingData->getTagName(),
$tagName,
$componentRenderingData->getTagContent(),
$properties,
);
Expand Down
Loading