Files
swiftadmin/vendor/workerman/webman-framework/src/Middleware.php

82 lines
2.2 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
2023-04-25 20:11:49 +08:00
namespace Webman;
use RuntimeException;
use function array_merge;
use function array_reverse;
use function is_array;
use function method_exists;
2022-08-19 19:48:37 +08:00
class Middleware
{
/**
* @var array
*/
2023-04-25 20:11:49 +08:00
protected static $instances = [];
2022-08-19 19:48:37 +08:00
/**
2023-04-25 20:11:49 +08:00
* @param mixed $allMiddlewares
2022-08-19 19:48:37 +08:00
* @param string $plugin
* @return void
*/
2023-04-25 20:11:49 +08:00
public static function load($allMiddlewares, string $plugin = '')
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
if (!is_array($allMiddlewares)) {
2022-08-19 19:48:37 +08:00
return;
}
2023-04-25 20:11:49 +08:00
foreach ($allMiddlewares as $appName => $middlewares) {
if (!is_array($middlewares)) {
throw new RuntimeException('Bad middleware config');
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
foreach ($middlewares as $className) {
if (method_exists($className, 'process')) {
static::$instances[$plugin][$appName][] = [$className, 'process'];
2022-08-19 19:48:37 +08:00
} else {
// @todo Log
2023-04-25 20:11:49 +08:00
echo "middleware $className::process not exsits\n";
2022-08-19 19:48:37 +08:00
}
}
}
}
/**
* @param string $plugin
2023-04-25 20:11:49 +08:00
* @param string $appName
* @param bool $withGlobalMiddleware
2022-08-19 19:48:37 +08:00
* @return array|mixed
*/
2023-04-25 20:11:49 +08:00
public static function getMiddleware(string $plugin, string $appName, bool $withGlobalMiddleware = true)
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
$globalMiddleware = $withGlobalMiddleware && isset(static::$instances[$plugin]['']) ? static::$instances[$plugin][''] : [];
if ($appName === '') {
return array_reverse($globalMiddleware);
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
$appMiddleware = static::$instances[$plugin][$appName] ?? [];
return array_reverse(array_merge($globalMiddleware, $appMiddleware));
2022-08-19 19:48:37 +08:00
}
/**
* @return void
2023-04-25 20:11:49 +08:00
* @deprecated
2022-08-19 19:48:37 +08:00
*/
public static function container($_)
{
}
}