/** * @param string $key * @param string $instanceOf * @param callable|null $notFound * @return mixed|null */ public function find(string $key, string $instanceOf, callable $notFound = null) { // Check in runtime memory if ($this->repo->has($key)) { $pull = $this->repo->pull($key); if (is_object($pull) && is_a($pull, $instanceOf)) { return $pull; } } // Check in cache if (isset($this->cache)) { $cached = $this->cache->get($key); if (is_object($cached) && is_a($cached, $instanceOf)) { return $cached; } } if (is_callable($notFound)) { $callBack = call_user_func($notFound); if (is_object($callBack)) { $this->set($key, clone $callBack); return $callBack; } } return null; }
/** * Retrieve an instance of service from container * * @param string $key * @param array ...$args * @return mixed * @throws ContainerException * @throws Exception\RepositoryException */ public function get(string $key, ...$args) { // Check if service exists if (!$this->has($key)) { throw ContainerException::serviceNotFound(__METHOD__, $key); } // Check if its in Repository if ($this->repo->has($key)) { // Pull instance from repository return $this->repo->pull($key); } else { // Get new instance of service class $service = $this->services[$key]; return $service->createInstance($this, $args); } }
/** * Register Translator (i18n) Component * @param string $lang * @return Translator\Language */ protected function getCachedLanguage(string $lang) : Translator\Language { // Get cache Disk instance $cache = $this->disks->pull("cache"); // Cached language file $langFile = sprintf("bootstrap.lang_%s.php.cache", strtolower($lang)); try { $lang = unserialize($cache->read($langFile)); } catch (DiskException $e) { } // Got language? if (!isset($lang) || !$lang instanceof Translator\Language) { $lang = $this->translator->language($lang); // Write to cache $cache->write($langFile, serialize($lang), Disk::WRITE_FLOCK); } // Return Language instance return $lang; }