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( public function __construct(
private DefaultContainer $container, private DefaultContainer $container,
) { ) {}
}
#[Initializer] #[Initializer]
public function __invoke(Cache $cache): TemplateProcessor public function __invoke(Cache $cache): TemplateProcessor
@@ -55,7 +54,7 @@ final readonly class TemplateProcessorInitializer
$performanceTracker->enable(); $performanceTracker->enable();
} }
$processor = new TemplateProcessor( return new TemplateProcessor(
astTransformers: $astTransformers, astTransformers: $astTransformers,
stringProcessors: $stringProcessors, stringProcessors: $stringProcessors,
container: $this->container, container: $this->container,
@@ -63,10 +62,5 @@ final readonly class TemplateProcessorInitializer
compiledTemplateCache: $compiledTemplateCache, compiledTemplateCache: $compiledTemplateCache,
performanceTracker: $performanceTracker 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 final readonly class TemplateRendererInitializer
{ {
public function __construct( public function __construct(
private DefaultContainer $container, private TemplateProcessor $templateProcessor,
private TemplateLoader $loader,
private PerformanceService $performanceService,
private Cache $cache,
) {} ) {}
#[Initializer] #[Initializer]
public function __invoke(): TemplateRenderer 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; $cacheEnabled = false;
return new Engine( return new Engine(
loader: $loader, loader: $this->loader,
performanceService: $performanceService, performanceService: $this->performanceService,
processor: $templateProcessor, processor: $this->templateProcessor,
cache: $cache, cache: $this->cache,
cacheEnabled: $cacheEnabled, // Use same cache state for Engine cacheEnabled: $cacheEnabled, // Use same cache state for Engine
); );
} }