Merge pull request #149 from zhamao-robot/feature/class-alias

添加类别名
This commit is contained in:
Jerry Ma 2022-08-22 15:38:49 +08:00 committed by GitHub
commit 53c1c0103c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 112 deletions

View File

@ -55,7 +55,8 @@
},
"files": [
"src/Globals/global_functions.php",
"src/Globals/global_defines_app.php"
"src/Globals/global_defines_app.php",
"src/Globals/global_class_alias.php"
]
},
"autoload-dev": {
@ -101,6 +102,6 @@
],
"analyse": "phpstan analyse --memory-limit 300M",
"cs-fix": "php-cs-fixer fix",
"test": "bin/phpunit-swoole --no-coverage"
"test": "bin/phpunit-zm --no-coverage"
}
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
class_alias(\ZM\Annotation\Framework\BindEvent::class, 'BindEvent');
class_alias(\ZM\Annotation\Framework\Init::class, 'Init');
class_alias(\ZM\Annotation\Framework\Setup::class, 'Setup');
class_alias(\ZM\Annotation\Http\Controller::class, 'Controller');
class_alias(\ZM\Annotation\Http\Route::class, 'Route');
class_alias(\ZM\Annotation\Middleware\Middleware::class, 'Middleware');
class_alias(\ZM\Annotation\OneBot\BotCommand::class, 'BotCommand');
class_alias(\ZM\Annotation\OneBot\BotEvent::class, 'BotEvent');
class_alias(\ZM\Annotation\OneBot\CommandArgument::class, 'CommandArgument');
class_alias(\ZM\Annotation\Closed::class, 'Closed');

View File

@ -11,10 +11,15 @@ use Traversable;
abstract class AnnotationBase implements IteratorAggregate
{
public $method = '';
public string $method = '';
/**
* @var Closure|string
*/
public $class = '';
public array $group = [];
public function __toString()
{
$str = __CLASS__ . ': ';
@ -44,7 +49,7 @@ abstract class AnnotationBase implements IteratorAggregate
*
* @param Closure|string $method
*/
public function withMethod($method): AnnotationBase
public function on($method): AnnotationBase
{
$this->method = $method;
return $this;
@ -54,4 +59,19 @@ abstract class AnnotationBase implements IteratorAggregate
{
return new ArrayIterator($this);
}
public function isInGroup(string $name): bool
{
return in_array($name, $this->group);
}
public function addGroup(string $name)
{
$this->group[] = $name;
}
public function getGroups(): array
{
return $this->group;
}
}

View File

@ -1,21 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Middleware;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class HandleAfter
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class HandleAfter extends AnnotationBase
{
}

View File

@ -1,21 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Middleware;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class HandleBefore
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class HandleBefore extends AnnotationBase
{
}

View File

@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Middleware;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use Exception;
use ZM\Annotation\AnnotationBase;
/**
* Class HandleException
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class HandleException extends AnnotationBase
{
/**
* @var string
*/
public $class_name = Exception::class;
public function __construct($class_name = Exception::class)
{
$this->class_name = $class_name;
}
}

View File

@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\Middleware;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* Class MiddlewareClass
* @Annotation
* @NamedArgumentConstructor()
* @Target("CLASS")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
class MiddlewareClass extends AnnotationBase
{
/**
* @var string
* @Required()
*/
public $name = '';
public function __construct($name)
{
$this->name = $name;
}
}

View File

@ -5,12 +5,18 @@ declare(strict_types=1);
namespace ZM;
use Exception;
use ZM\Command\Server\ServerStartCommand;
use ZM\Config\ZMConfig;
use ZM\Exception\InitException;
use ZM\Plugin\InstantPlugin;
class InstantApplication extends InstantPlugin
{
private static $obj;
/** @var null|InstantApplication 存储单例类的变量 */
private static ?InstantApplication $obj;
/** @var array 存储要传入的args */
private array $args = [];
/**
* @param null|mixed $dir
@ -22,7 +28,20 @@ class InstantApplication extends InstantPlugin
throw new InitException(zm_internal_errcode('E00069') . 'Initializing another Application is not allowed!');
}
self::$obj = $this; // 用于标记已经初始化完成
parent::__construct($dir ?? __DIR__);
parent::__construct($dir ?? (__DIR__ . '/../..'));
$this->args = ServerStartCommand::exportOptionArray();
}
public function withConfig(array $config): InstantApplication
{
// TODO: 完成patch config
return $this;
}
public function withArgs(array $args): InstantApplication
{
$this->args = ZMConfig::smartPatch($this->args, $args);
return $this;
}
/**
@ -30,6 +49,6 @@ class InstantApplication extends InstantPlugin
*/
public function run()
{
(new Framework())->init()->start();
(new Framework($this->args))->init()->start();
}
}