Skip to content

Commit

Permalink
[FEATURE] Populate and read cacheable state of template path providers
Browse files Browse the repository at this point in the history
  • Loading branch information
eliashaeussler committed Jan 15, 2025
1 parent 518fa53 commit 9f6f27b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
5 changes: 5 additions & 0 deletions Classes/Renderer/Template/Path/GlobalPathProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public function getTemplateRootPaths(): array
return $this->templateRootPaths;
}

public function isCacheable(): bool
{
return true;
}

public static function getPriority(): int
{
return 0;
Expand Down
2 changes: 2 additions & 0 deletions Classes/Renderer/Template/Path/PathProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ public function getPartialRootPaths(): array;
*/
public function getTemplateRootPaths(): array;

public function isCacheable(): bool;

public static function getPriority(): int;
}
5 changes: 5 additions & 0 deletions Classes/Renderer/Template/Path/TypoScriptPathProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function getTemplateRootPaths(): array
return $this->getViewConfiguration()[self::TEMPLATES] ?? [];
}

public function isCacheable(): bool
{
return true;
}

/**
* @return array{partialRootPaths?: array<int, string>, templateRootPaths?: array<int, string>}
*/
Expand Down
42 changes: 27 additions & 15 deletions Classes/Renderer/Template/TemplatePaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,56 @@ public function __construct(
*/
public function getPartialRootPaths(): array
{
if ($this->partialRootPaths === null) {
$this->partialRootPaths = $this->resolvePaths(
static fn(Path\PathProvider $pathProvider) => $pathProvider->getPartialRootPaths(),
);
}

return $this->partialRootPaths;
return $this->resolvePaths(
static fn(Path\PathProvider $pathProvider) => $pathProvider->getPartialRootPaths(),
$this->partialRootPaths,
);
}

/**
* @return array<int, string>
*/
public function getTemplateRootPaths(): array
{
if ($this->templateRootPaths === null) {
$this->templateRootPaths = $this->resolvePaths(
static fn(Path\PathProvider $pathProvider) => $pathProvider->getTemplateRootPaths(),
);
}

return $this->templateRootPaths;
return $this->resolvePaths(
static fn(Path\PathProvider $pathProvider) => $pathProvider->getTemplateRootPaths(),
$this->templateRootPaths,
);
}

/**
* @param callable(Path\PathProvider): array<int, string> $mapFunction
* @param array<int, string>|null $rootPaths
* @return array<int, string>
*/
private function resolvePaths(callable $mapFunction): array
private function resolvePaths(callable $mapFunction, ?array &$rootPaths): array
{
// Early return if root paths are already resolved and cached
if ($rootPaths !== null) {
return $rootPaths;
}

$cacheable = true;
$paths = [];

// Resolve root paths from path providers
foreach ($this->pathProviders as $pathProvider) {
\array_unshift($paths, $mapFunction($pathProvider));

if (!$pathProvider->isCacheable()) {
$cacheable = false;
}
}

// Merge and sort all root paths
$mergedPaths = array_replace(...$paths);
ksort($mergedPaths);

// Cache root paths if possible
if ($cacheable) {
$rootPaths = $mergedPaths;
}

return $mergedPaths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function getTemplateRootPaths(): array
return $this->templateRootPaths;
}

public function isCacheable(): bool
{
return true;
}

public static function getPriority(): int
{
return 10;
Expand Down

0 comments on commit 9f6f27b

Please sign in to comment.