Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #193 from KnpLabs/fix/form-manager
Browse files Browse the repository at this point in the history
Remove request scope on FormManager and Form helper
  • Loading branch information
docteurklein committed Sep 4, 2014
2 parents 1955c0f + 758f742 commit 66adb06
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
21 changes: 13 additions & 8 deletions Form/FormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Knp\RadBundle\Form;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class FormManager
{
private $request;
private $requestStack;
private $creators;

public function __construct(Request $request)
public function __construct(RequestStack $requestStack)
{
$this->creators = new \SplPriorityQueue;
$this->request = $request;
$this->creators = new \SplPriorityQueue;
$this->requestStack = $requestStack;
}

public function createObjectForm($object, $purpose = null, array $options = array())
Expand All @@ -28,11 +28,11 @@ public function createObjectForm($object, $purpose = null, array $options = arra

public function createBoundObjectForm($object, $purpose = null, array $options = array())
{
if (!$this->request->isMethodSafe()) {
$options = array_merge(array('method' => $this->request->getMethod()), $options);
if (!$this->getRequest()->isMethodSafe()) {
$options = array_merge(array('method' => $this->getRequest()->getMethod()), $options);
}
$form = $this->createObjectForm($object, $purpose, $options);
$form->handleRequest($this->request);
$form->handleRequest($this->getRequest());

return $form;
}
Expand All @@ -46,4 +46,9 @@ public function getCreators()
{
return array_values(iterator_to_array(clone $this->creators));
}

private function getRequest()
{
return $this->requestStack->getCurrentRequest();
}
}
2 changes: 1 addition & 1 deletion Resources/config/controller_helper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<argument type="service" id="doctrine" />
<tag name="remove-when-missing" service="doctrine" />
</service>
<service id="knp_rad.controller.helper.form" class="%knp_rad.controller.helper.form.class%" scope="request">
<service id="knp_rad.controller.helper.form" class="%knp_rad.controller.helper.form.class%">
<argument type="service" id="knp_rad.form.manager" />
<tag name="remove-when-missing" service="form.factory" />
</service>
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/form_manager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</parameters>

<services>
<service id="knp_rad.form.manager" class="%knp_rad.form.manager.class%" scope="request">
<argument type="service" id="request" />
<service id="knp_rad.form.manager" class="%knp_rad.form.manager.class%">
<argument type="service" id="request_stack" />
<tag name="remove-when-missing" service="form.factory" />
</service>

Expand Down
8 changes: 5 additions & 3 deletions spec/Knp/RadBundle/Form/FormManagerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

namespace spec\Knp\RadBundle\Form;

use PhpSpec\ObjectBehavior;
use PhpSpec\Exception\Example\PendingException;
use PhpSpec\ObjectBehavior;

class FormManagerSpec extends ObjectBehavior
{
/**
* @param Symfony\Component\HttpFoundation\RequestStack $requestStack
* @param Symfony\Component\HttpFoundation\Request $request
* @param Knp\RadBundle\Form\FormCreatorInterface $creator1
* @param Knp\RadBundle\Form\FormCreatorInterface $creator2
* @param Knp\RadBundle\Form\FormCreatorInterface $creator3
*/
function let($request, $creator1, $creator2, $creator3)
function let($requestStack, $request, $creator1, $creator2, $creator3)
{
$this->beConstructedWith($request);
$this->beConstructedWith($requestStack);

$requestStack->getCurrentRequest()->willReturn($request);
$this->registerCreator($creator1, 2);
$this->registerCreator($creator2, 3);
$this->registerCreator($creator3, 1);
Expand Down

0 comments on commit 66adb06

Please sign in to comment.