2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Snowflake\Abstracts;
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 22:33:50 +08:00
|
|
|
use Exception;
|
|
|
|
|
use ReflectionClass;
|
|
|
|
|
use ReflectionException;
|
|
|
|
|
use ReflectionMethod;
|
|
|
|
|
use Snowflake\Exception\NotFindClassException;
|
|
|
|
|
use Snowflake\Snowflake;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class BaseAnnotation
|
2020-08-31 12:38:32 +08:00
|
|
|
* @package Snowflake\Snowflake\Annotation\Base
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
|
|
|
|
abstract class BaseAnnotation extends Component
|
|
|
|
|
{
|
|
|
|
|
|
2020-08-31 22:33:50 +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 ReflectionClass $reflect
|
|
|
|
|
* @param array $rules
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function instance($reflect, $rules = [])
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2020-08-31 22:33:50 +08:00
|
|
|
$annotations = $this->getPrivates($reflect);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-08-31 22:33:50 +08:00
|
|
|
$classMethods = $reflect->getMethods(ReflectionMethod::IS_PUBLIC);
|
|
|
|
|
if (!$reflect->isInstantiable()) {
|
|
|
|
|
throw new Exception('Class ' . $reflect->getName() . ' cannot be instantiated.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$object = $reflect->newInstance();
|
|
|
|
|
|
|
|
|
|
$array = [];
|
|
|
|
|
foreach ($classMethods as $classMethod) {
|
|
|
|
|
$array = $this->resolveDocComment($classMethod, $object, $annotations, $array);
|
|
|
|
|
}
|
|
|
|
|
return $array;
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-31 22:33:50 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ReflectionMethod $function
|
|
|
|
|
* @param $object
|
|
|
|
|
* @param $annotations
|
|
|
|
|
* @param $array
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
protected function resolveDocComment($function, $object, $annotations, $array)
|
|
|
|
|
{
|
|
|
|
|
$comment = $function->getDocComment();
|
|
|
|
|
foreach ($annotations as $annotation) {
|
|
|
|
|
preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events);
|
|
|
|
|
if (!isset($events[1])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!($_key = $this->getName($function, $events))) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (isset($events[2])) {
|
|
|
|
|
$handler = Snowflake::createObject($events[2]);
|
|
|
|
|
} else {
|
|
|
|
|
$handler = [$object, $events[1]];
|
|
|
|
|
}
|
|
|
|
|
if (!isset($array[$annotation])) {
|
|
|
|
|
$array[$annotation] = [];
|
|
|
|
|
}
|
|
|
|
|
$array[$annotation][] = [$_key, $handler];
|
|
|
|
|
}
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $rule
|
|
|
|
|
* @param $content
|
|
|
|
|
* @param $rules
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function check($rule, $content, $rules)
|
|
|
|
|
{
|
|
|
|
|
if (empty($rule)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$explode = explode('|', $rule);
|
|
|
|
|
foreach ($explode as $value) {
|
|
|
|
|
$reflect = array_merge($rules[$value], [
|
|
|
|
|
'value' => $content
|
|
|
|
|
]);
|
|
|
|
|
$validator = Snowflake::createObject($reflect);
|
|
|
|
|
if (!$validator->check()) {
|
|
|
|
|
throw new Exception($validator->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract public function runWith($path);
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|