Use TypechoPlugin\ for plugin namespace

This commit is contained in:
joyqi 2021-09-16 17:53:59 +08:00
parent ec495d7e24
commit fc9aaf66f3
3 changed files with 34 additions and 16 deletions

View File

@ -1,6 +1,6 @@
<?php
namespace HelloWorld;
namespace TypechoPlugin\HelloWorld;
use Typecho\Plugin\PluginInterface;
use Typecho\Widget\Helper\Form;
@ -26,7 +26,7 @@ class Plugin implements PluginInterface
*/
public static function activate()
{
\Typecho\Plugin::factory('admin/menu.php')->navBar = ['HelloWorld_Plugin', 'render'];
\Typecho\Plugin::factory('admin/menu.php')->navBar = __CLASS__ . '::render';
}
/**

View File

@ -74,25 +74,42 @@ namespace {
}
namespace Typecho {
const PLUGIN_NAMESPACE = 'TypechoPlugin';
spl_autoload_register(function (string $className) {
$isDefinedAlias = defined('__TYPECHO_CLASS_ALIASES__');
$isNamespace = strpos($className, '\\') !== false;
$isAlias = $isDefinedAlias && isset(__TYPECHO_CLASS_ALIASES__[$className]);
$isPlugin = false;
// detect if class is predefined
if (strpos($className, '\\') !== false) {
if ($isDefinedAlias) {
$alias = array_search('\\' . ltrim($className, '\\'), __TYPECHO_CLASS_ALIASES__);
$isPlugin = strpos(ltrim($className, '\\'), PLUGIN_NAMESPACE . '\\') !== false;
if ($isPlugin) {
$realClassName = substr($className, strlen(PLUGIN_NAMESPACE) + 1);
$alias = Common::nativeClassName($realClassName);
$path = str_replace('\\', '/', $realClassName);
} else {
if ($isDefinedAlias) {
$alias = array_search('\\' . ltrim($className, '\\'), __TYPECHO_CLASS_ALIASES__);
}
$alias = empty($alias) ? Common::nativeClassName($className) : $alias;
$path = str_replace('\\', '/', $className);
}
$alias = empty($alias) ? Common::nativeClassName($className) : $alias;
$path = str_replace('\\', '/', $className);
} elseif (strpos($className, '_') !== false || $isAlias) {
$alias = $isAlias ? __TYPECHO_CLASS_ALIASES__[$className]
: '\\' . str_replace('_', '\\', $className);
$isPlugin = !$isAlias && !preg_match("/^(Typecho|Widget|IXR)_/", $className);
$path = str_replace('\\', '/', $alias);
if ($isPlugin) {
$alias = '\\TypechoPlugin\\' . str_replace('_', '\\', $className);
$path = str_replace('_', '/', $className);
} else {
$alias = $isAlias ? __TYPECHO_CLASS_ALIASES__[$className]
: '\\' . str_replace('_', '\\', $className);
$path = str_replace('\\', '/', $alias);
}
} else {
$path = $className;
}
@ -104,13 +121,14 @@ namespace Typecho {
|| trait_exists($alias, false))
) {
class_alias($alias, $className, false);
return;
}
// load class file
$path .= '.php';
$defaultFile = __TYPECHO_ROOT_DIR__ . '/var/' . $path;
if (file_exists($defaultFile)) {
if (file_exists($defaultFile) && !$isPlugin) {
include_once $defaultFile;
} else {
$pluginFile = __TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . '/' . $path;

View File

@ -320,10 +320,10 @@ class Plugin
{
switch (true) {
case file_exists($pluginFileName = $path . '/' . $pluginName . '/Plugin.php'):
$className = "{$pluginName}_Plugin";
$className = "\\" . PLUGIN_NAMESPACE . "\\{$pluginName}\\Plugin";
break;
case file_exists($pluginFileName = $path . '/' . $pluginName . '.php'):
$className = $pluginName;
$className = "\\" . PLUGIN_NAMESPACE . "\\" . $pluginName;
break;
default:
throw new PluginException('Missing Plugin ' . $pluginName, 404);
@ -388,9 +388,9 @@ class Plugin
* 设置回调函数
*
* @param string $component 当前组件
* @param mixed $value 回调函数
* @param callable $value 回调函数
*/
public function __set(string $component, $value)
public function __set(string $component, callable $value)
{
$weight = 0;