Skip to content

Commit

Permalink
Merge pull request #55 from zcmzc/master
Browse files Browse the repository at this point in the history
Fixed method level middleware
  • Loading branch information
huangzhhui committed Jun 26, 2019
2 parents 58ed44d + 4a1c2e2 commit 3d99ee7
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/http-server/src/Router/DispatcherFactory.php
Expand Up @@ -106,7 +106,7 @@ protected function initAnnotationRoute(array $collector): void
/**
* Register route according to AutoController annotation.
*/
protected function handleAutoController(string $className, AutoController $annotation, array $middlewares, array $methodMetadata = []): void
protected function handleAutoController(string $className, AutoController $annotation, array $middlewares = [], array $methodMetadata = []): void
{
$class = ReflectionManager::reflectClass($className);
$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
Expand All @@ -120,22 +120,22 @@ protected function handleAutoController(string $className, AutoController $annot
$methodName = $method->getName();
$router->addRoute($autoMethods, $path, [$className, $methodName, $annotation->server]);

$methodMiddlewares = $middlewares;
// Handle method level middlewares.
if (isset($methodMetadata[$methodName])) {
$methodMiddlewares = $this->handleMiddleware($methodMetadata[$methodName]);
$middlewares = array_merge($methodMiddlewares, $middlewares);
$methodMiddlewares = array_merge($methodMiddlewares, $this->handleMiddleware($methodMetadata[$methodName]));
$methodMiddlewares = array_unique($methodMiddlewares);
}
$middlewares = array_unique($middlewares);

// Register middlewares.
foreach ($autoMethods as $autoMethod) {
MiddlewareManager::addMiddlewares($annotation->server, $path, $autoMethod, $middlewares);
MiddlewareManager::addMiddlewares($annotation->server, $path, $autoMethod, $methodMiddlewares);
}
if (Str::endsWith($path, $defaultAction)) {
$path = Str::replaceLast($defaultAction, '', $path);
$router->addRoute($autoMethods, $path, [$className, $methodName, $annotation->server]);
foreach ($autoMethods as $autoMethod) {
MiddlewareManager::addMiddlewares($annotation->server, $path, $autoMethod, $middlewares);
MiddlewareManager::addMiddlewares($annotation->server, $path, $autoMethod, $methodMiddlewares);
}
}
}
Expand All @@ -162,6 +162,14 @@ protected function handleController(string $className, Controller $annotation, a
];

foreach ($methodMetadata as $methodName => $values) {

$methodMiddlewares = $middlewares;
// Handle method level middlewares.
if (isset($values)) {
$methodMiddlewares = array_merge($methodMiddlewares, $this->handleMiddleware($values));
$methodMiddlewares = array_unique($methodMiddlewares);
}

foreach ($mappingAnnotations as $mappingAnnotation) {
/** @var Mapping $mapping */
if ($mapping = $values[$mappingAnnotation] ?? null) {
Expand All @@ -179,16 +187,9 @@ protected function handleController(string $className, Controller $annotation, a
$annotation->server,
]);

// Handle method level middlewares.
if (isset($methodMetadata[$methodName])) {
$methodMiddlewares = $this->handleMiddleware($methodMetadata[$methodName]);
$middlewares = array_merge($methodMiddlewares, $middlewares);
}
$middlewares = array_unique($middlewares);

// Register middlewares.
foreach ($mapping->methods as $mappingMethod) {
MiddlewareManager::addMiddlewares($annotation->server, $path, $mappingMethod, $middlewares);
MiddlewareManager::addMiddlewares($annotation->server, $path, $mappingMethod, $methodMiddlewares);
}
}
}
Expand Down

0 comments on commit 3d99ee7

Please sign in to comment.