Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure driver on resource attribute #731

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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/DependencyInjection/SyliusResourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private function autoRegisterResources(array &$config, ContainerBuilder $contain
'factory' => Factory::class,
'form' => DefaultResourceType::class,
],
'driver' => 'doctrine/orm',
'driver' => $resource->getDriver() ?? 'doctrine/orm',
loic425 marked this conversation as resolved.
Show resolved Hide resolved
];
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Bundle/Tests/Command/DebugResourceCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ classes [
denormalizationContext null
validationContext null
class null
driver null
------------------------ -------

New operations
Expand Down Expand Up @@ -246,6 +247,7 @@ classes [
denormalizationContext null
validationContext null
class null
driver null
------------------------ --------------

New operations
Expand Down
21 changes: 21 additions & 0 deletions src/Bundle/Tests/DependencyInjection/Dummy/NoDriverResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\ResourceBundle\Tests\DependencyInjection\Dummy;

use Sylius\Component\Resource\Metadata\Resource;

#[Resource(driver: false)]
final class NoDriverResource
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
use Sylius\Bundle\ResourceBundle\Tests\DependencyInjection\Dummy\BookWithAliasResource;
use Sylius\Bundle\ResourceBundle\Tests\DependencyInjection\Dummy\DummyResource;
use Sylius\Bundle\ResourceBundle\Tests\DependencyInjection\Dummy\NoDriverResource;
use Sylius\Component\Resource\Factory\Factory;

class SyliusResourceExtensionTest extends AbstractExtensionTestCase
Expand Down Expand Up @@ -176,6 +177,15 @@ public function it_auto_registers_resources(): void
],
'driver' => 'doctrine/orm',
],
'app.no_driver' => [
'classes' => [
'model' => NoDriverResource::class,
'controller' => ResourceController::class,
'factory' => Factory::class,
'form' => DefaultResourceType::class,
],
'driver' => false,
],
]);
}

Expand Down
14 changes: 14 additions & 0 deletions src/Component/Metadata/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(
protected ?array $denormalizationContext = null,
protected ?array $validationContext = null,
private ?string $class = null,
private string|false|null $driver = null,
?array $operations = null,
) {
$this->operations = null === $operations ? null : new Operations($operations);
Expand Down Expand Up @@ -246,4 +247,17 @@ public function withValidationContext(?array $validationContext): self

return $self;
}

public function getDriver(): false|string|null
{
return $this->driver;
}

public function withDriver(false|string $driver): self
{
$self = clone $this;
$self->driver = $driver;

return $self;
}
}
9 changes: 8 additions & 1 deletion src/Component/spec/Metadata/ResourceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,18 @@ function it_can_be_constructed_with_a_class(): void
$this->getClass()->shouldReturn('App\Resource');
}

function it_can_be_constructed_with_a_driver(): void
{
$this->beConstructedWith('app.book', null, null, null, null, null, null, null, null, null, null, null, null, 'doctrine/orm');

$this->getDriver()->shouldReturn('doctrine/orm');
}

function it_can_be_constructed_with_operations(): void
{
$operations = [new Create(), new Update()];

$this->beConstructedWith(null, null, null, null, null, null, null, null, null, null, null, null, null, $operations);
$this->beConstructedWith(null, null, null, null, null, null, null, null, null, null, null, null, null, null, $operations);

$this->getOperations()->shouldHaveCount(2);
}
Expand Down
5 changes: 0 additions & 5 deletions tests/Application/config/sylius/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ sylius_resource:
locale_provider: test.translation_locale_provider

resources:
app.board_game:
driver: false
classes:
model: App\BoardGameBlog\Infrastructure\Sylius\Resource\BoardGameResource

app.blog_post:
classes:
model: App\Entity\BlogPost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
formType: BoardGameType::class,
templatesDir: 'crud',
routePrefix: '/admin',
driver: false,
)]
#[Create(
processor: CreateBoardGameProcessor::class,
Expand Down