Skip to content

Commit

Permalink
Remove attribute dependency from FormParser (#14)
Browse files Browse the repository at this point in the history
Instead of parseRequest, we provide Form::fromRequest now.
  • Loading branch information
kelunik committed Aug 22, 2023
1 parent 05278b2 commit 62bf983
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ Basic usage works by calling `parseForm($request)`, which will buffer the reques
```php
<?php

use Amp\Http\Server\FormParser;
use Amp\Http\Server\FormParser\Form;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Status;

new ClosureRequestHandler(function (Request $request) {
$form = FormParser\parseForm($request);
$form = Form::fromRequest($request);

return new Response(Status::OK, [
"content-type" => "text/plain; charset=utf-8"
Expand Down
4 changes: 2 additions & 2 deletions examples/basic-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Amp\ByteStream;
use Amp\Http\HttpStatus;
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\FormParser;
use Amp\Http\Server\FormParser\Form;
use Amp\Http\Server\Request;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
Expand Down Expand Up @@ -50,7 +50,7 @@
);
}

$form = FormParser\parseForm($request);
$form = Form::fromRequest($request);
$html = <<<HTML
<html lang="en">
<body>
Expand Down
26 changes: 26 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,38 @@

use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Http\Server\Request;

final class Form
{
use ForbidCloning;
use ForbidSerialization;

private static FormParser $formParser;

/**
* Try parsing the request's body with either application/x-www-form-urlencoded or multipart/form-data.
*/
public static function fromRequest(Request $request, ?FormParser $formParser = null): self
{
if ($request->hasAttribute(Form::class)) {
return $request->getAttribute(Form::class);
}

$form = ($formParser ?? self::getFormParser())->parseForm($request);

$request->setAttribute(Form::class, $form);

return $form;
}

private static function getFormParser(): FormParser
{
self::$formParser ??= new FormParser();

return self::$formParser;
}

/** @var list<non-empty-string>|null */
private ?array $names = null;

Expand Down
10 changes: 1 addition & 9 deletions src/FormParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ public function __construct(?int $fieldCountLimit = null)
*/
public function parseForm(Request $request): Form
{
if ($request->hasAttribute(Form::class)) {
return $request->getAttribute(Form::class);
}

$boundary = parseContentBoundary($request->getHeader('content-type') ?? '');

// Don't consume body if we don't have a form content type
Expand All @@ -50,11 +46,7 @@ public function parseForm(Request $request): Form
throw new HttpErrorException(HttpStatus::BAD_REQUEST, "Request body ended unexpectedly");
}

$form = $this->parseBody($body, $boundary);

$request->setAttribute(Form::class, $form);

return $form;
return $this->parseBody($body, $boundary);
}

/**
Expand Down
16 changes: 0 additions & 16 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@

namespace Amp\Http\Server\FormParser;

use Amp\Http\Server\HttpErrorException;
use Amp\Http\Server\Request;

/**
* Try parsing the request's body with either application/x-www-form-urlencoded or multipart/form-data.
*
* @throws HttpErrorException
*/
function parseForm(Request $request): Form
{
static $parser;
$parser ??= new FormParser();

return $parser->parseForm($request);
}

/**
* Parse the given content-type and returns the boundary if parsing is supported,
* an empty string content-type is url-encoded mode or null if not supported.
Expand Down
7 changes: 3 additions & 4 deletions test/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Amp\Http\Server\Response;
use Amp\PHPUnit\AsyncTestCase;
use League\Uri\Http;
use function Amp\Http\Server\FormParser\parseForm;

class FormTest extends AsyncTestCase
{
Expand Down Expand Up @@ -55,7 +54,7 @@ public function testWwwFormUrlencoded(): void
$handler = new ClosureRequestHandler(function (Request $request) use ($callback): Response {
$callback();

$form = parseForm($request);
$form = Form::fromRequest($request);

$this->assertSame('bar', $form->getValue('foo'));
$this->assertSame('y', $form->getValue('x'));
Expand All @@ -73,7 +72,7 @@ public function testWwwFormUrlencoded(): void
public function testNonForm(): void
{
$handler = new ClosureRequestHandler(function (Request $request): Response {
parseForm($request);
Form::fromRequest($request);

$this->assertTrue($request->hasAttribute(Form::class)); // attribute is set either way
$this->assertSame('{}', $request->getBody()->buffer());
Expand All @@ -93,7 +92,7 @@ public function testNone(): void
$handler = new ClosureRequestHandler(function (Request $request): Response {
$this->assertFalse($request->hasAttribute(Form::class));

$form = parseForm($request);
$form = Form::fromRequest($request);
self::assertSame([], $form->getNames());

$this->assertTrue($request->hasAttribute(Form::class)); // attribute is set either way
Expand Down

0 comments on commit 62bf983

Please sign in to comment.