仿制封装Typecho_Http_Client类
4个月前 Typecho_Http_Client 类功能非常强大,它封装了 PHP 的 cURL 扩展,提供了简洁的 ...
好的,没问题!我已经帮你把整个插件系统整合好了,这是一个可以直接运行的完整项目代码包。
你可以按照下面的目录结构,把这些文件创建好,然后运行 index.php 就能看到效果。
项目目录结构
plaintext
your_project/
├── index.php # 主程序入口
├── PluginInterface.php # 插件接口定义
├── PluginManager.php # 插件管理器
└── plugins/ # 插件目录
├── HelloWorldPlugin/
│ ├── HelloWorldPlugin.php
│ └── plugin.json
└── ContentFilterPlugin/
├── ContentFilterPlugin.php
└── plugin.json
文件内容
<?php<br />
require_once 'PluginInterface.php';<br />
require_once 'PluginManager.php';<br />
<br />
// 初始化插件管理器<br />
$pluginManager = new PluginManager();<br />
<br />
// 加载 plugins 目录下的所有插件<br />
$pluginManager->loadPluginsFromDirectory('./plugins');<br />
<br />
echo "\n--- Main Application Start ---\n\n";<br />
<br />
// 模拟原始页面内容<br />
$pageContent = "<h1>Welcome to My Website</h1>";<br />
$pageContent .= "<p>This is a good article. It has some interesting points.</p>";<br />
<br />
echo "Original Page Content:\n";<br />
echo $pageContent . "\n\n";<br />
<br />
// 触发 'before_page_render' 钩子<br />
echo "Triggering 'before_page_render' hook...\n";<br />
$pluginManager->triggerHook('before_page_render');<br />
<br />
// 应用 'content_filter' 过滤器<br />
echo "Applying 'content_filter' filter...\n";<br />
$filteredContent = $pluginManager->applyFilters('content_filter', $pageContent);<br />
<br />
echo "\nFiltered Page Content:\n";<br />
echo $filteredContent;<br />
<br />
echo "\n\n--- Main Application End ---\n";<br />
<br />
```<br />
<br />
<br />
<?php<br />
interface PluginInterface {<br />
public function __construct($pluginManager, $config);<br />
public function initialize();<br />
public function getInfo();<br />
}<br />
<br />
```<br />
<br />
<br />
<?php<br />
class HelloWorldPlugin implements PluginInterface {<br />
private $pluginManager;<br />
private $config;<br />
<br />
public function __construct($pluginManager, $config) {<br />
$this->pluginManager = $pluginManager;<br />
$this->config = $config;<br />
}<br />
<br />
public function initialize() {<br />
$this->pluginManager->registerHook(<br />
'before_page_render', <br />
[$this, 'onBeforePageRender'], <br />
$this->config['priority']<br />
);<br />
}<br />
<br />
public function onBeforePageRender() {<br />
echo "[HelloWorldPlugin] Hook 'before_page_render' triggered! Priority: " . $this->config['priority'] . "\n";<br />
}<br />
<br />
public function getInfo() {<br />
return $this->config;<br />
}<br />
}<br />
<br />
```<br />
<br />
<br />
{<br />
"name": "Hello World Plugin",<br />
"version": "1.0",<br />
"description": "A simple hello world plugin that demonstrates hook usage.",<br />
"main": "HelloWorldPlugin.php",<br />
"enabled": true,<br />
"priority": 20<br />
}<br />
<br />
```<br />
<br />
<?php<br />
class ContentFilterPlugin implements PluginInterface {<br />
private $pluginManager;<br />
private $config;<br />
<br />
public function __construct($pluginManager, $config) {<br />
$this->pluginManager = $pluginManager;<br />
$this->config = $config;<br />
}<br />
<br />
public function initialize() {<br />
$this->pluginManager->registerFilter(<br />
'content_filter', <br />
[$this, 'filterContent'], <br />
$this->config['priority']<br />
);<br />
}<br />
<br />
public function filterContent($content) {<br />
echo "[ContentFilterPlugin] Filter 'content_filter' applied! Priority: " . $this->config['priority'] . "\n";<br />
<br />
// 将 'good' 替换为 'excellent'<br />
$content = str_replace('good', 'excellent', $content);<br />
<br />
// 添加版权信息<br />
$content .= "\n\n<div style='margin-top: 20px; padding: 10px; background-color: #f0f0f0;'>";<br />
$content .= "*This content was filtered by ContentFilterPlugin.*";<br />
$content .= "</div>";<br />
<br />
return $content;<br />
}<br />
<br />
public function getInfo() {<br />
return $this->config;<br />
}<br />
}<br />
<br />
```<br />
<br />
{<br />
"name": "Content Filter Plugin",<br />
"version": "1.0",<br />
"description": "Filters and modifies content using filters.",<br />
"main": "ContentFilterPlugin.php",<br />
"enabled": true,<br />
"priority": 10<br />
}<br />
<br />
```<br />
<br />
如何运行<br />
<br />
#免责声明#
本文为转载 或 原创内容,未经授权禁止转载、摘编、复制及镜像使用、转载请注明作者、出处及原文链接、违者将依法追究责任。

4个月前 Typecho_Http_Client 类功能非常强大,它封装了 PHP 的 cURL 扩展,提供了简洁的 ...

4个月前 实现 Cookie 隐私权限弹窗的核心是「底部固定显示 + 权限选择逻辑 + Cookie 状...

1年前 赚钱的方式多种多样,关键在于结合自身资源、技能和兴趣,选择适合自...

3个月前 用HTML构建广告与内容结构,CSS固定首屏全屏广告,JS用setInterval倒计时,结...

10天前 1. 我主页藏了不少同款好东西,就等有缘人发现了。 2. 喜欢这类内容�...

3个月前使用AI编写的一款php类文件自动加载,至于性能,效率,内存占用率怎么样那就不知道了