Skip to content

Commit

Permalink
fix grid sorting with sorting key not defined and refactor grid defin…
Browse files Browse the repository at this point in the history
…ition creation
  • Loading branch information
Florian-Merle committed Nov 27, 2024
1 parent 0f305fe commit 42ea1d8
Show file tree
Hide file tree
Showing 22 changed files with 620 additions and 228 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<testsuite name="SyliusGridBundle Test Suite">
<directory>./tests/</directory>
<directory>./src/Bundle/Tests/</directory>
<directory>./src/Component/Tests/</directory>
</testsuite>
</testsuites>
</phpunit>
46 changes: 46 additions & 0 deletions src/Bundle/Factory/GridConfigurationExtenderListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Bundle\GridBundle\Factory;

use Sylius\Bundle\GridBundle\Registry\GridRegistryInterface;
use Sylius\Component\Grid\Configuration\GridConfigurationExtenderInterface;
use Sylius\Component\Grid\Factory\GridConfigurationProcessEvent;
use Webmozart\Assert\Assert;

final class GridConfigurationExtenderListener
{
public function __construct(
private GridRegistryInterface $gridRegistry,
private array $gridConfigurations,
private GridConfigurationExtenderInterface $gridConfigurationExtender,
) {
}

public function __invoke(GridConfigurationProcessEvent $event): void
{
$gridConfiguration = $event->getGridConfiguration();
$parentGridCode = $gridConfiguration['extends'] ?? null;

if (null === $parentGridCode) {
return;
}

$parentGridConfiguration = $this->gridRegistry->getGrid($parentGridCode)?->toArray() ?? $this->gridConfigurations[$parentGridCode] ?? null;
Assert::notNull($parentGridConfiguration, sprintf('Parent grid with code "%s" does not exists.', $gridConfiguration['extends']));

$gridConfiguration = $this->gridConfigurationExtender->extends($gridConfiguration, $parentGridConfiguration);

$event->setGridConfiguration($gridConfiguration);
}
}
41 changes: 5 additions & 36 deletions src/Bundle/Provider/ServiceGridProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,23 @@

use Sylius\Bundle\GridBundle\Grid\GridInterface;
use Sylius\Bundle\GridBundle\Registry\GridRegistryInterface;
use Sylius\Component\Grid\Configuration\GridConfigurationExtenderInterface;
use Sylius\Component\Grid\Configuration\GridConfigurationRemovalsHandler;
use Sylius\Component\Grid\Configuration\GridConfigurationRemovalsHandlerInterface;
use Sylius\Component\Grid\Definition\ArrayToDefinitionConverterInterface;
use Sylius\Component\Grid\Definition\Grid;
use Sylius\Component\Grid\Exception\UndefinedGridException;
use Sylius\Component\Grid\Factory\GridFactoryInterface;
use Sylius\Component\Grid\Provider\GridProviderInterface;
use Webmozart\Assert\Assert;

final class ServiceGridProvider implements GridProviderInterface
{
private ArrayToDefinitionConverterInterface $converter;

private GridRegistryInterface $gridRegistry;

private GridConfigurationExtenderInterface $gridConfigurationExtender;

private GridConfigurationRemovalsHandlerInterface $gridConfigurationRemovalsHandler;
private GridFactoryInterface $gridFactory;

public function __construct(
ArrayToDefinitionConverterInterface $converter,
GridRegistryInterface $gridRegistry,
GridConfigurationExtenderInterface $gridConfigurationExtender,
?GridConfigurationRemovalsHandlerInterface $gridConfigurationRemovalsHandler = null,
GridFactoryInterface $gridFactory,
) {
$this->converter = $converter;
$this->gridRegistry = $gridRegistry;
$this->gridConfigurationExtender = $gridConfigurationExtender;
$this->gridConfigurationRemovalsHandler = $gridConfigurationRemovalsHandler ?? new GridConfigurationRemovalsHandler();
$this->gridFactory = $gridFactory;
}

public function get(string $code): Grid
Expand All @@ -58,25 +46,6 @@ public function get(string $code): Grid
throw new UndefinedGridException($code);
}

