Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

methodNotAllowedFallback对405 Method Not Allowed 的支持 #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class App
*/
protected static $requestClass = '';

/**
* @var bool
*/
protected static $methodNotAllowed = false;
/**
* @var string
*/
public static $allowedMethods = '';

/**
* App constructor.
* @param string $requestClass
Expand Down Expand Up @@ -154,9 +163,10 @@ public function onMessage($connection, $request)
$plugin = $controllerAndAction['plugin'] ?? static::getPluginByPath($path);
if (!$controllerAndAction || Route::hasDisableDefaultRoute($plugin)) {
$request->plugin = $plugin;
$callback = static::getFallback($plugin);
$callback = static::$methodNotAllowed ? static::getMethodNotAllowedFallback($plugin) : static::getFallback($plugin);
$request->app = $request->controller = $request->action = '';
static::send($connection, $callback($request), $request);
static::$methodNotAllowed = false;
return null;
}
$app = $controllerAndAction['app'];
Expand Down Expand Up @@ -238,6 +248,24 @@ protected static function getFallback(string $plugin = ''): Closure
};
}

/**
* GetMethodNotAllowedFallback.
* @param string $plugin
* @return Closure
*/
protected static function getMethodNotAllowedFallback(string $plugin = ''): Closure
{

return Route::getMethodNotAllowedFallback($plugin) ?: function () {
try {
$notFoundContent = file_get_contents(static::$publicPath . '/405.html');
} catch (Throwable $e) {
$notFoundContent = '405 Method Not Allowed';
}
return new Response(405, [], $notFoundContent);
};
}

/**
* ExceptionResponse.
* @param Throwable $e
Expand Down Expand Up @@ -560,6 +588,14 @@ protected static function findRoute(TcpConnection $connection, string $path, str
static::send($connection, $callback($request), $request);
return true;
}

if (
$routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED and
count(array_diff($routeInfo[1], ['OPTIONS'])) > 0
){
static::$methodNotAllowed = true;
static::$allowedMethods = implode(',' , $routeInfo[1] ?? []);
}
return false;
}

Expand Down
26 changes: 26 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class Route
*/
protected static $fallback = [];

/**
* @var null|callable
*/
protected static $methodNotAllowedFallback = [];

/**
* @var array
*/
Expand Down Expand Up @@ -458,6 +463,27 @@ public static function getFallback(string $plugin = ''): ?callable
return static::$fallback[$plugin] ?? null;
}

/**
* methodNotAllowedFallback.
* @param callable|mixed $callback
* @param string $plugin
* @return void
*/
public static function methodNotAllowedFallback(callable $callback, string $plugin = '')
{
static::$methodNotAllowedFallback[$plugin] = $callback;
}

/**
* GetMethodNotAllowedFallback.
* @param string $plugin
* @return callable|null
*/
public static function getMethodNotAllowedFallback(string $plugin = ''): ?callable
{
return static::$methodNotAllowedFallback[$plugin] ?? null;
}

/**
* @return void
* @deprecated
Expand Down