cacheFile = $cacheFile; $this->builder = $builder; $this->configCache = $configCache ?? new ConfigCache( $this->cacheFile, $isDebugEnabled ?? $this->isEnvVarTrue('DI_CONTAINER_DEBUG') ); $this->dumper = $dumper; $this->cachedContainerClassName = $cachedContainerClassName ?? 'MyCachedContainer'; } public function isFresh(): bool { return $this->configCache->isFresh(); } public function load(): ContainerInterface { if ($this->isFresh()) { return $this->getCachedContainer(); } return $this->forceLoad(); } public function forceLoad(): ContainerInterface { $this->builder->compile(); $this->configCache->write( $this->getDumper()->dump( [ 'class' => $this->cachedContainerClassName, 'base_class' => BaseContainer::class ] ), $this->builder->getResources() ); return $this->getCachedContainer(); } private function getCachedContainer(): ContainerInterface { require_once $this->cacheFile; return new $this->cachedContainerClassName(); } private function isEnvVarTrue(string $envVar): bool { if (!isset($_ENV[$envVar])) { return false; } return filter_var($_ENV[$envVar], FILTER_VALIDATE_BOOLEAN) ?? false; } private function getDumper(): PhpDumper { return $this->dumper ?? new PhpDumper($this->builder); } }