$gridConfiguration = $grid->toArray();

if (isset($gridConfiguration['extends'])) {
$gridConfiguration = $this->extend($gridConfiguration, $gridConfiguration['extends']);
}

$gridConfiguration = $this->gridConfigurationRemovalsHandler->handle($gridConfiguration);

return $this->converter->convert($code, $gridConfiguration);
}

private function extend(array $gridConfiguration, string $parentGridCode): array
{
$parentGrid = $this->gridRegistry->getGrid($parentGridCode);

Assert::notNull($parentGrid, sprintf('Parent grid with code "%s" does not exists.', $parentGridCode));

$parentGridConfiguration = $parentGrid->toArray();

return $this->gridConfigurationExtender->extends($gridConfiguration, $parentGridConfiguration);
return $this->gridFactory->createGrid($code, $grid->toArray());
}
}
33 changes: 29 additions & 4 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,45 @@
<service id="sylius.grid.configuration_removals_handler" class="Sylius\Component\Grid\Configuration\GridConfigurationRemovalsHandler"/>
<service id="Sylius\Component\Grid\Configuration\GridConfigurationRemovalsHandlerInterface" alias="sylius.grid.configuration_removals_handler" />

<service id="sylius.grid.configuration_sorting_handler" class="Sylius\Component\Grid\Configuration\GridConfigurationSortingHandler"/>
<service id="Sylius\Component\Grid\Configuration\GridConfigurationSortingHandlerInterface" alias="sylius.grid.configuration_sorting_handler" />

<service id="sylius.grid.array_grid_provider" class="Sylius\Component\Grid\Provider\ArrayGridProvider">
<argument type="service" id="sylius.grid.array_to_definition_converter" />
<argument>%sylius.grids_definitions%</argument>
<argument type="service" id="sylius.grid.configuration_extender" />
<argument type="service" id="sylius.grid.configuration_removals_handler" />
<argument type="service" id="sylius.grid.grid_factory" />
<tag name="sylius.grid_provider" key="array" priority="-200"/>
</service>
<service id="Sylius\Component\Grid\Provider\ArrayGridProvider" alias="sylius.grid.array_grid_provider" />

<service id="sylius.grid.service_grid_provider" class="Sylius\Bundle\GridBundle\Provider\ServiceGridProvider">
<service id="sylius.grid.grid_factory" class="Sylius\Component\Grid\Factory\GridFactory">
<argument type="service" id="sylius.grid.array_to_definition_converter" />
<argument type="service" id="event_dispatcher" />
</service>
<service id="Sylius\Component\Grid\Factory\GridFactory" alias="sylius.grid.grid_factory" />

<service id="sylius.grid.grid_configuration_extender_listener" class="Sylius\Bundle\GridBundle\Factory\GridConfigurationExtenderListener">
<argument type="service" id="sylius.grid.grid_registry" />
<argument>%sylius.grids_definitions%</argument>
<argument type="service" id="sylius.grid.configuration_extender" />
<tag name="kernel.event_listener" priority="128" />
</service>
<service id="Sylius\Bundle\GridBundle\Factory\GridConfigurationExtenderListener" alias="sylius.grid.grid_configuration_extender_listener" />

<service id="sylius.grid.grid_configuration_removals_listener" class="Sylius\Component\Grid\Factory\GridConfigurationRemovalsListener">
<argument type="service" id="sylius.grid.configuration_removals_handler" />
<tag name="kernel.event_listener" priority="64" />
</service>
<service id="Sylius\Component\Grid\Factory\GridConfigurationRemovalsListener" alias="sylius.grid.grid_configuration_removals_listener" />

