该文是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']));