Theme.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace addons\cms\library;
  3. class Theme
  4. {
  5. private static $config = [];
  6. public static function get()
  7. {
  8. if (empty(self::$config)) {
  9. $config = (array)json_decode(file_get_contents(self::getConfigFile()), true);
  10. self::$config = $config;
  11. }
  12. return self::$config;
  13. }
  14. public static function getFirstParam($type='model',$index=0)
  15. {
  16. $config = self::get();
  17. $id = 0;
  18. if (isset($config['tabbar']['list']) && is_array($config['tabbar']['list'])) {
  19. if ($type == 'model') {
  20. preg_match("/(?<=model\=)\d$/", $config['tabbar']['list'][$index]['path'], $matches);
  21. if (count($matches) > 0) {
  22. $id = $matches[0];
  23. }
  24. } else {
  25. preg_match("/(?<=channel\=)\d$/", $config['tabbar']['list'][$index]['path'], $matches);
  26. if (count($matches) > 0) {
  27. $id = $matches[0];
  28. }
  29. }
  30. }
  31. return $id;
  32. }
  33. public static function render($config)
  34. {
  35. if (isset($config['tabbar']['list']) && is_array($config['tabbar']['list'])) {
  36. $url = url('/', '', false, true);
  37. $url = preg_replace("/\/([\w]+)\.php\//i", "/", $url);
  38. $url = rtrim($url, "/");
  39. foreach ($config['tabbar']['list'] as $index => &$item) {
  40. $item['image'] = preg_match("/^\/assets\/addons/", $item['image']) ? $url . $item['image'] : cdnurl($item['image'], true);
  41. $item['selectedImage'] = preg_match("/^\/assets\/addons/", $item['selectedImage']) ? $url . $item['selectedImage'] : cdnurl($item['selectedImage'], true);
  42. }
  43. }
  44. return $config;
  45. }
  46. public static function set($config, $overwrite = false)
  47. {
  48. self::$config = $overwrite ? $config : array_merge(self::$config, $config);
  49. file_put_contents(self::getConfigFile(), json_encode(self::$config, JSON_UNESCAPED_UNICODE));
  50. return self::$config;
  51. }
  52. public static function getConfigFile()
  53. {
  54. return ADDON_PATH . 'cms' . DS . 'data' . DS . 'theme.json';
  55. }
  56. }