<service id="sylius.grid.grid_configuration_sorting_listener" class="Sylius\Component\Grid\Factory\GridConfigurationSortingListener">
<argument type="service" id="sylius.grid.configuration_sorting_handler" />
<tag name="kernel.event_listener" priority="64" />
</service>
<service id="Sylius\Component\Grid\Factory\GridConfigurationSortingListener" alias="sylius.grid.grid_configuration_sorting_listener" />

<service id="sylius.grid.service_grid_provider" class="Sylius\Bundle\GridBundle\Provider\ServiceGridProvider">
<argument type="service" id="sylius.grid.grid_registry" />
<argument type="service" id="sylius.grid.grid_factory" />
<tag name="sylius.grid_provider" key="service" priority="-100"/>
</service>
<service id="Sylius\Bundle\GridBundle\Provider\ServiceGridProvider" alias="sylius.grid.service_grid_provider" />
Expand Down
108 changes: 108 additions & 0 deletions src/Bundle/spec/Factory/GridConfigurationExtenderListenerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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 spec\Sylius\Bundle\GridBundle\Factory;

use PhpSpec\ObjectBehavior;
use PHPUnit\Framework\Assert;
use Sylius\Bundle\GridBundle\Grid\GridInterface;
use Sylius\Bundle\GridBundle\Registry\GridRegistryInterface;
use Sylius\Component\Grid\Configuration\GridConfigurationExtenderInterface;
use Sylius\Component\Grid\Factory\GridConfigurationProcessEvent;

final class GridConfigurationExtenderListenerSpec extends ObjectBehavior
{
function let(
GridRegistryInterface $gridRegistry,
GridConfigurationExtenderInterface $gridConfigurationExtender,
): void {
$this->beConstructedWith(
$gridRegistry,
[
'author' => ['foo' => 'foobar'],
],
$gridConfigurationExtender,
);
}

function it_does_not_update_grid(
GridRegistryInterface $gridRegistry,
GridConfigurationExtenderInterface $gridConfigurationExtender,
): void {
$event = new GridConfigurationProcessEvent('author', ['bar' => 'baz']);

$this->__invoke($event);

Assert::assertEquals($event->getGridConfiguration(), ['bar' => 'baz']);
}

function it_updates_grid_with_parent_from_grid_registry(
GridRegistryInterface $gridRegistry,
GridInterface $grid,
GridConfigurationExtenderInterface $gridConfigurationExtender,
): void {
$gridRegistry->getGrid('author')->willReturn($grid);
$grid->toArray()->willReturn([
'bar' => 'baz',
]);

$gridConfigurationExtender
->extends(['extends' => 'author'], ['bar' => 'baz'])
->willReturn([
'extends' => 'author',
'bar' => 'baz',
]);

$event = new GridConfigurationProcessEvent('author_with_books', [
'extends' => 'author',
]);

$this->__invoke($event);

Assert::assertEquals($event->getGridConfiguration(), [
'extends' => 'author',
'bar' => 'baz',
]);
}

function it_updates_grid_with_parent_from_grid_configurations_array(
GridConfigurationExtenderInterface $gridConfigurationExtender,
): void {
$gridConfigurationExtender
->extends(['extends' => 'author'], ['foo' => 'foobar'])
->willReturn([
'extends' => 'author',
'foo' => 'foobar',
]);

$event = new GridConfigurationProcessEvent('author_with_books', [
'extends' => 'author',
]);

$this->__invoke($event);

Assert::assertEquals($event->getGridConfiguration(), [
'extends' => 'author',
'foo' => 'foobar',
]);
}

function it_throws_an_invalid_argument_exception_when_parent_grid_is_not_found(): void
{
$event = new GridConfigurationProcessEvent('sylius_admin_book', [
'extends' => '404',
]);

$this->shouldThrow(\InvalidArgumentException::class)->during('__invoke', [$event]);
}
}
Loading

0 comments on commit 42ea1d8

Please sign in to comment.