cachePath = $cachePath; $this->legacyContainer = $legacyContainer; $this->cache = $cache ?? new ContainerCache( $cachePath . '_di/container.php', $this, null, null, $isDebugEnabled ); parent::__construct(); } public function build(): ContainerInterface { if ($this->cache->isFresh()) { return $this->cache->load(); } return $this->forceBuild(); } public function forceBuild(): ContainerInterface { if (!is_writable($this->cachePath)) { throw new InvalidArgumentException( sprintf( 'DI container build requires directory "%" to be writable', $this->cachePath ) ); } file_put_contents($this->cachePath . '/services.php', $this->getTemporaryServiceFileContent()); $phpLoader = new PhpFileLoader( $this, new FileLocator( [ $this->cachePath ] ) ); $phpLoader->load('services.php'); return $this->cache->forceLoad(); } /** * @inheritDoc */ public function get(string $id, int $invalidBehavior = SymfonyContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) { try { return parent::get($id, $invalidBehavior); } catch (SymfonyServiceNotFoundException $exception) { } try { return $this->legacyContainer->get($id); } catch (ServiceNotFoundException $exception) { throw new SymfonyServiceNotFoundException($id); } } /** * @inheritDoc */ public function has(string $id) { if (parent::has($id)) { return true; } try { $this->legacyContainer->get($id); return true; } catch (ServiceNotFoundException $exception) { return false; } } /** * @inheritDoc */ public function findDefinition(string $id) { try { return parent::findDefinition($id); } catch (SymfonyServiceNotFoundException $exception) { return (new Definition($id)) ->setAutowired(true) ->setPublic(true) ->setFactory(new Reference(LegacyServiceGateway::class)) ->setArguments([$id]); } } private function getTemporaryServiceFileContent(): string { $contents = []; /** @var common_ext_Extension $extension */ foreach ($this->getExtensionsManager()->getInstalledExtensions() as $extension) { foreach ($extension->getManifest()->getContainerServiceProvider() as $serviceProvider) { $contents[] = '(new ' . $serviceProvider . '())($configurator);'; } } return vsprintf( 'legacyContainer->get(common_ext_ExtensionsManager::SERVICE_ID); } }