wecenter学习笔记-插件机制

该文是wecenter学习笔记的一部分

插件机制

虽然wecenter有一个初级插件模型,但很遗憾并没有第三方基于这套机制去开发插件,连官方站点的挂出的插件也只是采用二次开发的方式,并不是基于插件开发的。

目前有哪些插件

项目中内置两个插件

  • aws_external
  • aws_offical_external

提供了几个utility函数

插件的结构

典型的插件结构如下所示:

每个插件一个为plugins里的一个目录,目录下的config.php为插件的元数据信息

$aws_plugin = array(
    'title' => 'External for Anwsion',  // 插件标题
    'version' => 20130107,  // 插件版本
    'description' => 'Anwsion 官网调用插件',  // 插件描述
    'requirements' => '20120706',   // 最低 Build 要求
    
    'contents' => array(
        // 对控制器构造部署代码 (setup)
        'setups' => array(
            array(
            'app' => 'app',
            'controller' => 'controller',
            'include' => 'aws_plungin_controller.php'
            )
        ),
    
        // 对控制器 Action 部署代码 (只支持模板输出之前)
        'actions' => array(
            array(
            'app' => 'app',
            'controller' => 'controller',
            'action' => 'action',
            'template' => 'template.tpl',
            'include' => 'aws_plungin_controller.php'
            )
        ),
        
        // 注册 Model, 用 $this->model('name') 访问
        'model' => array(
            'class_name' => 'aws_offical_external_class',   // Model name, 以 _class 结尾
            'include' => 'aws_offical_external.php',    // 引入代码文件位置
        ),
    ),
);

配置主要包括以下信息:

  • 初始化代码
  • action
  • model

插件的元数据加载

应用启动时加载插件管理器(core_plugins)

aws_app.inc.app#init

117 self::$plugins = load_class('core_plugins');

检索所有插件目录,并加载元数据

aws_app.inc.app#load_plugins()

$dir_handle = opendir($this->plugins_path);

while (($file = readdir($dir_handle)) !== false)
{
    if ($file != '.' AND $file != '..' AND is_dir($this->plugins_path . $file))
    {
        $config_file = $this->plugins_path . $file . '/config.php';

        if (file_exists($config_file))
        {
            $aws_plugin = false;

            require_once($config_file);

            if (is_array($aws_plugin) AND G_VERSION_BUILD >= $aws_plugin['requirements'])
            {
                if ($aws_plugin['contents']['model'])
                {
                    $this->plugins_model[$aws_plugin['contents']['model']['class_name']] = $this->plugins_path . $file . '/' . $aws_plugin['contents']['model']['include'];
                }

                if ($aws_plugin['contents']['setups'])
                {
                    foreach ($aws_plugin['contents']['setups'] AS $key => $data)
                    {
                        if ($data['app'] AND $data['controller'] AND $data['include'])
                        {
                            $this->plugins_table[$data['app']][$data['controller']]['setup'][] = array(
                                'file' => $this->plugins_path . $file . '/' . $data['include'],
                            );
                        }
                    }
                }

                if ($aws_plugin['contents']['actions'])
                {
                    foreach ($aws_plugin['contents']['actions'] AS $key => $data)
                    {
                        if ($data['app'] AND $data['controller'] AND $data['include'])
                        {
                            $this->plugins_table[$data['app']][$data['controller']][$data['action']][] = array(
                                'file' => $this->plugins_path . $file . '/' . $data['include'],
                                'template' => $data['template']
                            );
                        }
                    }
                }

                $this->plugins[$file] = $aws_plugin;
            }
        }
    }
}

closedir($dir_handle);

将元数据写入缓存

载入插件

Controller的初始化函数里会根据参数加载对应的插件

aws_controller.inc.php#__construct

// 载入插件
if ($plugins = AWS_APP::plugins()->parse($_GET['app'], $_GET['c'], 'setup'))
{
    foreach ($plugins as $plugin_file)
    {
        include $plugin_file;
    }
}

模版渲染时会唤起插件

system/class/cls_template.inc.php#output

if ($plugins = AWS_APP::plugins()->parse($_GET['app'], $_GET['c'], $_GET['act'], str_replace(self::$template_ext, '', $template_filename)))
{
    foreach ($plugins AS $plugin_file)
    {
        include_once $plugin_file;
    }
}

此外,定时任务也会唤起插件

app/crond/main.php#run_action

if ($plugins = AWS_APP::plugins()->parse('crond', 'main', $call_action))
{
    foreach ($plugins AS $plugin_file)
    {
        include($plugin_file);
    }
}

插件怎么使用

由于Autoload会处理Plugin里model类的加载,所有model类的使用方式并无二般。如:

$this->model('aws_external')->format_js_question_ul_output($_GET['ul_class'],$_GET['is_recommend']));

自动引入机制和Autoload ←o→ Cache的实现原理

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,156评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,126评论 25 709
  • 转载自cr180大神DiscuzX2.5完整目录结构【source程序文件库】 /source/admincp后台...
    cndaqiang阅读 902评论 1 2
  • 该文是wecenter学习笔记的一部分 自动引入机制和Autoload 通过约束model类、system类和co...
    imhaiyang阅读 559评论 0 1
  • 雨滴掉落在古巷 烟雨蒙蒙 小楼窗台的郁金香 慢慢消散在树荫里 那里有我曾经埋藏的秘密 我想你 想你 在往日破旧的电...
    W一个我阅读 340评论 2 12