Skip to content

Commit d404af5

Browse files
author
Massimiliano Braglia
committed
Added AutoSubmitRequestHandler
1 parent 2f23846 commit d404af5

File tree

7 files changed

+232
-0
lines changed

7 files changed

+232
-0
lines changed

src/ApiPlatformBundle.php

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

33
namespace Fazland\ApiPlatformBundle;
44

5+
use Fazland\ApiPlatformBundle\DependencyInjection\CompilerPass\OverrideDefaultRequestHandlerPass;
56
use Fazland\ApiPlatformBundle\DependencyInjection\CompilerPass\RegisterDecoders;
67
use Symfony\Component\DependencyInjection\ContainerBuilder;
78
use Symfony\Component\HttpKernel\Bundle\Bundle;
@@ -15,6 +16,7 @@ public function build(ContainerBuilder $container): void
1516
{
1617
$container
1718
->addCompilerPass(new RegisterDecoders())
19+
->addCompilerPass(new OverrideDefaultRequestHandlerPass())
1820
;
1921
}
2022
}

src/DependencyInjection/ApiPlatformExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function load(array $configs, ContainerBuilder $container): void
4949
}
5050

5151
$container->setParameter('fazland_api.response_charset', $config['response_charset']);
52+
$container->setParameter('fazland_api.auto_submit_request_handler_is_enabled', $config['auto_submit_request_handler']['enabled']);
5253
}
5354

