Skip to content

Commit

Permalink
refactor #730 Grid aware operation (loic425)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.11 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Bug fix?        | yes (kind of)
| New feature?    | no
| BC breaks?      | no (yes on 1.11 alpha only)
| Deprecations?   | no
| Related tickets | 
| License         | MIT


Commits
-------

e014bb2 Grid aware operation
  • Loading branch information
lchrusciel authored Jul 10, 2023
2 parents 0d40366 + e014bb2 commit 66db48f
Show file tree
Hide file tree
Showing 22 changed files with 220 additions and 131 deletions.
7 changes: 7 additions & 0 deletions src/Bundle/Resources/config/services/metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
<argument type="service" id=".inner" />
</service>

<service id="sylius.resource_metadata_collection.factory.provider"
class="Sylius\Component\Resource\Metadata\Resource\Factory\ProviderResourceMetadataCollectionFactory"
decorates="sylius.resource_metadata_collection.factory"
>
<argument type="service" id=".inner" />
</service>

<service id="sylius.resource_metadata_collection.factory.resource_factory"
class="Sylius\Component\Resource\Metadata\Resource\Factory\FactoryResourceMetadataCollectionFactory"
decorates="sylius.resource_metadata_collection.factory"
Expand Down
1 change: 0 additions & 1 deletion src/Bundle/Tests/Command/DebugResourceCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ public function it_displays_the_metadata_for_given_resource_operation(): void
name "app_one_create"
repositoryMethod null
repositoryArguments null
grid null
read null
write null
validate null
Expand Down
5 changes: 5 additions & 0 deletions src/Component/Grid/State/RequestGridProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sylius\Component\Resource\Context\Context;
use Sylius\Component\Resource\Context\Option\RequestOption;
use Sylius\Component\Resource\Grid\View\Factory\GridViewFactoryInterface;
use Sylius\Component\Resource\Metadata\GridAwareOperationInterface;
use Sylius\Component\Resource\Metadata\Operation;
use Sylius\Component\Resource\State\ProviderInterface;

Expand All @@ -36,6 +37,10 @@ public function provide(Operation $operation, Context $context): object|iterable
throw new \LogicException('You can not use a grid if Sylius Grid Bundle is not available. Try running "composer require sylius/grid-bundle".');
}

if (!$operation instanceof GridAwareOperationInterface) {
throw new \LogicException(sprintf('You can not use a grid if your operation does not implement "%s".', GridAwareOperationInterface::class));
}

$grid = $operation->getGrid();

