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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user