5455
/**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fazland\ApiPlatformBundle\DependencyInjection\CompilerPass;
4+
5+
use Fazland\ApiPlatformBundle\Form\Extension\AutoSubmitRequestHandler;
6+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
9+
final class OverrideDefaultRequestHandlerPass implements CompilerPassInterface
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public function process(ContainerBuilder $container): void
15+
{
16+
$isAutoSubmitRequestHandlerEnabled = $container->getParameter('fazland_api.auto_submit_request_handler_is_enabled');
17+
if (! $isAutoSubmitRequestHandlerEnabled) {
18+
return;
19+
}
20+
21+
$container->findDefinition('form.type_extension.form.request_handler')
22+
->setClass(AutoSubmitRequestHandler::class)
23+
;
24+
}
25+
}

src/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function getConfigTreeBuilder(): TreeBuilder
4747
->scalarNode('response_charset')
4848
->defaultValue('UTF-8')
4949
->end()
50+
->arrayNode('auto_submit_request_handler')
51+
->canBeDisabled()
52+
->end()
5053
->end()
5154
;
5255

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fazland\ApiPlatformBundle\Form\Extension;
4+
5+
use Symfony\Component\Form\Exception\UnexpectedTypeException;
6+
use Symfony\Component\Form\FormError;
7+
use Symfony\Component\Form\FormInterface;
8+
use Symfony\Component\Form\RequestHandlerInterface;
9+
use Symfony\Component\Form\Util\ServerParams;
10+
use Symfony\Component\HttpFoundation\File\File;
11+
use Symfony\Component\HttpFoundation\Request;
12+
13+
final class AutoSubmitRequestHandler implements RequestHandlerInterface
14+
{
15+
/**
16+
* @var ServerParams
17+
*/
18+
private $serverParams;
19+
20+
public function __construct(?ServerParams $serverParams = null)
21+
{
22+
$this->serverParams = $serverParams ?? new ServerParams();
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function handleRequest(FormInterface $form, $request = null): void
29+
{
30+
if (! $request instanceof Request) {
31+
throw new UnexpectedTypeException($request, Request::class);
32+
}
33+
34+
$name = $form->getName();
35+
$method = $form->getConfig()->getMethod();
36+
37+
if ($request->getMethod() !== $method) {
38+
return;
39+
}
40+
41+
// For request methods that must not have a request body we fetch data
42+
// from the query string. Otherwise we look for data in the request body.
43+
if (Request::METHOD_GET === $method || Request::METHOD_HEAD === $method || Request::METHOD_TRACE === $method) {
44+
if ('' === $name) {
45+
$data = $request->query->all();
46+
} else {
47+
// Don't submit GET requests if the form's name does not exist
48+
// in the request
49+
if (! $request->query->has($name)) {
50+
return;
51+
}
52+
53+
$data = $request->query->get($name);
54+
}
55+
} else {
56+
// Mark the form with an error if the uploaded size was too large
57+
// This is done here and not in FormValidator because $_POST is
58+
// empty when that error occurs. Hence the form is never submitted.
59+
if ($this->serverParams->hasPostMaxSizeBeenExceeded()) {
60+
// Submit the form, but don't clear the default values
61+
$form->submit(null, false);
62+
63+
$form->addError(new FormError(
64+
\call_user_func($form->getConfig()->getOption('upload_max_size_message')),
65+
null,
66+
['{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()]
67+
));
68+
69+
return;
70+
}
71+
72+
if ('' === $name) {
73+
$params = $request->request->all();
74+
$files = $request->files->all();
75+
} elseif ($request->request->has($name) || $request->files->has($name)) {
76+
$default = $form->getConfig()->getCompound() ? [] : null;
77+
$params = $request->request->get($name, $default);
78+
$files = $request->files->get($name, $default);
79+
} else {
80+
// Don't submit the form if it is not present in the request
81+
return;
82+
}
83+
84+
if (\is_array($params) && \is_array($files)) {
85+
$data = \array_replace_recursive($params, $files);
86+
} else {
87+
$data = $params ?: $files;
88+
}
89+
}
90+
91+
$form->submit($data, Request::METHOD_PATCH !== $method);
92+
}
93+
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
public function isFileUpload($data): bool
98+
{
99+
return $data instanceof File;
100+
}
101+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fazland\ApiPlatformBundle\Tests\DependencyInjection\CompilerPass;
4+
5+
use Fazland\ApiPlatformBundle\DependencyInjection\CompilerPass\OverrideDefaultRequestHandlerPass;
6+
use Fazland\ApiPlatformBundle\Form\Extension\AutoSubmitRequestHandler;
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\Prophecy\ObjectProphecy;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\DependencyInjection\Definition;
11+
12+
class OverrideDefaultRequestHandlerPassTest extends TestCase
13+
{
14+
/**
15+
* @var ContainerBuilder|ObjectProphecy
16+
*/
17+
private $container;
18+
19+
/**
20+
* @var OverrideDefaultRequestHandlerPass
21+
*/
22+
private $compilerPass;
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
protected function setUp(): void
28+
{
29+
$this->container = $this->prophesize(ContainerBuilder::class);
30+
$this->compilerPass = new OverrideDefaultRequestHandlerPass();
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function testProcessShouldNotActIfAutoSubmitRequestHandlerIsDisabled(): void
37+
{
38+
$this->container->getParameter('fazland_api.auto_submit_request_handler_is_enabled')->willReturn(false);
39+
$this->container->findDefinition('form.type_extension.form.request_handler')->shouldNotBeCalled();
40+
41+
$this->compilerPass->process($this->container->reveal());
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function testProcessShouldOverrideDefaultRequestHandlerWithAutoSubmitRequestHandler(): void
48+
{
49+
$definition = $this->prophesize(Definition::class);
50+
51+
$this->container->getParameter('fazland_api.auto_submit_request_handler_is_enabled')->willReturn(true);
52+
$this->container->findDefinition('form.type_extension.form.request_handler')->willReturn($definition);
53+
$definition->setClass(AutoSubmitRequestHandler::class)->shouldBeCalled();
54+
55+
$this->compilerPass->process($this->container->reveal());
56+
}
57+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Fazland\ApiPlatformBundle\Tests\Form\Extension;
4+
5+
use Fazland\ApiPlatformBundle\Form\Extension\AutoSubmitRequestHandler;
6+
use Symfony\Component\Form\RequestHandlerInterface;
7+
use Symfony\Component\Form\Tests\Extension\HttpFoundation\HttpFoundationRequestHandlerTest;
8+
use Symfony\Component\HttpFoundation\Request;
9+
10+
class AutoSubmitRequestHandlerTest extends HttpFoundationRequestHandlerTest
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
protected function getRequestHandler(): RequestHandlerInterface
16+
{
17+
return new AutoSubmitRequestHandler($this->serverParams);
18+
}
19+
20+
/**
21+
* @dataProvider methodProvider
22+
*/
23+
public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method): void
24+
{
25+
$form = $this->getMockForm('', $method);
26+
$form->expects(self::any())
27+
->method('all')
28+
->will(self::returnValue([
29+
'param1' => $this->getMockForm('param1'),
30+
'param2' => $this->getMockForm('param2'),
31+
]))
32+
;
33+
34+
$this->setRequestData($method, ['paramx' => 'submitted value']);
35+
36+
$form->expects(self::once())
37+
->method('submit')
38+
->with(['paramx' => 'submitted value'], Request::METHOD_PATCH !== $method)
39+
;
40+
41+
$this->requestHandler->handleRequest($form, $this->request);
42+
}
43+
}

0 commit comments

Comments
 (0)