Config.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace Entity;
  3. class Config extends ModelBase
  4. {
  5. private static $config = array();
  6. protected function install()
  7. {
  8. $dbPrefix = $this->getDbPrefix();
  9. $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}config` (
  10. `lang` VARCHAR(8) NULL,
  11. `key` VARCHAR(64) NOT NULL,
  12. `value` TEXT NULL,
  13. UNIQUE(`lang`, `key`)
  14. )");
  15. if ($result === false)
  16. throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
  17. return true;
  18. }
  19. public static function getConfig($lang =null, $key =null, $defaultValue =null)
  20. {
  21. $fetcher = new self();
  22. if (isset(self::$config[$lang]))
  23. return;
  24. $values = $fetcher->selects(array("lang" => $lang));
  25. foreach ($values as $i)
  26. self::$config[$lang][$i->key] = $i->value;
  27. if ($key)
  28. return (isset(self::$config[$lang][$key]) ? self::$config[$lang][$key] : $defaultValue);
  29. return $defaultValue;
  30. }
  31. }