eee
This commit is contained in:
@@ -19,8 +19,9 @@ class DeferProxyGenerator
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @param array $construct
|
* @param array $construct
|
||||||
* @return object
|
* @return object
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public static function create(string $className, array $construct): object
|
public static function create(string $className, array $construct): object
|
||||||
{
|
{
|
||||||
@@ -60,6 +61,7 @@ class DeferProxyGenerator
|
|||||||
/**
|
/**
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @return string
|
* @return string
|
||||||
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
private static function generate(string $className): string
|
private static function generate(string $className): string
|
||||||
{
|
{
|
||||||
@@ -195,7 +197,6 @@ class DeferProxyGenerator
|
|||||||
$value = $param->getDefaultValue();
|
$value = $param->getDefaultValue();
|
||||||
|
|
||||||
return match (true) {
|
return match (true) {
|
||||||
is_null($value) => new Expr\ConstFetch(new Name('null')),
|
|
||||||
is_bool($value) => new Expr\ConstFetch(new Name($value ? 'true' : 'false')),
|
is_bool($value) => new Expr\ConstFetch(new Name($value ? 'true' : 'false')),
|
||||||
is_int($value) => new Node\Scalar\LNumber($value),
|
is_int($value) => new Node\Scalar\LNumber($value),
|
||||||
is_float($value) => new Node\Scalar\DNumber($value),
|
is_float($value) => new Node\Scalar\DNumber($value),
|
||||||
@@ -218,7 +219,6 @@ class DeferProxyGenerator
|
|||||||
private static function buildDefaultValueFromScalar(mixed $value): Node\Expr
|
private static function buildDefaultValueFromScalar(mixed $value): Node\Expr
|
||||||
{
|
{
|
||||||
return match (true) {
|
return match (true) {
|
||||||
is_null($value) => new Expr\ConstFetch(new Name('null')),
|
|
||||||
is_bool($value) => new Expr\ConstFetch(new Name($value ? 'true' : 'false')),
|
is_bool($value) => new Expr\ConstFetch(new Name($value ? 'true' : 'false')),
|
||||||
is_int($value) => new Node\Scalar\LNumber($value),
|
is_int($value) => new Node\Scalar\LNumber($value),
|
||||||
is_float($value) => new Node\Scalar\DNumber($value),
|
is_float($value) => new Node\Scalar\DNumber($value),
|
||||||
|
|||||||
@@ -1,102 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use PhpParser\Error;
|
|
||||||
use PhpParser\Node;
|
|
||||||
use PhpParser\NodeDumper;
|
|
||||||
use PhpParser\NodeFactory;
|
|
||||||
use PhpParser\NodeTraverser;
|
|
||||||
use PhpParser\NodeVisitorAbstract;
|
|
||||||
use PhpParser\ParserFactory;
|
|
||||||
use PhpParser\PrettyPrinter\Standard;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
|
||||||
|
|
||||||
class MiddlewareProxyGenerator {
|
|
||||||
private static string $cacheFile = __DIR__ . '/middleware_proxy.php';
|
|
||||||
private static NodeFactory $factory;
|
|
||||||
|
|
||||||
public static function generateProxy(array $middlewares): string {
|
|
||||||
self::$factory = new NodeFactory();
|
|
||||||
|
|
||||||
// 创建类结构(v5.5.0兼容写法)
|
|
||||||
$class = new Node\Stmt\Class_(
|
|
||||||
'MiddlewareProxy',
|
|
||||||
[
|
|
||||||
'implements' => [new Node\Name\FullyQualified(\Psr\Http\Server\MiddlewareInterface::class)],
|
|
||||||
'stmts' => []
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// 生成中间件调用链
|
|
||||||
$method = self::createMethodNode($middlewares);
|
|
||||||
$class->stmts[] = $method;
|
|
||||||
|
|
||||||
// 生成代码
|
|
||||||
$printer = new Standard();
|
|
||||||
return $printer->prettyPrintFile([$class]);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function createMethodNode(array $middlewares): Node\Stmt\Function_ {
|
|
||||||
$method = new Node\Stmt\Function_(
|
|
||||||
'process',
|
|
||||||
[
|
|
||||||
'params' => [
|
|
||||||
new Node\Param(
|
|
||||||
new Node\Expr\Variable('request'),
|
|
||||||
null,
|
|
||||||
new Node\UnionType([new Node\Expr\Variable(\Psr\Http\Message\ServerRequestInterface::class)])
|
|
||||||
),
|
|
||||||
new Node\Param(
|
|
||||||
new Node\Expr\Variable('handler'),
|
|
||||||
null,
|
|
||||||
new Node\UnionType([new Node\Expr\Variable(\Psr\Http\Server\RequestHandlerInterface::class)])
|
|
||||||
)
|
|
||||||
],
|
|
||||||
'returnType' => new Node\Name(ResponseInterface::class),
|
|
||||||
'stmts' => []
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
// 生成中间件调用链(v5.5.0闭包写法)
|
|
||||||
$chain = [];
|
|
||||||
$count = count($middlewares);
|
|
||||||
for ($i = 0; $i < $count; $i++) {
|
|
||||||
$className = $middlewares[$i]::class;
|
|
||||||
$chain[] = self::createMiddlewareCall($i, $className, $count);
|
|
||||||
}
|
|
||||||
|
|
||||||
$method->stmts = array_reverse($chain);
|
|
||||||
return $method;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function createMiddlewareCall(int $index, string $className, int $count): Node {
|
|
||||||
$mwVar = new Node\Expr\Variable("mw{$index}");
|
|
||||||
$nextHandler = new Node\Expr\Variable("next");
|
|
||||||
|
|
||||||
// 创建中间件实例(v5.5.0兼容)
|
|
||||||
$instanciate = new Node\Expr\New_($className);
|
|
||||||
|
|
||||||
// 创建闭包表达式(v5.5.0语法)
|
|
||||||
$closure = new Node\Expr\Closure([
|
|
||||||
'static' => true,
|
|
||||||
'params' => [new Node\Param($nextHandler)],
|
|
||||||
'stmts' => [
|
|
||||||
new Node\Stmt\Expression(
|
|
||||||
new Node\Expr\MethodCall(
|
|
||||||
$instanciate,
|
|
||||||
'process',
|
|
||||||
[new Node\Arg($nextHandler), new Node\Arg($mwVar)]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
|
|
||||||
return new Node\Stmt\Expression($closure);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成代理类代码
|
|
||||||
$middlewares = [AuthMiddleware::class, LogMiddleware::class];
|
|
||||||
$proxyCode = MiddlewareProxyGenerator::generateProxy($middlewares);
|
|
||||||
|
|
||||||
// 写入代理类文件
|
|
||||||
file_put_contents(MiddlewareProxyGenerator::$cacheFile, $proxyCode);
|
|
||||||
Reference in New Issue
Block a user