Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Bundle/ResourceBundle/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private function find($action, $mandatory = true)
*/
private function buildForm($form = null, $object = null, array $options = [])
{
return $this->getFormFactory()->create($this->resource, $form, $object, $options);
return $this->getFormFactory()->create($form ?: $this->resource, $object, $options);
}

/**
Expand Down
16 changes: 11 additions & 5 deletions src/Bundle/ResourceBundle/Form/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface;
use Lug\Component\Resource\Model\ResourceInterface;
use Symfony\Component\Form\FormFactoryInterface as SymfonyFormFactoryInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\Test\FormInterface;

/**
* @author GeLo <geloen.eric@gmail.com>
Expand Down Expand Up @@ -41,15 +43,19 @@ public function __construct(SymfonyFormFactoryInterface $factory, ParameterResol
}

/**
* {@inheritdoc}
* @param string|FormTypeInterface|ResourceInterface $type
* @param mixed $data
* @param mixed[] $options
*
* @return FormInterface
*/
public function create(ResourceInterface $resource, $type = null, $data = null, array $options = [])
public function create($type = null, $data = null, array $options = [])
{
if ($type === null) {
$type = $this->parameterResolver->resolveForm($resource);
if ($type instanceof ResourceInterface) {
$type = $this->parameterResolver->resolveForm($type);
}

$validationGroups = $this->parameterResolver->resolveValidationGroups($resource);
$validationGroups = $this->parameterResolver->resolveValidationGroups();
$translationDomain = $this->parameterResolver->resolveTranslationDomain();

if (!empty($validationGroups)) {
Expand Down
9 changes: 4 additions & 5 deletions src/Bundle/ResourceBundle/Form/FormFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
interface FormFactoryInterface
{
/**
* @param ResourceInterface $resource
* @param string|FormTypeInterface|null $type
* @param mixed $data
* @param mixed[] $options
* @param string|FormTypeInterface|ResourceInterface $type
* @param mixed $data
* @param mixed[] $options
*
* @return FormInterface
*/
public function create(ResourceInterface $resource, $type = null, $data = null, array $options = []);
public function create($type = null, $data = null, array $options = []);
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function onView(ViewEvent $event)

if ($grid->getBatchForm() === null) {
$batchForm = !isset($data['batch_form']) || !$data['batch_form'] instanceof FormInterface
? $this->formFactory->create($event->getResource(), GridBatchType::class, null, ['grid' => $grid])
? $this->formFactory->create(GridBatchType::class, null, ['grid' => $grid])
: $data['batch_form'];

$grid->setBatchForm($batchForm->createView());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Lug\Bundle\ResourceBundle\Rest\View\EventSubscriber;

use JMS\Serializer\Exclusion\GroupsExclusionStrategy;
use Lug\Bundle\ResourceBundle\Rest\AbstractSubscriber;
use Lug\Bundle\ResourceBundle\Rest\RestEvents;
use Lug\Bundle\ResourceBundle\Rest\View\ViewEvent;
Expand All @@ -29,13 +30,15 @@ public function onApi(ViewEvent $event)
return;
}

$groups = $this->getParameterResolver()->resolveSerializerGroups($event->getResource());
$view = $event->getView();
$groups = $this->getParameterResolver()->resolveSerializerGroups();

if (!empty($groups)) {
$view->getContext()->addGroups($groups);
if (empty($groups)) {
$groups = [GroupsExclusionStrategy::DEFAULT_GROUP, 'lug.'.$event->getResource()->getName()];
}

$view = $event->getView();

$view->getContext()->addGroups($groups);
$view->getContext()->setSerializeNull($this->getParameterResolver()->resolveSerializerNull());
}

Expand Down
12 changes: 6 additions & 6 deletions src/Bundle/ResourceBundle/Routing/CachedParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ public function resolveRepositoryMethod($action)
/**
* {@inheritdoc}
*/
public function resolveSerializerGroups(ResourceInterface $resource)
public function resolveSerializerGroups()
{
return !isset($this->cache[$key = 'serializer_groups_'.spl_object_hash($resource)])
? $this->cache[$key] = $this->parameterResolver->resolveSerializerGroups($resource)
return !isset($this->cache[$key = 'serializer_groups'])
? $this->cache[$key] = $this->parameterResolver->resolveSerializerGroups()
: $this->cache[$key];
}

Expand Down Expand Up @@ -248,10 +248,10 @@ public function resolveTranslationDomain()
/**
* {@inheritdoc}
*/
public function resolveValidationGroups(ResourceInterface $resource)
public function resolveValidationGroups()
{
return !isset($this->cache[$key = 'validation_groups_'.spl_object_hash($resource)])
? $this->cache[$key] = $this->parameterResolver->resolveValidationGroups($resource)
return !isset($this->cache[$key = 'validation_groups'])
? $this->cache[$key] = $this->parameterResolver->resolveValidationGroups()
: $this->cache[$key];
}

Expand Down
13 changes: 4 additions & 9 deletions src/Bundle/ResourceBundle/Routing/ParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

namespace Lug\Bundle\ResourceBundle\Routing;

use JMS\Serializer\Exclusion\GroupsExclusionStrategy;
use Lug\Bundle\ResourceBundle\Exception\RequestNotFoundException;
use Lug\Bundle\ResourceBundle\Exception\RuntimeException;
use Lug\Component\Resource\Model\ResourceInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Validator\Constraint;

/**
* @author GeLo <geloen.eric@gmail.com>
Expand Down Expand Up @@ -233,12 +231,9 @@ public function resolveRepositoryMethod($action)
/**
* {@inheritdoc}
*/
public function resolveSerializerGroups(ResourceInterface $resource)
public function resolveSerializerGroups()
{
return $this->resolveParameter(
'serializer_groups',
[GroupsExclusionStrategy::DEFAULT_GROUP, 'lug.'.$resource->getName()]
);
return $this->resolveParameter('serializer_groups', []);
}

/**
Expand Down Expand Up @@ -300,9 +295,9 @@ public function resolveTranslationDomain()
/**
* {@inheritdoc}
*/
public function resolveValidationGroups(ResourceInterface $resource)
public function resolveValidationGroups()
{
return $this->resolveParameter('validation_groups', [Constraint::DEFAULT_GROUP, 'lug.'.$resource->getName()]);
return $this->resolveParameter('validation_groups', []);
}

/**
Expand Down
10 changes: 3 additions & 7 deletions src/Bundle/ResourceBundle/Routing/ParameterResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ public function resolveRedirectRouteParametersForward();
public function resolveRepositoryMethod($action);

/**
* @param ResourceInterface $resource
*
* @return string[]
*/
public function resolveSerializerGroups(ResourceInterface $resource);
public function resolveSerializerGroups();

/**
* @return bool
Expand Down Expand Up @@ -129,11 +127,9 @@ public function resolveThemes();
public function resolveTranslationDomain();

/**
* @param ResourceInterface $resource
*
* @return string|null
* @return string[]
*/
public function resolveValidationGroups(ResourceInterface $resource);
public function resolveValidationGroups();

/**
* @return bool
Expand Down
31 changes: 15 additions & 16 deletions src/Bundle/ResourceBundle/Tests/Form/FormFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,47 @@ public function testInheritance()
$this->assertInstanceOf(BundleFormFactoryInterface::class, $this->formFactory);
}

public function testCreateWithResourceType()
public function testCreateWithStringType()
{
$this->parameterResolver
->expects($this->once())
->method('resolveForm')
->with($this->identicalTo($resource = $this->createResourceMock()))
->will($this->returnValue($resourceType = 'resource_type'));

$this->symfonyFormFactory
->expects($this->once())
->method('create')
->with(
$this->identicalTo($resourceType),
$this->identicalTo($type = 'type'),
$this->identicalTo($data = 'data'),
$this->identicalTo($options = ['option'])
)
->will($this->returnValue($form = 'form'));

$this->assertSame($form, $this->formFactory->create($resource, null, $data, $options));
$this->assertSame($form, $this->formFactory->create($type, $data, $options));
}

public function testCreateWithStringType()
public function testCreateWithResourceType()
{
$this->parameterResolver
->expects($this->once())
->method('resolveForm')
->with($this->identicalTo($type = $this->createResourceMock()))
->will($this->returnValue($resourceType = 'resource_type'));

$this->symfonyFormFactory
->expects($this->once())
->method('create')
->with(
$this->identicalTo($type = 'type'),
$this->identicalTo($resourceType),
$this->identicalTo($data = 'data'),
$this->identicalTo($options = ['option'])
)
->will($this->returnValue($form = 'form'));

$this->assertSame($form, $this->formFactory->create($this->createResourceMock(), $type, $data, $options));
$this->assertSame($form, $this->formFactory->create($type, $data, $options));
}

public function testCreateWithValidationGroups()
{
$this->parameterResolver
->expects($this->once())
->method('resolveValidationGroups')
->with($this->identicalTo($resource = $this->createResourceMock()))
->will($this->returnValue($groups = ['group']));

$this->symfonyFormFactory
Expand All @@ -107,7 +106,7 @@ public function testCreateWithValidationGroups()
)
->will($this->returnValue($form = 'form'));

$this->assertSame($form, $this->formFactory->create($resource, $type, $data, $options));
$this->assertSame($form, $this->formFactory->create($type, $data, $options));
}

public function testCreateWithTranslationDomain()
Expand All @@ -127,7 +126,7 @@ public function testCreateWithTranslationDomain()
)
->will($this->returnValue($form = 'form'));

$this->assertSame($form, $this->formFactory->create($this->createResourceMock(), $type, $data, $options));
$this->assertSame($form, $this->formFactory->create($type, $data, $options));
}

public function testCreateWithApi()
Expand All @@ -147,7 +146,7 @@ public function testCreateWithApi()
)
->will($this->returnValue($form = 'form'));

$this->assertSame($form, $this->formFactory->create($this->createResourceMock(), $type, $data, $options));
$this->assertSame($form, $this->formFactory->create($type, $data, $options));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Lug\Bundle\ResourceBundle\Rest\View\EventSubscriber\GridViewSubscriber;
use Lug\Bundle\ResourceBundle\Rest\View\ViewEvent;
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface;
use Lug\Component\Resource\Model\ResourceInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormRendererInterface;
Expand Down Expand Up @@ -202,11 +201,6 @@ public function testViewWithoutBatchForm()
->method('getView')
->will($this->returnValue($view = $this->createViewMock()));

$event
->expects($this->once())
->method('getResource')
->will($this->returnValue($resource = $this->createResourceMock()));

$view
->expects($this->once())
->method('getData')
Expand All @@ -216,7 +210,6 @@ public function testViewWithoutBatchForm()
->expects($this->once())
->method('create')
->with(
$this->identicalTo($resource),
$this->identicalTo(GridBatchType::class),
$this->isNull(),
$this->identicalTo(['grid' => $gridView])
Expand Down Expand Up @@ -394,14 +387,6 @@ private function createViewEventMock()
->getMock();
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface
*/
private function createResourceMock()
{
return $this->getMock(ResourceInterface::class);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|View
*/
Expand Down
Loading