Skip to content

Commit ee3e6a7

Browse files
committed
support amp
1 parent b4e911b commit ee3e6a7

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"symfony/finder": "^6",
3232
"guzzlehttp/guzzle": "^7.7",
3333
"symfony/dotenv": "^6.0",
34+
"monolog/monolog": "^3.4",
3435
"ext-simplexml": "*"
3536
}
3637
}

src/Commands/StartCommand.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3333
Driver::WORKERMAN => $this->workermanStart(),
3434
Driver::ROADRUNNER => $this->roadrunnerStart($output, $serverConfig),
3535
Driver::SWOOLE => $this->swooleStart(),
36+
Driver::AMP => $this->ampStart(),
3637
default => throw new RuntimeException('unsupported driver: ' . $serverConfig->getDriver()),
3738
};
3839
}
@@ -91,4 +92,16 @@ private function swooleStart(): int
9192

9293
return Command::SUCCESS;
9394
}
95+
96+
/**
97+
* @throws ContainerExceptionInterface
98+
* @throws ReflectionException
99+
* @throws NotFoundExceptionInterface
100+
*/
101+
private function ampStart(): int
102+
{
103+
Application::getClass(Application::class)->run();
104+
105+
return Command::SUCCESS;
106+
}
94107
}

src/Http/Enum/Driver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ class Driver
1111
public const ROADRUNNER = 'roadrunner';
1212

1313
public const SWOOLE = 'swoole';
14+
15+
public const AMP = 'amp';
1416
}

src/Http/ServerFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MicroPHP\Framework\Http;
66

7+
use MicroPHP\Amp\AmpHttpServer;
78
use MicroPHP\Framework\Http\Contract\HttpServerInterface;
89
use MicroPHP\Framework\Http\Enum\Driver;
910
use MicroPHP\RoadRunner\RoadRunnerHttpServer;
@@ -21,6 +22,7 @@ public static function newServer(): HttpServerInterface
2122
Driver::WORKERMAN => new WorkermanHttpServer(),
2223
Driver::ROADRUNNER => new RoadRunnerHttpServer(),
2324
Driver::SWOOLE => new SwooleHttpServer(),
25+
Driver::AMP => new AmpHttpServer(),
2426
default => throw new RuntimeException('unsupported driver: ' . $serverConfig->getDriver()),
2527
};
2628
}

src/Http/ServerRequest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace MicroPHP\Framework\Http;
66

7+
use Amp\ByteStream\BufferException;
8+
use Amp\ByteStream\StreamException;
9+
use Amp\Http\Server\ClientException;
710
use GuzzleHttp\Psr7\HttpFactory;
811
use GuzzleHttp\Psr7\Uri;
912
use InvalidArgumentException;
@@ -45,6 +48,26 @@ public static function fromSwoole(Request $swooleRequest): ServerRequestInterfac
4548
->withUploadedFiles(self::normalizeFiles($swooleRequest->files ?? []));
4649
}
4750

51+
/**
52+
* @throws ClientException
53+
* @throws BufferException
54+
* @throws StreamException
55+
*/
56+
public static function fromAmp(\Amp\Http\Server\Request $ampRequest): ServerRequestInterface|ServerRequest
57+
{
58+
$request = new ServerRequest(
59+
$ampRequest->getMethod(),
60+
$ampRequest->getUri(),
61+
$ampRequest->getHeaders(),
62+
$ampRequest->getBody()->buffer(),
63+
$ampRequest->getProtocolVersion(),
64+
);
65+
66+
return $request->withParsedBody(self::normalizeParsedBody([], $request))
67+
->withCookieParams($ampRequest->getCookies())
68+
->withQueryParams($ampRequest->getQueryParameters());
69+
}
70+
4871
protected static function normalizeParsedBody(array $data = [], RequestInterface $request = null): array
4972
{
5073
if (! $request) {

src/Router/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Router extends \League\Route\Router
1010
{
1111
public function getOrPost(string $path, $handler): RouteGroup
1212
{
13-
return $this->group('', function(RouteGroup $group) use ($path, $handler) {
13+
return $this->group('', function (RouteGroup $group) use ($path, $handler) {
1414
$group->get($path, $handler);
1515
$group->post($path, $handler);
1616
});

0 commit comments

Comments
 (0)