Skip to content

Commit

Permalink
优化插件功能
Browse files Browse the repository at this point in the history
  • Loading branch information
weichaoduo committed Apr 9, 2020
1 parent 195165d commit 54faedf
Show file tree
Hide file tree
Showing 38 changed files with 813 additions and 796 deletions.
3 changes: 3 additions & 0 deletions app/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
// 项目程序模型文件所在根目录(文件系统)
define('MODEL_PATH', APP_PATH . 'model/');

// 插件目录
define('PLUGIN_PATH', APP_PATH . 'plugin/');

// 项目程序服务文件所在根目录(文件系统)
define('API_PATH', APP_PATH . 'api/');

Expand Down
4 changes: 2 additions & 2 deletions app/ctrl/BaseCtrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use main\app\classes\UserAuth;
use main\app\classes\UserLogic;
use main\app\model\DbModel;
use main\app\model\user\UserPostedFlagModel;
use main\app\model\SettingModel;
use main\app\model\system\AnnouncementModel;
use main\app\model\user\UserModel;
use main\app\protocol\Ajax;
use main\lib\MyPdo;

/**
* 控制器基类
Expand Down Expand Up @@ -223,7 +223,7 @@ public function render($tpl, $dataArr = [], $partial = false)
if ($tplEngine == 'php') {
require_once VIEW_PATH . $tpl;
if (!$partial && XPHP_DEBUG) {
$sqlLogs = MyPdo::$sqlLogs;
$sqlLogs = DbModel::$sqlLogs;
include_once VIEW_PATH . 'debug.php';
unset($sqlLogs);
}
Expand Down
3 changes: 3 additions & 0 deletions app/ctrl/OrgRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function pageIndex()
case 'issues':
$projectCtrlMain->pageIssues();
break;
case 'plugin':
$projectCtrlMain->pagePlugin();
break;
case 'summary':
$projectCtrlMain->pageHome();
break;
Expand Down
2 changes: 1 addition & 1 deletion app/ctrl/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function run()
* 执行SQL
*
* @param $sql
* @param \main\lib\MyPdo $db
* @param \main\app\model\DbModel $db
* @return bool
*/
private function runSql($sql, $db)
Expand Down
4 changes: 2 additions & 2 deletions app/ctrl/admin/DataBackup.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function pageIframeBackup()
mkdir($backupPath,0777);
}

$dbConfig = getCommonConfigVar('database');
$dbConfig = getYamlConfigByModule('database');
$dbConfig = $dbConfig['default'];

$time = -microtime(true);
Expand Down Expand Up @@ -68,7 +68,7 @@ public function pageIframeRecover($dump_file_name)
}


$dbConfig = getCommonConfigVar('database');
$dbConfig = getYamlConfigByModule('database');
$dbConfig = $dbConfig['default'];

$dumpFile = STORAGE_PATH .'dump_test.sql.gz';
Expand Down
2 changes: 1 addition & 1 deletion app/ctrl/admin/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function pageIndex()
$settingModel = new SettingModel();
$mysqlVersionStr = $settingModel->getFieldBySql($versionSql);

$dbConf = getCommonConfigVar('database');
$dbConf = getYamlConfigByModule('database');

$data['sys_domain'] = ROOT_URL;//ServerInfo::getDomain();
$data['sys_datetime'] = date('Y-m-d H:i:s', time());
Expand Down
2 changes: 1 addition & 1 deletion app/ctrl/issue/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ public function filter()

$userLogic = new UserLogic();
$users = $userLogic->getAllUser();
$emptyObj = new \stdClass();
foreach ($data['issues'] as &$issue) {
$issueId = $issue['id'];
IssueFilterLogic::formatIssue($issue);
Expand All @@ -672,7 +673,6 @@ public function filter()
$customValueArr = $customValuesIssueArr[$issueId];
$issue = array_merge($customValueArr, $issue);
}
$emptyObj = new \stdClass();
$issue['creator_info'] = isset($users[$issue['creator']])?$users[$issue['creator']]:$emptyObj;
$issue['modifier_info'] = isset($users[$issue['modifier']])?$users[$issue['modifier']]:$emptyObj;
$issue['reporter_info'] = isset($users[$issue['reporter']])?$users[$issue['reporter']]:$emptyObj;
Expand Down
18 changes: 18 additions & 0 deletions app/ctrl/project/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,24 @@ public function pageIssues()
$issueMainCtrl->pageIndex();
}

