Files
kiri-core/Annotation/Aspect.php
T

49 lines
904 B
PHP
Raw Normal View History

2021-03-29 02:34:31 +08:00
<?php
namespace Annotation;
2021-03-29 10:05:51 +08:00
use Exception;
2021-03-29 03:09:55 +08:00
use Snowflake\Aop;
use Snowflake\IAspect;
use Snowflake\Snowflake;
defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implement ');
2021-03-29 02:34:31 +08:00
/**
* Class Aspect
* @package Annotation
*/
2021-03-29 03:09:55 +08:00
#[\Attribute(\Attribute::TARGET_METHOD)] class Aspect extends Attribute
2021-03-29 02:34:31 +08:00
{
/**
* Aspect constructor.
2021-03-29 03:09:55 +08:00
* @param string $aspect
2021-03-29 02:34:31 +08:00
*/
2021-03-29 03:09:55 +08:00
public function __construct(public string $aspect)
2021-03-29 02:34:31 +08:00
{
}
2021-05-03 03:48:52 +08:00
public function execute(mixed $class, mixed $method = ''): mixed
2021-03-29 02:34:31 +08:00
{
2021-05-03 03:48:52 +08:00
// TODO: Change the autogenerated stub
if (!in_array(IAspect::class, class_implements($this->aspect))) {
2021-03-29 10:05:51 +08:00
throw new Exception(ASPECT_ERROR . IAspect::class);
2021-03-29 03:09:55 +08:00
}
/** @var Aop $aop */
$aop = Snowflake::app()->get('aop');
2021-05-03 03:48:52 +08:00
$aop->aop_add([$class, $method], $this->aspect);
2021-03-29 03:09:55 +08:00
2021-05-03 03:48:52 +08:00
return true;
2021-03-29 02:34:31 +08:00
}
}