if (null === $grid) {
Expand Down
2 changes: 0 additions & 2 deletions src/Component/Metadata/Api/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(
string|callable|null $repository = null,
?string $repositoryMethod = null,
?array $repositoryArguments = null,
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand All @@ -61,7 +60,6 @@ public function __construct(
repository: $repository,
repositoryMethod: $repositoryMethod,
repositoryArguments: $repositoryArguments,
grid: $grid,
read: $read,
write: $write,
validate: $validate,
Expand Down
2 changes: 0 additions & 2 deletions src/Component/Metadata/Api/GetCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(
string|callable|null $repository = null,
?string $repositoryMethod = null,
?array $repositoryArguments = null,
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand All @@ -61,7 +60,6 @@ public function __construct(
repository: $repository,
repositoryMethod: $repositoryMethod,
repositoryArguments: $repositoryArguments,
grid: $grid,
read: $read,
write: $write,
validate: $validate,
Expand Down
2 changes: 0 additions & 2 deletions src/Component/Metadata/Api/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(
string|callable|null $repository = null,
?string $repositoryMethod = null,
?array $repositoryArguments = null,
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand All @@ -61,7 +60,6 @@ public function __construct(
repository: $repository,
repositoryMethod: $repositoryMethod,
repositoryArguments: $repositoryArguments,
grid: $grid,
read: $read,
write: $write,
validate: $validate,
Expand Down
2 changes: 0 additions & 2 deletions src/Component/Metadata/Api/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(
string|callable|null $repository = null,
?string $repositoryMethod = null,
?array $repositoryArguments = null,
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand All @@ -61,7 +60,6 @@ public function __construct(
repository: $repository,
repositoryMethod: $repositoryMethod,
repositoryArguments: $repositoryArguments,
grid: $grid,
read: $read,
write: $write,
validate: $validate,
Expand Down
2 changes: 0 additions & 2 deletions src/Component/Metadata/Api/Put.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(
string|callable|null $repository = null,
?string $repositoryMethod = null,
?array $repositoryArguments = null,
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand All @@ -61,7 +60,6 @@ public function __construct(
repository: $repository,
repositoryMethod: $repositoryMethod,
repositoryArguments: $repositoryArguments,
grid: $grid,
read: $read,
write: $write,
validate: $validate,
Expand Down
2 changes: 0 additions & 2 deletions src/Component/Metadata/ApplyStateMachineTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function __construct(
string|callable|null $responder = null,
string|callable|null $repository = null,
?string $repositoryMethod = null,
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand All @@ -54,7 +53,6 @@ public function __construct(
responder: $responder,
repository: $repository,
repositoryMethod: $repositoryMethod,
grid: $grid,
read: $read,
write: $write,
validate: $validate ?? false,
Expand Down
2 changes: 0 additions & 2 deletions src/Component/Metadata/BulkUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function __construct(
string|callable|null $repository = null,
?string $repositoryMethod = null,
?array $repositoryArguments = null,
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand Down Expand Up @@ -61,7 +60,6 @@ public function __construct(
repository: $repository,
repositoryMethod: $repositoryMethod,
repositoryArguments: $repositoryArguments,
grid: $grid,
read: $read,
write: $write,
validate: $validate,
Expand Down
2 changes: 0 additions & 2 deletions src/Component/Metadata/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function __construct(
string|callable|false|null $factory = null,
private ?string $factoryMethod = null,
private ?array $factoryArguments = [],
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand Down Expand Up @@ -68,7 +67,6 @@ public function __construct(
repository: $repository,
repositoryMethod: $repositoryMethod,
repositoryArguments: $repositoryArguments,
grid: $grid,
read: $read,
write: $write,
validate: $validate,
Expand Down
26 changes: 26 additions & 0 deletions src/Component/Metadata/GridAwareOperationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Resource\Metadata;

/**
* The Operation has a grid.
*
* @experimental
*/
interface GridAwareOperationInterface
{
public function getGrid(): ?string;

public function withGrid(string $grid): self;
}
2 changes: 0 additions & 2 deletions src/Component/Metadata/HttpOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function __construct(
string|callable|null $repository = null,
?string $repositoryMethod = null,
?array $repositoryArguments = null,
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand All @@ -61,7 +60,6 @@ public function __construct(
repository: $repository,
repositoryMethod: $repositoryMethod,
repositoryArguments: $repositoryArguments,
grid: $grid,
read: $read,
write: $write,
validate: $validate,
Expand Down
18 changes: 15 additions & 3 deletions src/Component/Metadata/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @experimental
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class Index extends HttpOperation implements CollectionOperationInterface
final class Index extends HttpOperation implements CollectionOperationInterface, GridAwareOperationInterface
{
public function __construct(
?array $methods = null,
Expand All @@ -32,7 +32,6 @@ public function __construct(
string|callable|null $repository = null,
?string $repositoryMethod = null,
?array $repositoryArguments = null,
?string $grid = null,
?bool $read = null,
?bool $write = null,
?bool $validate = null,
Expand All @@ -44,6 +43,7 @@ public function __construct(
?array $validationContext = null,
string|callable|null $twigContextFactory = null,
?string $redirectToRoute = null,
private ?string $grid = null,
) {
parent::__construct(
methods: $methods ?? ['GET'],
Expand All @@ -58,7 +58,6 @@ public function __construct(
repository: $repository,
repositoryMethod: $repositoryMethod,
repositoryArguments: $repositoryArguments,
grid: $grid,
read: $read,
write: $write,
validate: $validate,
Expand All @@ -72,4 +71,17 @@ public function __construct(
redirectToRoute: $redirectToRoute,
);
}

public function getGrid(): ?string
{
return $this->grid;
}

public function withGrid(string $grid): self
{
$self = clone $this;
$self->grid = $grid;

return $self;
}
}
14 changes: 0 additions & 14 deletions src/Component/Metadata/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function __construct(
string|callable|null $repository = null,
protected ?string $repositoryMethod = null,
protected ?array $repositoryArguments = null,
protected ?string $grid = null,
protected ?bool $read = null,
protected ?bool $write = null,
protected ?bool $validate = null,
Expand Down Expand Up @@ -191,19 +190,6 @@ public function withRepositoryArguments(array $repositoryArguments): self
return $self;
}

public function getGrid(): ?string
{
return $this->grid;
}

public function withGrid(string $grid): self
{
$self = clone $this;
$self->grid = $grid;

return $self;
}

public function canRead(): ?bool
{
return $this->read;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Sylius\Component\Resource\Metadata\Resource\Factory;

use Sylius\Component\Resource\Grid\State\RequestGridProvider;
use Sylius\Component\Resource\Metadata\HttpOperation;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Sylius\Component\Resource\Metadata\Operation;
Expand All @@ -22,7 +21,6 @@
use Sylius\Component\Resource\Metadata\Resource as ResourceMetadata;
use Sylius\Component\Resource\Metadata\Resource\ResourceMetadataCollection;
use Sylius\Component\Resource\Reflection\ClassReflection;
use Sylius\Component\Resource\Symfony\Request\State\Provider;
use Sylius\Component\Resource\Symfony\Request\State\Responder;
use Sylius\Component\Resource\Symfony\Routing\Factory\OperationRouteNameFactory;

Expand Down Expand Up @@ -196,14 +194,6 @@ private function getOperationWithDefaults(ResourceMetadata $resource, Operation
$operation = $operation->withRouteName($routeName);
}

if (null === $operation->getProvider() && null !== $operation->getGrid()) {
$operation = $operation->withProvider(RequestGridProvider::class);
}

if (null === $operation->getProvider()) {
$operation = $operation->withProvider(Provider::class);
}

if (null === $operation->getResponder()) {
$operation = $operation->withResponder(Responder::class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Resource\Metadata\Resource\Factory;

use Sylius\Component\Resource\Grid\State\RequestGridProvider;
use Sylius\Component\Resource\Metadata\GridAwareOperationInterface;
use Sylius\Component\Resource\Metadata\Operation;
use Sylius\Component\Resource\Metadata\Operations;
use Sylius\Component\Resource\Metadata\Resource as ResourceMetadata;
use Sylius\Component\Resource\Metadata\Resource\ResourceMetadataCollection;
use Sylius\Component\Resource\Symfony\Request\State\Provider;

final class ProviderResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
public function __construct(
private ResourceMetadataCollectionFactoryInterface $decorated,
) {
}

public function create(string $resourceClass): ResourceMetadataCollection
{
$resourceCollectionMetadata = $this->decorated->create($resourceClass);

/** @var ResourceMetadata $resource */
foreach ($resourceCollectionMetadata->getIterator() as $i => $resource) {
$operations = $resource->getOperations() ?? new Operations();

/** @var Operation $operation */
foreach ($operations as $operation) {
/** @var string $key */
$key = $operation->getName();

$operations->add($key, $this->addDefaults($operation));
}

$resource = $resource->withOperations($operations);

$resourceCollectionMetadata[$i] = $resource;
}

return $resourceCollectionMetadata;
}

private function addDefaults(Operation $operation): Operation
{
if (
null === $operation->getProvider() &&
$operation instanceof GridAwareOperationInterface &&
null !== $operation->getGrid()
) {
$operation = $operation->withProvider(RequestGridProvider::class);
}

if (null === $operation->getProvider()) {
$operation = $operation->withProvider(Provider::class);
}

return $operation;
}
}
Loading

0 comments on commit 66db48f

Please sign in to comment.