/** * Get adaptor * @return IAdaptor * @throws InvalidArgumentException */ public function adaptor() { if (DICService::get('db_adaptor') === null) { $manager = function () { if (!Config::exists('db')) { return new Sqlite(); } else { $adaptor_str = Config::get('db'); switch ($adaptor_str) { case 'sqlite': return new Sqlite(); case 'mysql': return new Mysql(); default: if (class_exists($adaptor_str)) { return new $adaptor_str(); } else { throw new InvalidArgumentException("DB adaptor `{$adaptor_str}` is not exists"); } } } }; DICService::set('db_adaptor', $manager); } return DICService::get('db_adaptor'); }
/** * Clear old counter * * @param integer $day * @return true */ public function clearOld($day = null) { if ($day === null) { if (Config::exists('counter', 'day')) { $day = Config::get('counter', 'day'); } else { $day = self::DEFAULT_DAY_SAVE; } } return $this->store()->clearOld($day); }
/** * Acquire key * * @param string $key * @param integer $max_acquire * @return boolean */ public function acquire($key, $max_acquire = 1) { if ($this->string()->length($key) === 0) { throw new \InvalidArgumentException('Key is not set'); } $key = $this->string()->toUpper($key); if ($this->dictionary->has($key)) { $entity = $this->dictionary->get($key); } else { $entity = new MutexEntity(); $entity->generate($key, $max_acquire); if (Config::exists('mutex', 'perm')) { $entity->permission = Config::get('mutex', 'perm'); } $this->dictionary->add($key, $entity); } return $this->adaptor()->acquire($entity); }
/** * Translate message * * @params string $message * @params array $params * @params string $lang * @params string $package * @params string $service * @return string */ protected function trans($message, array $params = [], $lang = null, $package = null, $service = null) { $class_array = explode('\\', get_called_class()); if ($package === null) { $package = $class_array[0]; } if ($service === null) { if (!isset($class_array[1])) { return $message; } else { $service = $class_array[1]; } } if ($lang === null) { if (Config::exists('lang')) { $lang = Config::get('lang'); } else { $lang = 'en'; } } return $this->translator()->trans($message, $params, $lang, $package, $service); }