$addons_path
$addons_path :
插件基类 Class Addons
<?php
namespace think;
use think\Config;
use think\View;
/**
* æ’件基类
* Class Addons
* @author Byron Sampson <xiaobo.sun@qq.com>
* @package think\addons
*/
abstract class Addons
{
// 视图实例对象
protected $view = null;
// 当å‰é”™è¯¯ä¿¡æ¯
protected $error;
// æ’件目录
public $addons_path = '';
// æ’ä»¶é…置作用域
protected $configRange = 'addonconfig';
// æ’ä»¶ä¿¡æ¯ä½œç”¨åŸŸ
protected $infoRange = 'addoninfo';
/**
* 架构函数
* @access public
*/
public function __construct()
{
$name = $this->getName();
// 获å–当剿’件目录
$this->addons_path = ADDON_PATH . $name . DS;
// åˆå§‹åŒ–视图模型
$config = ['view_path' => $this->addons_path];
$config = array_merge(Config::get('template'), $config);
$this->view = new View($config, Config::get('view_replace_str'));
// 控制器åˆå§‹åŒ–
if (method_exists($this, '_initialize')) {
$this->_initialize();
}
}
/**
* 读å–基础é…置信æ¯
* @param string $name
* @return array
*/
final public function getInfo($name = '')
{
if (empty($name)) {
$name = $this->getName();
}
if (Config::has($name, $this->infoRange)) {
return Config::get($name, $this->infoRange);
}
$info_file = $this->addons_path . 'info.ini';
if (is_file($info_file)) {
$info = Config::parse($info_file, '', $name, $this->infoRange);
$info['url'] = addon_url($name);
}
Config::set($name, $info, $this->infoRange);
return $info;
}
/**
* èŽ·å–æ’ä»¶çš„é…置数组
* @param string $name å¯é€‰æ¨¡å—å
* @return array
*/
final public function getConfig($name = '')
{
if (empty($name)) {
$name = $this->getName();
}
if (Config::has($name, $this->configRange)) {
return Config::get($name, $this->configRange);
}
$config = [];
$config_file = $this->addons_path . 'config.php';
if (is_file($config_file)) {
$temp_arr = include $config_file;
foreach ($temp_arr as $key => $value) {
$config[$value['name']] = $value['value'];
}
unset($temp_arr);
}
Config::set($name, $config, $this->configRange);
return $config;
}
/**
* 设置é…置数æ®
* @param $name
* @param array $value
* @return array
*/
final public function setConfig($name = '', $value = [])
{
if (empty($name)) {
$name = $this->getName();
}
$config = $this->getConfig($name);
$config = array_merge($config, $value);
Config::set($name, $config, $this->configRange);
return $config;
}
/**
* 设置æ’ä»¶ä¿¡æ¯æ•°æ®
* @param $name
* @param array $value
* @return array
*/
final public function setInfo($name = '', $value = [])
{
if (empty($name)) {
$name = $this->getName();
}
$info = $this->getInfo($name);
$info = array_merge($info, $value);
Config::set($name, $info, $this->infoRange);
return $info;
}
/**
* 获å–完整é…置列表
* @param string $name
* @return array
*/
final public function getFullConfig($name = '')
{
$fullConfigArr = [];
if (empty($name)) {
$name = $this->getName();
}
$config_file = $this->addons_path . 'config.php';
if (is_file($config_file)) {
$fullConfigArr = include $config_file;
}
return $fullConfigArr;
}
/**
* 获å–当剿¨¡å—å
* @return string
*/
final public function getName()
{
$data = explode('\\', get_class($this));
return strtolower(array_pop($data));
}
/**
* 检查基础é…ç½®ä¿¡æ¯æ˜¯å¦å®Œæ•´
* @return bool
*/
final public function checkInfo()
{
$info = $this->getInfo();
$info_check_keys = ['name', 'title', 'intro', 'author', 'version', 'state'];
foreach ($info_check_keys as $value) {
if (!array_key_exists($value, $info)) {
return false;
}
}
return true;
}
/**
* åŠ è½½æ¨¡æ¿å’Œé¡µé¢è¾“出 å¯ä»¥è¿”回输出内容
* @access public
* @param string $template æ¨¡æ¿æ–‡ä»¶å或者内容
* @param array $vars 模æ¿è¾“出å˜é‡
* @param array $replace 替æ¢å†…容
* @param array $config 模æ¿å‚æ•°
* @return mixed
* @throws \Exception
*/
public function fetch($template = '', $vars = [], $replace = [], $config = [])
{
if (!is_file($template)) {
$template = '/' . $template;
}
// 关闿¨¡æ¿å¸ƒå±€
$this->view->engine->layout(false);
echo $this->view->fetch($template, $vars, $replace, $config);
}
/**
* 渲染内容输出
* @access public
* @param string $content 内容
* @param array $vars 模æ¿è¾“出å˜é‡
* @param array $replace 替æ¢å†…容
* @param array $config 模æ¿å‚æ•°
* @return mixed
*/
public function display($content, $vars = [], $replace = [], $config = [])
{
// 关闿¨¡æ¿å¸ƒå±€
$this->view->engine->layout(false);
echo $this->view->display($content, $vars, $replace, $config);
}
/**
* 渲染内容输出
* @access public
* @param string $content 内容
* @param array $vars 模æ¿è¾“出å˜é‡
* @return mixed
*/
public function show($content, $vars = [])
{
// 关闿¨¡æ¿å¸ƒå±€
$this->view->engine->layout(false);
echo $this->view->fetch($content, $vars, [], [], true);
}
/**
* 模æ¿å˜é‡èµ‹å€¼
* @access protected
* @param mixed $name è¦æ˜¾ç¤ºçš„æ¨¡æ¿å˜é‡
* @param mixed $value å˜é‡çš„值
* @return void
*/
public function assign($name, $value = '')
{
$this->view->assign($name, $value);
}
/**
* 获å–当å‰é”™è¯¯ä¿¡æ¯
* @return mixed
*/
public function getError()
{
return $this->error;
}
//必须实现安装
abstract public function install();
//å¿…é¡»å¸è½½æ’件方法
abstract public function uninstall();
}