Files
kiri-core/system/Annotation/Annotation.php
T
2020-09-01 03:48:18 +08:00

267 lines
5.2 KiB
PHP

<?php
namespace Snowflake\Annotation;
use Exception;
use HttpServer\Route\Annotation\Websocket;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use validator\RequiredValidator;
use validator\RequiredValidator as NotEmptyValidator;
/**
* Class Annotation
* @package Snowflake\Snowflake\Annotation
*/
class Annotation extends BaseAnnotation
{
public $namespace = '';
public $prefix = '';
public $path = '';
protected $_Scan_directory = [];
protected $_alias = [];
protected $params = [];
private $_classMap = [];
/**
* @param $name
* @param $class
* @return mixed
* @throws
*/
public function register($name, $class)
{
if (!isset($this->_classMap[$name]) || is_string($this->_classMap[$name])) {
$this->_classMap[$name] = Snowflake::createObject($class);
}
return $this->_classMap[$name];
}
/**
* @param $path
* @param $namespace
* @throws ReflectionException
*/
public function registration_notes($path, $namespace)
{
$this->scanning(rtrim($path, '/'), $namespace, get_called_class());
}
/**
* @param ReflectionClass $reflect
* @param string $className
* @throws ReflectionException
* @throws Exception
* @Message(updatePosition)
* 注入注解
*/
private function resolve(ReflectionClass $reflect, string $className)
{
$controller = $reflect->newInstance();
$annotations = $this->getAnnotation($className);
$methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $function) {
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;
}
$this->push($this->getName($annotation, $events), [$controller, $methodName]);
}
}
}
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function get($name)
{
if (!isset($this->_classMap[$name])) {
throw new Exception('Undefined analytic class ' . $name . '.');
}
return $this->_classMap[$name];
}
/**
* @param $events
* @throws Exception
*/
public function isLegitimate($events)
{
throw new Exception('Undefined analytic function isLegitimate.');
}
/**
* @param $function
* @param $events
* @throws Exception
*/
public function getName($function, $events)
{
throw new Exception('Undefined analytic function getName.');
}
/**
* @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
* @param $className
* @throws ReflectionException
*/
protected function scanning($path, $namespace, $className)
{
$di = Snowflake::getDi();
foreach (glob($path . '/*') as $file) {
if (is_dir($file)) {
$this->scanning($path, $namespace, $className);
}
$explode = explode('/', $file);
$class = str_replace('.php', '', end($explode));
$this->resolve($di->getReflect($namespace . '\\' . $class), $className);
}
}
/**
* @param $path
* @param array $params
* @return bool|mixed
*/
public function runWith($path, $params = [])
{
if (!$this->has($path)) {
return null;
}
$callback = $this->_Scan_directory[$path];
if (!isset($this->params[$path])) {
return $callback(...$params);
}
return $callback(...$this->params[$path]);
}
/**
* @param $name
* @param $callback
* @param array $params
*/
public function push($name, $callback, $params = [])
{
$this->_Scan_directory[$name] = $callback;
if (!empty($params)) {
$this->params[$name] = $params;
}
}
/**
* @param $name
* @return array|null[]
*/
public function pop($name)
{
if (isset($this->_Scan_directory[$name])) {
return [$this->_Scan_directory[$name], $this->params[$name] ?? []];
}
return [null, null];
}
/**
* @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)
{
if (!isset($this->_classMap[$name])) {
return parent::__get($name); // TODO: Change the autogenerated stub
}
if (!is_object($this->_classMap[$name])) {
$this->_classMap[$name] = Snowflake::createObject($this->_classMap[$name]);
}
return $this->_classMap[$name];
}
/**
* @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);
}
}