Files
kiri-core/Annotation/Annotation.php
T

207 lines
3.6 KiB
PHP
Raw Normal View History

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;
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;
}
/**
* @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
*/
public function injectProperty(object $class)
{
$this->_loader->injectProperty($class::class, $class);
}
/**
* @param string $path
* @param string $namespace
* @param string $alias
* @return void
* @throws Exception
*/
2021-05-04 01:43:32 +08:00
public function read(string $path, string $namespace = 'App', string $alias = 'root'): void
2021-04-25 17:01:57 +08:00
{
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
}
/**
* @param string $dir
* @param string|array $outPath
* @throws Exception
*/
public function runtime(string $dir, string|array $outPath = '')
{
if (empty($outPath)) {
$outPath = [];
} else if (is_string($outPath)) {
$outPath = [$outPath];
}
$this->_loader->loadByDirectory($dir, $outPath);
}
2020-12-14 19:03:05 +08:00
}