Files
kiri-core/system/Annotation/Annotation.php
T

267 lines
5.2 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
namespace Snowflake\Annotation;
use Exception;
2020-09-01 03:44:49 +08:00
use HttpServer\Route\Annotation\Websocket;
2020-08-31 01:27:08 +08:00
use ReflectionClass;
use ReflectionException;
2020-09-01 03:11:34 +08:00
use ReflectionMethod;
2020-08-31 01:27:08 +08:00
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
2020-08-31 22:33:50 +08:00
use validator\RequiredValidator;
use validator\RequiredValidator as NotEmptyValidator;
2020-08-31 01:27:08 +08:00
/**
* Class Annotation
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Annotation
2020-08-31 01:27:08 +08:00
*/
class Annotation extends BaseAnnotation
{
public $namespace = '';
public $prefix = '';
public $path = '';
2020-09-01 03:11:34 +08:00
protected $_Scan_directory = [];
protected $_alias = [];
2020-08-31 22:33:50 +08:00
2020-09-01 03:11:34 +08:00
protected $params = [];
2020-08-31 22:33:50 +08:00
2020-08-31 01:27:08 +08:00
2020-09-01 03:11:34 +08:00
private $_classMap = [];
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @param $class
2020-09-01 03:46:17 +08:00
* @return mixed
2020-09-01 03:11:34 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
public function register($name, $class)
{
2020-09-01 03:46:17 +08:00
if (!isset($this->_classMap[$name]) || is_string($this->_classMap[$name])) {
2020-09-01 03:44:49 +08:00
$this->_classMap[$name] = Snowflake::createObject($class);
}
return $this->_classMap[$name];
2020-08-31 01:27:08 +08:00
}
/**
2020-08-31 22:33:50 +08:00
* @param $path
* @param $namespace
2020-08-31 01:27:08 +08:00
* @throws ReflectionException
*/
2020-09-01 03:11:34 +08:00
public function registration_notes($path, $namespace)
2020-08-31 01:27:08 +08:00
{
2020-09-01 03:11:34 +08:00
$this->scanning(rtrim($path, '/'), $namespace, get_called_class());
2020-08-31 22:33:50 +08:00
}
/**
* @param ReflectionClass $reflect
2020-08-31 23:18:50 +08:00
* @param string $className
* @throws ReflectionException
2020-08-31 22:33:50 +08:00
* @throws Exception
2020-08-31 23:18:50 +08:00
* @Message(updatePosition)
2020-09-01 03:11:34 +08:00
* 注入注解
2020-08-31 22:33:50 +08:00
*/
2020-09-01 03:11:34 +08:00
private function resolve(ReflectionClass $reflect, string $className)
2020-08-31 22:33:50 +08:00
{
$controller = $reflect->newInstance();
2020-08-31 01:27:08 +08:00
2020-08-31 23:18:50 +08:00
$annotations = $this->getAnnotation($className);
$methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
2020-08-31 22:33:50 +08:00
foreach ($methods as $function) {
2020-08-31 23:18:50 +08:00
foreach ($annotations as $annotation) {
$comment = $function->getDocComment();
$methodName = $function->getName();
preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events);
if (!isset($events[1])) {
continue;
}
if (!$this->isLegitimate($events)) {
continue;
}
2020-09-01 03:47:13 +08:00
$this->push($this->getName($annotation, $events), [$controller, $methodName]);
2020-08-31 22:33:50 +08:00
}
2020-08-31 01:27:08 +08:00
}
}
2020-09-01 03:11:34 +08:00
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function get($name)
{
2020-09-01 03:19:40 +08:00
if (!isset($this->_classMap[$name])) {
2020-09-01 03:12:40 +08:00
throw new Exception('Undefined analytic class ' . $name . '.');
2020-09-01 03:11:34 +08:00
}
2020-09-01 03:19:40 +08:00
return $this->_classMap[$name];
2020-09-01 03:11:34 +08:00
}
2020-08-31 01:27:08 +08:00
/**
2020-08-31 22:33:50 +08:00
* @param $events
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-08-31 22:33:50 +08:00
public function isLegitimate($events)
2020-08-31 01:27:08 +08:00
{
2020-08-31 23:32:25 +08:00
throw new Exception('Undefined analytic function isLegitimate.');
2020-08-31 01:27:08 +08:00
}
2020-08-31 22:33:50 +08:00
/**
* @param $function
* @param $events
* @throws Exception
*/
public function getName($function, $events)
{
2020-08-31 23:32:25 +08:00
throw new Exception('Undefined analytic function getName.');
2020-08-31 22:33:50 +08:00
}
/**
* @param $controller
* @param $methodName
* @param $events
* @throws Exception
*/
public function createHandler($controller, $methodName, $events)
{
throw new Exception('Undefined analytic function.');
}
/**
* @param string $path
* @param $namespace
2020-08-31 23:18:50 +08:00
* @param $className
2020-08-31 22:33:50 +08:00
* @throws ReflectionException
*/
2020-08-31 23:18:50 +08:00
protected function scanning($path, $namespace, $className)
2020-08-31 22:33:50 +08:00
{
$di = Snowflake::getDi();
foreach (glob($path . '/*') as $file) {
if (is_dir($file)) {
2020-08-31 23:18:50 +08:00
$this->scanning($path, $namespace, $className);
2020-08-31 22:33:50 +08:00
}
$explode = explode('/', $file);
$class = str_replace('.php', '', end($explode));
2020-08-31 23:18:50 +08:00
$this->resolve($di->getReflect($namespace . '\\' . $class), $className);
2020-08-31 22:33:50 +08:00
}
}
2020-08-31 01:27:08 +08:00
/**
* @param $path
2020-09-01 03:11:34 +08:00
* @param array $params
2020-08-31 01:27:08 +08:00
* @return bool|mixed
*/
2020-09-01 03:11:34 +08:00
public function runWith($path, $params = [])
2020-08-31 01:27:08 +08:00
{
if (!$this->has($path)) {
return null;
}
2020-08-31 22:33:50 +08:00
$callback = $this->_Scan_directory[$path];
if (!isset($this->params[$path])) {
2020-09-01 03:11:34 +08:00
return $callback(...$params);
2020-08-31 22:33:50 +08:00
}
return $callback(...$this->params[$path]);
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
* @param $callback
2020-08-31 22:33:50 +08:00
* @param array $params
2020-08-31 01:27:08 +08:00
*/
2020-08-31 22:33:50 +08:00
public function push($name, $callback, $params = [])
2020-08-31 01:27:08 +08:00
{
$this->_Scan_directory[$name] = $callback;
2020-08-31 22:33:50 +08:00
if (!empty($params)) {
$this->params[$name] = $params;
}
2020-08-31 01:27:08 +08:00
}
2020-09-01 03:11:34 +08:00
/**
* @param $name
* @return array|null[]
*/
public function pop($name)
{
2020-09-01 03:48:18 +08:00
if (isset($this->_Scan_directory[$name])) {
2020-09-01 03:11:34 +08:00
return [$this->_Scan_directory[$name], $this->params[$name] ?? []];
}
return [null, null];
}
2020-08-31 01:27:08 +08:00
/**
* @param $path
* @return bool|mixed
*/
public function has($path)
{
return isset($this->_Scan_directory[$path]);
}
/**
* @param $name
* @return mixed|null
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public function __get($name)
{
2020-09-01 00:17:19 +08:00
if (!isset($this->_classMap[$name])) {
return parent::__get($name); // TODO: Change the autogenerated stub
2020-08-31 01:27:08 +08:00
}
2020-09-01 00:17:19 +08:00
if (!is_object($this->_classMap[$name])) {
$this->_classMap[$name] = Snowflake::createObject($this->_classMap[$name]);
}
return $this->_classMap[$name];
2020-08-31 01:27:08 +08:00
}
2020-09-01 03:11:34 +08:00
/**
* @param ReflectionClass $reflect
* @return array
*/
protected function getPrivates(ReflectionClass $reflect)
{
$arrays = [];
$properties = $reflect->getProperties(ReflectionMethod::IS_PRIVATE);
foreach ($properties as $property) {
$arrays[] = $property->getName();
}
return $arrays;
}
/**
* @param string $class
* @return string[]
* @throws ReflectionException
*/
public function getAnnotation(string $class)
{
$reflect = Snowflake::getDi()->getReflect($class);
return $this->getPrivates($reflect);
}
2020-08-31 01:27:08 +08:00
}