Skip to content

Commit

Permalink
Use TypechoPlugin\ for plugin namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Sep 16, 2021
1 parent ec495d7 commit fc9aaf6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions usr/plugins/HelloWorld/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace HelloWorld;
namespace TypechoPlugin\HelloWorld;

use Typecho\Plugin\PluginInterface;
use Typecho\Widget\Helper\Form;
Expand All @@ -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';
}

/**
Expand Down
36 changes: 27 additions & 9 deletions var/Typecho/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,42 @@ function _n(string $single, string $plural, int $number): string
}

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;

$alias = empty($alias) ? Common::nativeClassName($className) : $alias;
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__);
}

$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);

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);
$path = str_replace('\\', '/', $alias);
}
} else {
$path = $className;
}
Expand All @@ -104,13 +121,14 @@ function _n(string $single, string $plural, int $number): string
|| 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;
Expand Down
8 changes: 4 additions & 4 deletions var/Typecho/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ public static function portal(string $pluginName, string $path): array
{
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);
Expand Down Expand Up @@ -388,9 +388,9 @@ public function __get(string $component)
* 设置回调函数
*
* @param string $component 当前组件
* @param mixed $value 回调函数
* @param callable $value 回调函数
*/
public function __set(string $component, $value)
public function __set(string $component, callable $value)
{
$weight = 0;

Expand Down

0 comments on commit fc9aaf6

Please sign in to comment.