/**
* 跳转至事项页面
* @throws \Exception
*/
public function pagePlugin()
{
// 1.取出插件名称
$pluginName = $_GET['_target'][3];

// 2. 数据库查询插件配置信息

// 3. 实例化插件控制器和入口

// 渲染数据


}

/**
* backlog页面
* @throws \Exception
Expand Down
36 changes: 36 additions & 0 deletions app/event/Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php


namespace main\app\event;

/**
* 定义 Masterlab的事件
* @package main\app\event
*/
final class Events
{

/**
* Private constructor. This class cannot be instantiated.
*/
private function __construct()
{
}

public const postConnect = 'postConnect';

public const onIssueCreateBefore = 'onIssueCreateBefore';
public const onIssueCreateAfter = 'onIssueCreateAfter';


public const onSchemaCreateTableColumn = 'onSchemaCreateTableColumn';
public const onSchemaDropTable = 'onSchemaDropTable';
public const onSchemaAlterTable = 'onSchemaAlterTable';
public const onSchemaAlterTableAddColumn = 'onSchemaAlterTableAddColumn';
public const onSchemaAlterTableRemoveColumn = 'onSchemaAlterTableRemoveColumn';
public const onSchemaAlterTableChangeColumn = 'onSchemaAlterTableChangeColumn';
public const onSchemaAlterTableRenameColumn = 'onSchemaAlterTableRenameColumn';
public const onSchemaColumnDefinition = 'onSchemaColumnDefinition';
public const onSchemaIndexDefinition = 'onSchemaIndexDefinition';

}
48 changes: 48 additions & 0 deletions app/event/IssuePlacedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace main\app\event;

use main\app\ctrl\BaseUserCtrl;
use Symfony\Contracts\EventDispatcher\Event;
use main\app\model\issue\IssueModel;

/**
* 系统全局设置的各种配置属性
*
*/
class IssuePlacedEvent extends Event
{
const NAME = 'issue.placed';

/**
* @var BaseUserCtrl|null
*/
protected $ctrl = null;

/**
* @var IssueModel
*/
protected $issueModel;

public function __construct(BaseUserCtrl $ctrl, IssueModel $issueModel)
{
$this->ctrl = $ctrl;
$this->issueModel = $issueModel;
}

/**
* @return IssueModel
*/
public function getIssueModel()
{
return $this->issueModel;
}

/**
* @return BaseUserCtrl|null
*/
public function getCtrl()
{
return $this->ctrl;
}
}
31 changes: 23 additions & 8 deletions app/globals.php
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
<?php
/**
* 项目统一加载文件
*/
* 项目统一加载文件
*/


// 项目文件根目录(文件系统,即本文件所在目录)
define('APP_PATH', realpath(dirname(__FILE__)) . '/');
define('PRE_APP_PATH', realpath(dirname(__FILE__).'/../') . '/');
define('PRE_APP_PATH', realpath(dirname(__FILE__) . '/../') . '/');

// composer自动加载文件
require_once APP_PATH. '/../vendor/autoload.php';
require_once APP_PATH . '/../vendor/autoload.php';

// 加载自定义的函数库
include_once APP_PATH . 'function/autoload.php';

// 项目状态:deploy | development
$appStatus = "";
$cacheYamlConfig = false;
if (file_exists(PRE_APP_PATH . 'env.ini')) {
$envArr = parse_ini_file(PRE_APP_PATH . 'env.ini');
$appStatus = $envArr['APP_STATUS'];
$cacheYamlConfig = (boolean)$envArr['CACHE_YAML'];
unset($envArr);
}
if (isset($_SERVER['APP_STATUS'])) {
$appStatus = $_SERVER['APP_STATUS'];
}
define('APP_STATUS', $appStatus);

