refactor(view): simplify dependency injection for template initializers

- Replace `DefaultContainer` lookups with direct constructor injection in `TemplateProcessorInitializer` and `TemplateRendererInitializer`.
- Streamline method logic by removing redundant operations and ensuring dependencies are passed explicitly.
- Enhance readability and maintainability by reducing unnecessary indirections.
This commit is contained in:
2025-11-03 22:48:27 +01:00
parent 84a5a3fa21
commit 1af63ed7ec
2 changed files with 10 additions and 22 deletions

View File

@@ -23,8 +23,7 @@ final readonly class TemplateProcessorInitializer
{
public function __construct(
private DefaultContainer $container,
) {
}
) {}
#[Initializer]
public function __invoke(Cache $cache): TemplateProcessor
@@ -55,7 +54,7 @@ final readonly class TemplateProcessorInitializer
$performanceTracker->enable();
}
$processor = new TemplateProcessor(
return new TemplateProcessor(
astTransformers: $astTransformers,
stringProcessors: $stringProcessors,
container: $this->container,
@@ -63,10 +62,5 @@ final readonly class TemplateProcessorInitializer
compiledTemplateCache: $compiledTemplateCache,
performanceTracker: $performanceTracker
);
$this->container->singleton(TemplateProcessor::class, $processor);
return $processor;
}
}

View File

@@ -13,28 +13,22 @@ use App\Framework\View\Loading\TemplateLoader;
final readonly class TemplateRendererInitializer
{
public function __construct(
private DefaultContainer $container,
private TemplateProcessor $templateProcessor,
private TemplateLoader $loader,
private PerformanceService $performanceService,
private Cache $cache,
) {}
#[Initializer]
public function __invoke(): TemplateRenderer
{
$templateProcessor = $this->container->get(TemplateProcessor::class);
$loader = $this->container->get(TemplateLoader::class);
/** @var PerformanceService $performanceService */
$performanceService = $this->container->get(PerformanceService::class);
/** @var Cache $cache */
$cache = $this->container->get(Cache::class);
$cacheEnabled = false;
return new Engine(
loader: $loader,
performanceService: $performanceService,
processor: $templateProcessor,
cache: $cache,
loader: $this->loader,
performanceService: $this->performanceService,
processor: $this->templateProcessor,
cache: $this->cache,
cacheEnabled: $cacheEnabled, // Use same cache state for Engine
);
}