This commit is contained in:
2021-03-03 19:02:27 +08:00
parent c337280d4e
commit 1daf9d95ca
3 changed files with 55 additions and 3 deletions
+10
View File
@@ -56,4 +56,14 @@ class Annotation extends Component
$this->_loader->_scanDir(new DirectoryIterator($path), $namespace); $this->_loader->_scanDir(new DirectoryIterator($path), $namespace);
} }
/**
* @param string $filename
* @return mixed
*/
public function getFilename(string $filename): mixed
{
return $this->_loader->getClassByFilepath($filename);
}
} }
+20 -1
View File
@@ -24,6 +24,9 @@ class Loader extends BaseObject
private array $_classes = []; private array $_classes = [];
private array $_fileMap = [];
/** /**
* @param $path * @param $path
* @param $namespace * @param $namespace
@@ -143,7 +146,7 @@ class Loader extends BaseObject
continue; continue;
} }
$_array = ['target' => [], 'methods' => [], 'property' => []]; $_array = ['handler' => $replace->newInstanceWithoutConstructor(), 'target' => [], 'methods' => [], 'property' => []];
foreach ($replace->getAttributes() as $attribute) { foreach ($replace->getAttributes() as $attribute) {
if ($attribute->getName() == Attribute::class) { if ($attribute->getName() == Attribute::class) {
continue; continue;
@@ -175,10 +178,26 @@ class Loader extends BaseObject
$_array['property'][$method->getName()] = $_property; $_array['property'][$method->getName()] = $_property;
} }
$this->_fileMap[$replace->getFileName()] = $replace->getName();
$this->_classes[$replace->getName()] = $_array; $this->_classes[$replace->getName()] = $_array;
} catch (Throwable $throwable) { } catch (Throwable $throwable) {
echo $throwable->getMessage() . PHP_EOL; echo $throwable->getMessage() . PHP_EOL;
} }
} }
} }
/**
* @param string $filename
* @return mixed
*/
public function getClassByFilepath(string $filename): mixed
{
if (!isset($this->_fileMap[$filename])) {
return null;
}
return $this->_classes[$this->_fileMap[$filename]];
}
} }
+25 -2
View File
@@ -3,6 +3,7 @@
namespace HttpServer; namespace HttpServer;
use Annotation\Attribute;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use HttpServer\Abstracts\HttpService; use HttpServer\Abstracts\HttpService;
use HttpServer\Events\OnClose; use HttpServer\Events\OnClose;
@@ -464,7 +465,18 @@ class Server extends HttpService
$router->loadRouterSetting(); $router->loadRouterSetting();
$attributes = Snowflake::app()->getAttributes(); $attributes = Snowflake::app()->getAttributes();
$attributes->read(CONTROLLER_PATH, 'App\Http\Controllers', 'controllers');
recursive_directory(CONTROLLER_PATH, function (\DirectoryIterator $file) use ($attributes) {
$annotations = $attributes->getFilename($file->getRealPath());
/** @var Attribute $attribute */
foreach ($annotations['methods'] as $name => $attribute) {
if (!($attribute instanceof Attribute)) {
continue;
}
$attribute->execute([$annotations['handler'], $name]);
}
});
}); });
} }
@@ -477,7 +489,18 @@ class Server extends HttpService
$event = Snowflake::app()->getEvent(); $event = Snowflake::app()->getEvent();
$event->on(Event::SERVER_WORKER_START, function () { $event->on(Event::SERVER_WORKER_START, function () {
$attributes = Snowflake::app()->getAttributes(); $attributes = Snowflake::app()->getAttributes();
$attributes->read(SOCKET_PATH, 'App\Websocket', 'sockets');
recursive_directory(SOCKET_PATH, function (\DirectoryIterator $file) use ($attributes) {
$annotations = $attributes->getFilename($file->getRealPath());
/** @var Attribute $attribute */
foreach ($annotations['methods'] as $name => $attribute) {
if (!($attribute instanceof Attribute)) {
continue;
}
$attribute->execute([$annotations['handler'], $name]);
}
});
}); });
} }