<?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';
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();
}
}
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;
}
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;
}
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;
}
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;
}
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;
}
final public function getName()
{
$data = explode('\\', get_class($this));
return strtolower(array_pop($data));
}
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;
}
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);
}
public function display($content, $vars = [], $replace = [], $config = [])
{
$this->view->engine->layout(false);
echo $this->view->display($content, $vars, $replace, $config);
}
public function show($content, $vars = [])
{
$this->view->engine->layout(false);
echo $this->view->fetch($content, $vars, [], [], true);
}
public function assign($name, $value = '')
{
$this->view->assign($name, $value);
}
public function getError()
{
return $this->error;
}
abstract public function install();
abstract public function uninstall();
}