2020-12-14 19:03:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Annotation;
|
|
|
|
|
|
|
|
|
|
|
2021-03-03 18:35:04 +08:00
|
|
|
use DirectoryIterator;
|
2021-03-23 16:14:05 +08:00
|
|
|
use Exception;
|
2021-07-27 16:15:28 +08:00
|
|
|
use ReflectionException;
|
2020-12-14 19:03:05 +08:00
|
|
|
use Snowflake\Abstracts\Component;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Annotation
|
|
|
|
|
* @package Annotation
|
|
|
|
|
*/
|
|
|
|
|
class Annotation extends Component
|
|
|
|
|
{
|
|
|
|
|
|
2021-02-23 14:16:08 +08:00
|
|
|
|
2021-04-25 17:01:57 +08:00
|
|
|
private Loader $_loader;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private array $_model_sets = [];
|
|
|
|
|
private array $_model_gets = [];
|
|
|
|
|
private array $_model_relate = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $class
|
|
|
|
|
* @param string $setName
|
|
|
|
|
* @param string $method
|
|
|
|
|
*/
|
|
|
|
|
public function addSets(string $class, string $setName, string $method)
|
|
|
|
|
{
|
|
|
|
|
$this->_model_sets[$class][$setName] = $method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $class
|
|
|
|
|
* @param string $setName
|
|
|
|
|
* @param string $method
|
|
|
|
|
*/
|
|
|
|
|
public function addGets(string $class, string $setName, string $method)
|
|
|
|
|
{
|
|
|
|
|
$this->_model_gets[$class][$setName] = $method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $class
|
|
|
|
|
* @param string $setName
|
|
|
|
|
* @param string $method
|
|
|
|
|
*/
|
|
|
|
|
public function addRelate(string $class, string $setName, string $method)
|
|
|
|
|
{
|
|
|
|
|
$this->_model_relate[$class][$setName] = $method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $class
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getGets($class): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_model_gets[$class] ?? [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $class
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getSets($class): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_model_gets[$class] ?? [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $class
|
2021-04-27 15:02:34 +08:00
|
|
|
* @param string|null $setName
|
|
|
|
|
* @return array|string|null
|
2021-04-25 17:01:57 +08:00
|
|
|
*/
|
2021-04-27 15:02:34 +08:00
|
|
|
public function getGetMethodName(string $class, string $setName = null): array|null|string
|
2021-04-25 17:01:57 +08:00
|
|
|
{
|
2021-04-27 15:02:34 +08:00
|
|
|
$gets = $this->_model_gets[$class] ?? null;
|
2021-04-25 17:01:57 +08:00
|
|
|
if ($gets == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-04-27 15:02:34 +08:00
|
|
|
if (empty($setName)) return $gets;
|
2021-04-25 17:01:57 +08:00
|
|
|
return $gets[$setName] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-05 17:55:15 +08:00
|
|
|
public function runGet($name, $value)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-04-25 17:01:57 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $class
|
2021-04-27 15:02:34 +08:00
|
|
|
* @param string|null $method
|
|
|
|
|
* @return array|string|null
|
2021-04-25 17:01:57 +08:00
|
|
|
*/
|
2021-04-27 15:02:34 +08:00
|
|
|
public function getRelateMethods(string $class, string $method = null): array|null|string
|
2021-04-25 17:01:57 +08:00
|
|
|
{
|
2021-04-27 15:02:34 +08:00
|
|
|
$gets = $this->_model_relate[$class] ?? null;
|
|
|
|
|
if ($gets == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (empty($method)) return $gets;
|
|
|
|
|
return $gets[$method] ?? null;
|
2021-04-25 17:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $class
|
|
|
|
|
* @param string $setName
|
|
|
|
|
* @return mixed|null
|
|
|
|
|
*/
|
|
|
|
|
public function getSetMethodName(string $class, string $setName): ?string
|
|
|
|
|
{
|
|
|
|
|
if (!isset($this->_model_sets[$class])) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$lists = $this->_model_sets[$class];
|
|
|
|
|
|
|
|
|
|
if (isset($lists[$setName])) {
|
|
|
|
|
return $lists[$setName];
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function init(): void
|
|
|
|
|
{
|
|
|
|
|
$this->_loader = new Loader();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Loader
|
|
|
|
|
*/
|
|
|
|
|
public function getLoader(): Loader
|
|
|
|
|
{
|
|
|
|
|
return $this->_loader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Loader $loader
|
|
|
|
|
* @return Loader
|
|
|
|
|
*/
|
|
|
|
|
public function setLoader(Loader $loader): Loader
|
|
|
|
|
{
|
|
|
|
|
return $this->_loader = $loader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $className
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @return array 根据类名获取注解
|
|
|
|
|
* 根据类名获取注解
|
|
|
|
|
*/
|
|
|
|
|
public function getMethods(string $className, string $method = ''): mixed
|
|
|
|
|
{
|
|
|
|
|
return $this->_loader->getMethod($className, $method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param object $class
|
2021-07-27 16:15:28 +08:00
|
|
|
* @throws ReflectionException
|
2021-04-25 17:01:57 +08:00
|
|
|
*/
|
|
|
|
|
public function injectProperty(object $class)
|
|
|
|
|
{
|
|
|
|
|
$this->_loader->injectProperty($class::class, $class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $namespace
|
2021-08-05 16:30:45 +08:00
|
|
|
* @param array $exclude
|
2021-04-25 17:01:57 +08:00
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-08-05 16:30:45 +08:00
|
|
|
public function read(string $path, string $namespace = 'App', array $exclude = []): void
|
2021-04-25 17:01:57 +08:00
|
|
|
{
|
2021-08-05 16:30:45 +08:00
|
|
|
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace, $exclude);
|
2021-04-25 17:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $dir
|
2021-08-05 15:55:27 +08:00
|
|
|
* @param array $exclude
|
2021-07-27 16:15:28 +08:00
|
|
|
* @return array
|
2021-04-25 17:01:57 +08:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-08-05 15:55:27 +08:00
|
|
|
public function runtime(string $dir, array $exclude = []): array
|
2021-04-25 17:01:57 +08:00
|
|
|
{
|
2021-08-05 15:55:27 +08:00
|
|
|
return $this->_loader->loadByDirectory($dir, $exclude);
|
2021-04-25 17:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-14 19:03:05 +08:00
|
|
|
}
|