Skip to content

Commit

Permalink
优化模板开发
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 13, 2024
1 parent 23a369f commit 5fc72b3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* 安装后配置文件位置: `/config/cms.php`
* 安装后模版主题位置: `/view/laket-cms/template`
* 安装后模版主题资源位置: `/public/static/laket-cms/template`
* 模板开发文档: [`Theme`](/docs/theme.md)
* 用户首页: `/cms`


Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"require": {
"php": ">=8.0",
"laket/laket-admin": "1.2.*"
"laket/laket-admin": "1.3.*"
},
"autoload": {
"psr-4": {
Expand All @@ -29,7 +29,7 @@
},
"laket" : {
"title": "CMS",
"version": "1.1.2",
"version": "1.1.3",
"adaptation": "1.3.*"
},
"extra": {
Expand Down
42 changes: 42 additions & 0 deletions docs/theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## 模板开发

模板开发包括本地模板开发和插件模板开发


1. 本地模板位置

```
/view/laket-cms/template
```

本地开发模板只需要修改对应的模板


2. 插件模板开发

```php
// 注入信息放到服务提供者 `boot()` 方法
public function boot()
{
// 判断主题并使用插件模板
add_filter('cms_theme_path', function($themePath, $theme) {
if ($theme == 'green') {
return root_path('view/cms-green/green');
}

return $themePath;
});

// 添加模板到主题列表
add_filter('cms_themes', function($themes) {
// 获取插件主题信息
$theme = Template::themeInfo(root_path('view/cms-green/green'));

$themes[] = $theme;

return $themes;
});
}
```

插件模板和本地模板信息和文件相同
71 changes: 43 additions & 28 deletions src/Service/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ public static function themePath($path = '')
$theme = SettingsModel::config('web_theme', 'default');

$themePath = static::path($theme);
$themePath = $themePath . ($path ? DIRECTORY_SEPARATOR . $path : $path);

return $themePath . ($path ? DIRECTORY_SEPARATOR . $path : $path);
// 主题路径过滤器
$themePath = apply_filters('cms_theme_path', $themePath, $theme);

return $themePath;
}

/**
Expand All @@ -51,40 +55,51 @@ public static function themes($path = null)

$newThemes = collect($themes)
->map(function($item) {
$infoFile = $item . '/info.php';
if (! empty($item) && file_exists($infoFile)) {
$info = include $infoFile;

$coverFile = $item . '/' . Arr::get($info, 'cover');
if (file_exists($coverFile)) {
$coverData = file_get_contents(realpath($coverFile));
$cover = "data:image/png;base64,".base64_encode($coverData);
} else {
$cover = "";
}

return [
'name' => Arr::get($info, 'name'),
'remark' => Arr::get($info, 'remark'),
'cover' => $cover,
'version' => Arr::get($info, 'version'),
'author' => Arr::get($info, 'author'),
];
}

return null;
return static::themeInfo($item);
})
->filter(function($item) {
if (! empty($item)) {
return $item;
}

return null;
return !empty($item);
})
->values()
->toArray();

// 所有主题过滤器
$newThemes = apply_filters('cms_themes', $newThemes);

return $newThemes;
}

/**
* 获取主题信息
*/
public static function themeInfo($path)
{
if (! empty($path) && file_exists($infoFile = $path . '/info.php')) {
$info = include $infoFile;

$coverFile = $path . '/' . Arr::get($info, 'cover');
if (file_exists($coverFile)) {
$coverData = file_get_contents(realpath($coverFile));
$cover = "data:image/png;base64,".base64_encode($coverData);
} else {
$cover = "";
}

$themeInfo = [
'name' => Arr::get($info, 'name'),
'remark' => Arr::get($info, 'remark'),
'cover' => $cover,
'version' => Arr::get($info, 'version'),
'author' => Arr::get($info, 'author'),
];

// 主题信息过滤器
$themeInfo = apply_filters('cms_theme_info', $themeInfo);

return $themeInfo;
}

return [];
}

}

0 comments on commit 5fc72b3

Please sign in to comment.