e
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Annotation;
|
||||
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\BaseAnnotation;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Annotation
|
||||
* @package BeReborn\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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Snowflake\Annotation;
|
||||
|
||||
|
||||
use ReflectionClass;
|
||||
|
||||
/**
|
||||
* Class Websocket
|
||||
* @package Snowflake\Annotation
|
||||
*/
|
||||
class Websocket extends Annotation
|
||||
{
|
||||
|
||||
const MESSAGE = 'WEBSOCKET:MESSAGE:';
|
||||
const EVENT = 'WEBSOCKET:EVENT:';
|
||||
|
||||
public $Message;
|
||||
|
||||
|
||||
public $Event;
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionClass $reflect
|
||||
* @param array $methods
|
||||
*/
|
||||
public function resolve(ReflectionClass $reflect, array $methods)
|
||||
{
|
||||
$controller = $reflect->newInstance();
|
||||
|
||||
foreach ($methods as $function) {
|
||||
$comment = $function->getDocComment();
|
||||
$methodName = $function->getName();
|
||||
|
||||
preg_match('/@Event\((.*)?\)/', $comment, $events);
|
||||
if (!isset($events[1])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!($_key = $this->getName($events, $comment))) {
|
||||
continue;
|
||||
}
|
||||
$this->push($_key, [$controller, $methodName]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $events
|
||||
* @param $comment
|
||||
* @return false|string
|
||||
*/
|
||||
private function getName($events, $comment)
|
||||
{
|
||||
$event = $events[1];
|
||||
if ($event !== 'message') {
|
||||
return self::EVENT . $event;
|
||||
}
|
||||
preg_match('/@Message\((.*)?\)/', $comment, $message);
|
||||
if (isset($message[1])) {
|
||||
return false;
|
||||
}
|
||||
return self::MESSAGE . $message[1];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user