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