This commit is contained in:
2021-03-04 10:19:56 +08:00
parent 51c34d1488
commit 6180cc3125
3 changed files with 38 additions and 2 deletions
+11
View File
@@ -6,6 +6,7 @@ namespace Annotation;
use DirectoryIterator; use DirectoryIterator;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Exception\ComponentException;
/** /**
* Class Annotation * Class Annotation
@@ -50,6 +51,7 @@ class Annotation extends Component
* @param string $namespace * @param string $namespace
* @param string $alias * @param string $alias
* @return void * @return void
* @throws ComponentException
*/ */
public function read(string $path, string $namespace, string $alias = 'root'): void public function read(string $path, string $namespace, string $alias = 'root'): void
{ {
@@ -58,6 +60,15 @@ class Annotation extends Component
} }
/**
* @param string $dir
*/
public function instanceDirectoryFiles(string $dir)
{
$this->_loader->loadByDirectory($dir);
}
/** /**
* @param string $filename * @param string $filename
* @return mixed * @return mixed
+25
View File
@@ -10,6 +10,7 @@ use ReflectionClass;
use ReflectionMethod; use ReflectionMethod;
use ReflectionProperty; use ReflectionProperty;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Throwable; use Throwable;
@@ -119,6 +120,7 @@ class Loader extends BaseObject
/** /**
* @param DirectoryIterator $paths * @param DirectoryIterator $paths
* @param $namespace * @param $namespace
* @throws ComponentException
*/ */
public function _scanDir(DirectoryIterator $paths, $namespace) public function _scanDir(DirectoryIterator $paths, $namespace)
{ {
@@ -199,6 +201,29 @@ class Loader extends BaseObject
} }
/**
* @param string $path
*/
public function loadByDirectory(string $path)
{
foreach ($this->_fileMap as $value) {
if (!str_starts_with($value, $path)) {
continue;
}
$annotations = $this->getClassByFilepath($path);
foreach ($annotations['methods'] as $name => $attribute) {
foreach ($attribute as $value) {
if (!($value instanceof \Annotation\Attribute)) {
continue;
}
$value->execute([$annotations['handler'], $name]);
}
}
}
}
/** /**
* @param DirectoryIterator $path * @param DirectoryIterator $path
* @param string $namespace * @param string $namespace
+2 -2
View File
@@ -465,7 +465,7 @@ class Server extends HttpService
$router = Snowflake::app()->getRouter(); $router = Snowflake::app()->getRouter();
$router->loadRouterSetting(); $router->loadRouterSetting();
recursive_directory(CONTROLLER_PATH); annotation()->instanceDirectoryFiles(CONTROLLER_PATH);
}); });
} }
@@ -477,7 +477,7 @@ 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 () {
recursive_directory(SOCKET_PATH); annotation()->instanceDirectoryFiles(SOCKET_PATH);
}); });
} }