/** * 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); }
/** * Load from yaml file * * @param string $file * @return boolean * @throws \RuntimeException */ public static function loadFromYamlFile($file = null) { if ($file === null) { $yaml = Config::get('schema'); } else { if (!file_exists($file)) { throw new \RuntimeException("file `{$file}` is not found"); } $yaml = Yaml::parse(file_get_contents($file)); } if (!empty($yaml)) { $self = new static(); return $self->load($yaml); } return false; }
/** * 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); }
/** * Run widget */ public function run() { $site = $this->getSite(); $data = Config::get('sites', $site); $entity = new SiteEntity(); $post = $this->request()->post()->get('FORM'); if ($post !== null) { $post = $this->trim($post); if ($this->checkFields($post, $entity)) { if ($entity->checkFields()) { $save = []; foreach ($entity->getData() as $key => $value) { $save[$this->string()->toLower($key)] = $value; } $data = array_replace($data, $save); $yml = Config::all(); $yml['sites'][$site] = $data; file_put_contents($this->getRealPath($this->path_to_config), Yaml::dump($yml)); $this->setFlash($this->trans('mvc.widgets.admin_settings.success')); $this->redirect($this->getCurPageParam([], ['post'])); } } } else { $entity->setData($data); } $flash = $this->getFlash(); $this->render('admin/mvc/settings_form', compact('entity', 'flash', 'post')); }