Files
kiri-core/system/Annotation/Annotation.php
T
2020-08-31 12:38:32 +08:00

150 lines
2.5 KiB
PHP

<?php
namespace Snowflake\Annotation;
use Exception;
use ReflectionClass;
use ReflectionException;
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class Annotation
* @package Snowflake\Snowflake\Annotation
* @property Websocket $websocket
*/
class Annotation extends BaseAnnotation
{
protected $_Scan_directory = [];
public $namespace = '';
public $prefix = '';
public $path = '';
private $_classMap = [
'websocket' => Websocket::class
];
/**
* @param $name
* @param $class
*/
public function register($name, $class)
{
$this->_classMap[$name] = $class;
}
/**
* @throws ReflectionException
*/
public function registration_notes()
{
if (!is_array($this->path)) {
return;
}
foreach ($this->path as $item) {
$this->scanning($item);
}
}
/**
* @param string $path
* @throws ReflectionException
* @throws Exception
*/
protected function scanning($path = '')
{
if (empty($path)) {
$path = rtrim($this->path, '/');
}
foreach (glob($path . '/*') as $file) {
if (is_dir($file)) {
$this->scanning($path);
}
$explode = explode('/', $file);
$class = str_replace('.php', '', end($explode));
$reflect = new ReflectionClass($this->namespace . $class);
$methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
if (empty($methods) || !$reflect->isInstantiable()) {
continue;
}
$this->resolve($reflect, $methods);
}
}
/**
* @param ReflectionClass $class
* @param array $methods
* @throws Exception
*/
public function resolve(ReflectionClass $class, array $methods)
{
throw new Exception('Undefined analytic function.');
}
/**
* @param $path
* @param mixed ...$param
* @return bool|mixed
*/
public function runWith($path, $param)
{
if (!$this->has($path)) {
return null;
}
return call_user_func($this->_Scan_directory[$path], ...$param);
}
/**
* @param $name
* @param $callback
*/
public function push($name, $callback)
{
$this->_Scan_directory[$name] = $callback;
}
/**
* @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 Snowflake::createObject($this->_classMap[$name]);
}
return parent::__get($name); // TODO: Change the autogenerated stub
}
}