// 加载主配置文件 config.yml
// 加载主配置文件 config.yml @todo 使用yaml扩展函数将更高效
use Symfony\Component\Yaml\Yaml;
$GLOBALS['_yml_config'] = Yaml::parseFile(PRE_APP_PATH.'config.'.$appStatus.'.yml');
$cacheYamlConfigFile = APP_PATH . 'storage/cache/config.yaml.php';
if ($cacheYamlConfig && file_exists($cacheYamlConfigFile)) {
include $cacheYamlConfigFile;
if (isset($_yaml_config) && !empty($_yaml_config)) {
$GLOBALS['_yml_config'] = $_yaml_config;
unset($_yaml_config);
}
} else {
$GLOBALS['_yml_config'] = Yaml::parseFile(PRE_APP_PATH . 'config.' . $appStatus . '.yml');
if($cacheYamlConfig){
$cacheYamlConfigVar = "<?php \n".'$_yaml_config = '.var_export($GLOBALS['_yml_config'], true).";\n";
@file_put_contents($cacheYamlConfigFile, $cacheYamlConfigVar);
}
}


// 加载公共常量定义文件
include_once APP_PATH."constants.php";
include_once APP_PATH . "constants.php";

// 加载主配置文件
include_once APP_PATH."config/app.cfg.php";
include_once APP_PATH . "config/app.cfg.php";

6 changes: 6 additions & 0 deletions app/model/DbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class DbModel extends BaseModel
*/
public $sql = '';

/**
* 请求生命周期的sql数组
* @var array
*/
public static $sqlLogs = [];

/**
* 当前表的字段信息
* @var array
Expand Down
104 changes: 104 additions & 0 deletions app/plugin/PluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace main\plugin;

use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* 插件管理类
* Class PluginManager
*/
class PluginManager
{
/**
* 监听已注册的插件
*
* @access private
* @var array
*/
private $_listeners = array();


private $_plugins = array();


/**
* @var EventDispatcher
*/
public $dispatcher = null;

/**
* 构造函数
*
* @access public
* @return void
*/
public function __construct($ctrlObj, $plugins)
{
#这里$plugin数组包含我们获取已经由用户激活的插件信息
#$plugin = array(
# 'name' => '插件名称',
# 'directory'=>'插件安装目录'
#);
$this->dispatcher = new EventDispatcher();
if ($plugins) {
foreach ($plugins as $plugin) {
$pluginName = $plugin['name'];
$pluginFile = PRE_APP_PATH . 'plugin/' . $plugin['directory'] . '/' . $pluginName . '.php';
if (file_exists($pluginFile)) {
include_once($pluginFile);
if (class_exists($pluginName)) {
//初始化所有插件
$this->_plugins[$pluginName] = new $pluginName($ctrlObj, $this);
}
}
}
}
#此处做些日志记录方面的东西
}

/**
* 注册需要监听的插件方法(钩子)
*
* @param string $hook
* @param object $reference
* @param string $method
*/
function register($hook, &$reference, $method)
{
//获取插件要实现的方法
$key = get_class($reference) . '->' . $method;
//将插件的引用连同方法push进监听数组中
$this->_listeners[$hook][$key] = array(&$reference, $method);
#此处做些日志记录方面的东西
}

/**
* 触发一个钩子
*
* @param string $hook 钩子的名称
* @param mixed $data 钩子的入参
* @return mixed
*/
function trigger($hook, $data = null)
{
$result = '';
//查看要实现的钩子,是否在监听数组之中
if (isset($this->_listeners[$hook]) && is_array($this->_listeners[$hook]) && count($this->_listeners[$hook]) > 0) {
// 循环调用开始
foreach ($this->_listeners[$hook] as $listener) {
// 取出插件对象的引用和方法
$class =& $listener[0];
$method = $listener[1];
if (method_exists($class, $method)) {
// 动态调用插件的方法
$result .= $class->$method($data);
}
}
}
#此处做些日志记录方面的东西
return $result;
}
}
Loading

0 comments on commit 54faedf

Please sign in to comment.