Skip to content

Commit

Permalink
重构生成请求上下文代理,使用原生注解
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Oct 25, 2023
1 parent d8d69f1 commit 3f7008d
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 59 deletions.
20 changes: 9 additions & 11 deletions src/RequestContextProxy/Cli/RequestContextProxyGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Imi\RequestContextProxy\Cli;

use Imi\Bean\Annotation;
use Imi\Bean\Annotation\Bean;
use Imi\Bean\BeanFactory;
use Imi\Bean\BeanManager;
use Imi\Bean\ReflectionUtil;
use Imi\Bean\Util\AttributeUtil;
use Imi\Cli\Annotation\Command;
use Imi\Cli\Annotation\CommandAction;
use Imi\Cli\Annotation\Option;
Expand Down Expand Up @@ -60,21 +60,19 @@ public function generate(string $target, string $class, string $name, ?string $b
throw new \RuntimeException(sprintf('Get namespace %s path failed', $namespace));
}
$fileName = File::path($fileName, $shortClassName . '.php');
$requestContextProxyAnnotation = Annotation::toComments(new RequestContextProxy([
$attributes = [];
$attributes[] = new RequestContextProxy([
'class' => $class,
'name' => $name,
]));
if (null === $bean)
]);
if (null !== $bean)
{
$beanAnnotation = null;
}
else
{
$beanAnnotation = Annotation::toComments(new Bean([
$attributes[] = new Bean([
'name' => $bean,
'recursion' => $recursion,
]));
]);
}
$classAttributesCode = AttributeUtil::generateAttributesCode($attributes);
$refClass = new \ReflectionClass($fromClass);
$methods = [];
foreach ($refClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method)
Expand Down Expand Up @@ -208,7 +206,7 @@ public function generate(string $target, string $class, string $name, ?string $b
}
}
// @phpstan-ignore-next-line
$content = (static function () use ($namespace, $requestContextProxyAnnotation, $methods, $shortClassName, $beanAnnotation, $interface, $methodCodes): string {
$content = (static function () use ($namespace, $classAttributesCode, $methods, $shortClassName, $interface, $methodCodes): string {
ob_start();
include __DIR__ . '/template.tpl';

Expand Down
20 changes: 8 additions & 12 deletions src/RequestContextProxy/Cli/template.tpl
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
<?php echo '<?php'; ?>
<?php declare(strict_types=1);
echo '<?php'; ?>

namespace <?php echo $namespace; ?>;

use Imi\Bean\Annotation\Bean;
use Imi\RequestContextProxy\BaseRequestContextProxy;
use Imi\RequestContextProxy\Annotation\RequestContextProxy;

/**
<?php if ($beanAnnotation) { ?>
* <?php echo $beanAnnotation; ?>
<?php }?>
* <?php echo $requestContextProxyAnnotation; ?>

*
<?php foreach ($methods as $item) { ?>
<?php foreach ($methods as $item)
{ ?>
* <?php echo $item; ?>
<?php } ?>
*/
class <?php echo $shortClassName; ?> extends BaseRequestContextProxy<?php if ($interface) { ?> implements \<?php echo $interface; ?><?php }?>
<?php echo $classAttributesCode; ?>

class <?php echo $shortClassName; ?> extends BaseRequestContextProxy<?php if ($interface)
{ ?> implements \<?php echo $interface; ?><?php }?>

{
<?php echo $methodCodes; ?>
Expand Down
6 changes: 3 additions & 3 deletions src/Server/Http/Message/Proxy/RequestProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

namespace Imi\Server\Http\Message\Proxy;

use Imi\RequestContextProxy\Annotation\RequestContextProxy;
use Imi\RequestContextProxy\BaseRequestContextProxy;

/**
* @RequestContextProxy(class="Imi\Server\Http\Message\Contract\IHttpRequest", name="request")
*
* @method \Imi\Util\Socket\IPEndPoint getClientAddress()
* @method static \Imi\Util\Socket\IPEndPoint getClientAddress()
* @method mixed getServerParam(string $name, mixed $default = NULL)
Expand Down Expand Up @@ -121,6 +118,9 @@
* @method \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
* @method static \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
*/
#[
\Imi\RequestContextProxy\Annotation\RequestContextProxy(class: 'Imi\\Server\\Http\\Message\\Contract\\IHttpRequest', name: 'request')
]
class RequestProxy extends BaseRequestContextProxy
{
}
10 changes: 4 additions & 6 deletions src/Server/Http/Message/Proxy/RequestProxyObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

namespace Imi\Server\Http\Message\Proxy;

use Imi\Bean\Annotation\Bean;
use Imi\RequestContextProxy\Annotation\RequestContextProxy;
use Imi\RequestContextProxy\BaseRequestContextProxy;

/**
* @Bean(name="HttpRequestProxy", recursion=false, instanceType="singleton")
*
* @RequestContextProxy(class="Imi\Server\Http\Message\Contract\IHttpRequest", name="request")
*
* @method \Imi\Util\Socket\IPEndPoint getClientAddress()
* @method static \Imi\Util\Socket\IPEndPoint getClientAddress()
* @method mixed getServerParam(string $name, mixed $default = NULL)
Expand Down Expand Up @@ -124,6 +118,10 @@
* @method \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
* @method static \Psr\Http\Message\UriInterface getAppUri(?string $serverName = NULL)
*/
#[
\Imi\RequestContextProxy\Annotation\RequestContextProxy(class: 'Imi\\Server\\Http\\Message\\Contract\\IHttpRequest', name: 'request'),
\Imi\Bean\Annotation\Bean(name: 'HttpRequestProxy', recursion: false)
]
class RequestProxyObject extends BaseRequestContextProxy implements \Imi\Server\Http\Message\Contract\IHttpRequest
{
/**
Expand Down
6 changes: 3 additions & 3 deletions src/Server/Http/Message/Proxy/ResponseProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

namespace Imi\Server\Http\Message\Proxy;

use Imi\RequestContextProxy\Annotation\RequestContextProxy;
use Imi\RequestContextProxy\BaseRequestContextProxy;

/**
* @RequestContextProxy(class="Imi\Server\Http\Message\Contract\IHttpResponse", name="response")
*
* @method \Imi\Server\Http\Message\Contract\IHttpResponse redirect(string $url, int $status = 302)
* @method static \Imi\Server\Http\Message\Contract\IHttpResponse redirect(string $url, int $status = 302)
* @method \Imi\Server\Http\Message\Contract\IHttpResponse send()
Expand Down Expand Up @@ -85,6 +82,9 @@
* @method \Imi\Util\Http\Contract\IMessage setBody(\Psr\Http\Message\StreamInterface $body)
* @method static \Imi\Util\Http\Contract\IMessage setBody(\Psr\Http\Message\StreamInterface $body)
*/
#[
\Imi\RequestContextProxy\Annotation\RequestContextProxy(class: 'Imi\\Server\\Http\\Message\\Contract\\IHttpResponse', name: 'response')
]
class ResponseProxy extends BaseRequestContextProxy
{
}
10 changes: 4 additions & 6 deletions src/Server/Http/Message/Proxy/ResponseProxyObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

namespace Imi\Server\Http\Message\Proxy;

use Imi\Bean\Annotation\Bean;
use Imi\RequestContextProxy\Annotation\RequestContextProxy;
use Imi\RequestContextProxy\BaseRequestContextProxy;

/**
* @Bean(name="HttpResponseProxy", recursion=false, instanceType="singleton")
*
* @RequestContextProxy(class="Imi\Server\Http\Message\Contract\IHttpResponse", name="response")
*
* @method \Imi\Server\Http\Message\Contract\IHttpResponse redirect(string $url, int $status = 302)
* @method static \Imi\Server\Http\Message\Contract\IHttpResponse redirect(string $url, int $status = 302)
* @method \Imi\Server\Http\Message\Contract\IHttpResponse send()
Expand Down Expand Up @@ -88,6 +82,10 @@
* @method \Imi\Util\Http\Contract\IMessage setBody(\Psr\Http\Message\StreamInterface $body)
* @method static \Imi\Util\Http\Contract\IMessage setBody(\Psr\Http\Message\StreamInterface $body)
*/
#[
\Imi\RequestContextProxy\Annotation\RequestContextProxy(class: 'Imi\\Server\\Http\\Message\\Contract\\IHttpResponse', name: 'response'),
\Imi\Bean\Annotation\Bean(name: 'HttpResponseProxy', recursion: false)
]
class ResponseProxyObject extends BaseRequestContextProxy implements \Imi\Server\Http\Message\Contract\IHttpResponse
{
/**
Expand Down
10 changes: 4 additions & 6 deletions src/Server/TcpServer/Message/Proxy/ReceiveDataProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

namespace Imi\Server\TcpServer\Message\Proxy;

use Imi\Bean\Annotation\Bean;
use Imi\RequestContextProxy\Annotation\RequestContextProxy;
use Imi\RequestContextProxy\BaseRequestContextProxy;

/**
* @Bean(name="TcpReceiveDataProxy", recursion=false, instanceType="singleton")
*
* @RequestContextProxy(class="Imi\Server\TcpServer\Message\IReceiveData", name="receiveData")
*
* @method int|string getClientId()
* @method static int|string getClientId()
* @method string getData()
Expand All @@ -22,6 +16,10 @@
* @method \Imi\Util\Socket\IPEndPoint getClientAddress()
* @method static \Imi\Util\Socket\IPEndPoint getClientAddress()
*/
#[
\Imi\RequestContextProxy\Annotation\RequestContextProxy(class: 'Imi\\Server\\TcpServer\\Message\\IReceiveData', name: 'receiveData'),
\Imi\Bean\Annotation\Bean(name: 'TcpReceiveDataProxy', recursion: false)
]
class ReceiveDataProxy extends BaseRequestContextProxy implements \Imi\Server\TcpServer\Message\IReceiveData
{
/**
Expand Down
10 changes: 4 additions & 6 deletions src/Server/UdpServer/Message/Proxy/PacketDataProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

namespace Imi\Server\UdpServer\Message\Proxy;

use Imi\Bean\Annotation\Bean;
use Imi\RequestContextProxy\Annotation\RequestContextProxy;
use Imi\RequestContextProxy\BaseRequestContextProxy;

/**
* @Bean(name="UdpPacketDataProxy", recursion=false, instanceType="singleton")
*
* @RequestContextProxy(class="Imi\Server\UdpServer\Message\IPacketData", name="packetData")
*
* @method string getData()
* @method static string getData()
* @method mixed getFormatData()
* @method static mixed getFormatData()
* @method \Imi\Util\Socket\IPEndPoint getClientAddress()
* @method static \Imi\Util\Socket\IPEndPoint getClientAddress()
*/
#[
\Imi\RequestContextProxy\Annotation\RequestContextProxy(class: 'Imi\\Server\\UdpServer\\Message\\IPacketData', name: 'packetData'),
\Imi\Bean\Annotation\Bean(name: 'UdpPacketDataProxy', recursion: false)
]
class PacketDataProxy extends BaseRequestContextProxy implements \Imi\Server\UdpServer\Message\IPacketData
{
/**
Expand Down
10 changes: 4 additions & 6 deletions src/Server/WebSocket/Message/Proxy/FrameProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

namespace Imi\Server\WebSocket\Message\Proxy;

use Imi\Bean\Annotation\Bean;
use Imi\RequestContextProxy\Annotation\RequestContextProxy;
use Imi\RequestContextProxy\BaseRequestContextProxy;

/**
* @Bean(name="WebSocketFrameProxy", recursion=false, instanceType="singleton")
*
* @RequestContextProxy(class="Imi\Server\WebSocket\Message\IFrame", name="frame")
*
* @method int|string getClientId()
* @method static int|string getClientId()
* @method string getData()
Expand All @@ -26,6 +20,10 @@
* @method \Imi\Util\Socket\IPEndPoint getClientAddress()
* @method static \Imi\Util\Socket\IPEndPoint getClientAddress()
*/
#[
\Imi\RequestContextProxy\Annotation\RequestContextProxy(class: 'Imi\\Server\\WebSocket\\Message\\IFrame', name: 'frame'),
\Imi\Bean\Annotation\Bean(name: 'WebSocketFrameProxy', recursion: false)
]
class FrameProxy extends BaseRequestContextProxy implements \Imi\Server\WebSocket\Message\IFrame
{
/**
Expand Down

0 comments on commit 3f7008d

Please sign in to comment.