好的,没问题!我已经帮你把整个插件系统整合好了,这是一个可以直接运行的完整项目代码包。

你可以按照下面的目录结构,把这些文件创建好,然后运行  index.php  就能看到效果。

项目目录结构

plaintext

your_project/
├── index.php # 主程序入口
├── PluginInterface.php # 插件接口定义
├── PluginManager.php # 插件管理器
└── plugins/ # 插件目录

├── HelloWorldPlugin/
│   ├── HelloWorldPlugin.php
│   └── plugin.json
└── ContentFilterPlugin/
    ├── ContentFilterPlugin.php
    └── plugin.json

 

文件内容

  1.  index.php  (主程序入口)
<?php
require_once 'PluginInterface.php';
require_once 'PluginManager.php';

// 初始化插件管理器
$pluginManager = new PluginManager();

// 加载 plugins 目录下的所有插件
$pluginManager->loadPluginsFromDirectory('./plugins');

echo "\n--- Main Application Start ---\n\n";

// 模拟原始页面内容
$pageContent = "<h1>Welcome to My Website</h1>";
$pageContent .= "<p>This is a good article. It has some interesting points.</p>";

echo "Original Page Content:\n";
echo $pageContent . "\n\n";

// 触发 'before_page_render' 钩子
echo "Triggering 'before_page_render' hook...\n";
$pluginManager->triggerHook('before_page_render');

// 应用 'content_filter' 过滤器
echo "Applying 'content_filter' filter...\n";
$filteredContent = $pluginManager->applyFilters('content_filter', $pageContent);

echo "\nFiltered Page Content:\n";
echo $filteredContent;

echo "\n\n--- Main Application End ---\n";

 

  1.  PluginInterface.php  (插件接口)
<?php
interface PluginInterface {
    public function __construct($pluginManager, $config);
    public function initialize();
    public function getInfo();
}

 

  1.  PluginManager.php  (插件管理器)

此处内容需要评论 回复 后方可阅读。

  1.  plugins/HelloWorldPlugin/HelloWorldPlugin.php 
<?php
class HelloWorldPlugin implements PluginInterface {
    private $pluginManager;
    private $config;

    public function __construct($pluginManager, $config) {
        $this->pluginManager = $pluginManager;
        $this->config = $config;
    }

    public function initialize() {
        $this->pluginManager->registerHook(
            'before_page_render', 
            [$this, 'onBeforePageRender'], 
            $this->config['priority']
        );
    }

    public function onBeforePageRender() {
        echo "[HelloWorldPlugin] Hook 'before_page_render' triggered! Priority: " . $this->config['priority'] . "\n";
    }

    public function getInfo() {
        return $this->config;
    }
}

 

  1.  plugins/HelloWorldPlugin/plugin.json 
{
    "name": "Hello World Plugin",
    "version": "1.0",
    "description": "A simple hello world plugin that demonstrates hook usage.",
    "main": "HelloWorldPlugin.php",
    "enabled": true,
    "priority": 20
}

 

  1.  plugins/ContentFilterPlugin/ContentFilterPlugin.php 
<?php
class ContentFilterPlugin implements PluginInterface {
    private $pluginManager;
    private $config;

    public function __construct($pluginManager, $config) {
        $this->pluginManager = $pluginManager;
        $this->config = $config;
    }

    public function initialize() {
        $this->pluginManager->registerFilter(
            'content_filter', 
            [$this, 'filterContent'], 
            $this->config['priority']
        );
    }

    public function filterContent($content) {
        echo "[ContentFilterPlugin] Filter 'content_filter' applied! Priority: " . $this->config['priority'] . "\n";
        
        // 将 'good' 替换为 'excellent'
        $content = str_replace('good', 'excellent', $content);
        
        // 添加版权信息
        $content .= "\n\n<div style='margin-top: 20px; padding: 10px; background-color: #f0f0f0;'>";
        $content .= "*This content was filtered by ContentFilterPlugin.*";
        $content .= "</div>";
        
        return $content;
    }

    public function getInfo() {
        return $this->config;
    }
}

 

  1.  plugins/ContentFilterPlugin/plugin.json 
{
    "name": "Content Filter Plugin",
    "version": "1.0",
    "description": "Filters and modifies content using filters.",
    "main": "ContentFilterPlugin.php",
    "enabled": true,
    "priority": 10
}

如何运行

1. 按照上面的结构创建所有文件
2. 在命令行进入项目目录,执行  php index.php 
3. 或者将项目放到 Web 服务器目录下,通过浏览器访问  index.php 

你将看到插件被自动加载,并按照优先级执行钩子和过滤器。

这个系统现在支持:

  • 插件的自动发现和加载
  • 通过配置文件管理插件状态和优先级
  • 钩子(Hook)用于事件通知
  • 过滤器(Filter)用于数据处理