/** * Loads a cached registry file's data into a registry * * @param string $filePath The cache registry file path * @param IBootstrapperRegistry $registry The registry to read settings into */ protected function loadRegistryFromCache($filePath, IBootstrapperRegistry &$registry) { $rawContents = file_get_contents($filePath); $decodedContents = json_decode($rawContents, true); foreach ($decodedContents["eager"] as $eagerBootstrapperClass) { $registry->registerEagerBootstrapper($eagerBootstrapperClass); } foreach ($decodedContents["lazy"] as $boundClass => $bindingData) { if ($bindingData["target"] === null) { $registry->registerLazyBootstrapper([$boundClass], $bindingData["bootstrapper"]); } else { $registry->registerLazyBootstrapper([[$boundClass => $bindingData["target"]]], $bindingData["bootstrapper"]); } } }
/** * Dispatches the registry lazily * * @param IBootstrapperRegistry $registry The bootstrapper registry * @param array $boundClassesToBindingData The mapping of bound classes to their targets and bootstrappers * @throws RuntimeException Thrown if there was a problem dispatching the bootstrappers */ private function dispatchLazily(IBootstrapperRegistry $registry, array $boundClassesToBindingData) { // This gets passed around by reference so that it'll have the latest objects come time to shut down $bootstrapperObjects = []; foreach ($boundClassesToBindingData as $boundClass => $bindingData) { $bootstrapperClass = $bindingData["bootstrapper"]; $target = $bindingData["target"]; $this->container->bind($boundClass, function () use($registry, &$bootstrapperObjects, $boundClass, $bootstrapperClass, $target) { $bootstrapper = $registry->getInstance($bootstrapperClass); if (!in_array($bootstrapper, $bootstrapperObjects)) { $bootstrapperObjects[] = $bootstrapper; } if (!isset($this->runBootstrappers[$bootstrapperClass])) { $bootstrapper->initialize(); $bootstrapper->registerBindings($this->container); $this->container->call([$bootstrapper, "run"], [], true); $this->runBootstrappers[$bootstrapperClass] = true; } return $this->container->makeShared($boundClass, $target); }, $target); } // Call the shutdown method $this->taskDispatcher->registerTask(TaskTypes::PRE_SHUTDOWN, function () use(&$bootstrapperObjects) { foreach ($bootstrapperObjects as $bootstrapper) { $this->container->call([$bootstrapper, "shutdown"], [], true); } }); }