diff --git a/usr/plugins/HelloWorld/Plugin.php b/usr/plugins/HelloWorld/Plugin.php index 44a7d69d..66d8d7dd 100644 --- a/usr/plugins/HelloWorld/Plugin.php +++ b/usr/plugins/HelloWorld/Plugin.php @@ -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'; } /** diff --git a/var/Typecho/Common.php b/var/Typecho/Common.php index e39d1746..45c930ce 100644 --- a/var/Typecho/Common.php +++ b/var/Typecho/Common.php @@ -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; diff --git a/var/Typecho/Plugin.php b/var/Typecho/Plugin.php index 2915c4f0..0f44b71b 100644 --- a/var/Typecho/Plugin.php +++ b/var/Typecho/Plugin.php @@ -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;