Skip to content

Use plural name from new resource metadata #999

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

Open
wants to merge 1 commit into
base: 1.13
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ parameters:
- '/Parameter \#2 \$callback of function preg_replace_callback expects callable\(array\<int\|string\, string\>\): string, Closure\(array\)\: mixed given\./'
- '/Parameter \#1 \$objectOrArray of method Symfony\\Component\\PropertyAccess\\PropertyAccessorInterface\:\:getValue\(\) expects array\|object, array\|object\|null given\./'
- '/Parameter \#1 \$min \(0\) of function random_int expects lower number than parameter \#2 \$max \(int\<\-1, max\>\)\./'
- '/Property Sylius\\Resource\\Symfony\\Routing\\Factory\\AttributesOperationRouteFactory\:\:\$resourceRegistry is never read, only written\./'
- '/Method Sylius\\Resource\\Model\\ResourceInterface\:\:getId\(\) has no return type specified\./'
- '/Method Sylius\\Resource\\Model\\TimestampableInterface\:\:setCreatedAt\(\) has no return type specified\./'
- '/Method Sylius\\Resource\\Model\\TimestampableInterface\:\:setUpdatedAt\(\) has no return type specified\./'
Expand Down
1 change: 0 additions & 1 deletion src/Bundle/Resources/config/services/routing/resource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<service id="sylius.routing.resource.route_collection_factory" class="Sylius\Resource\Symfony\Routing\Factory\Resource\ResourceRouteCollectionFactory">
<argument type="service" id="sylius.routing.factory.operation_route" />
<argument type="service" id="sylius.resource_metadata_collection.factory" />
<argument type="service" id="sylius.resource_registry" />
</service>
<service id="Sylius\Resource\Symfony\Routing\Factory\Resource\ResourceRouteCollectionFactoryInterface" alias="sylius.routing.resource.route_collection_factory" />
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function let(
new AttributesResourceMetadataCollectionFactory(
$resourceRegistry->getWrappedObject(),
new OperationRouteNameFactory(),
'symfony',
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\HttpOperation;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Metadata;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;
Expand All @@ -43,15 +42,19 @@ function it_is_initializable(): void
function it_generates_create_routes(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new Create();
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new Create())
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('dummies/new')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata('app.dummy'),
$resource,
$operation,
);

Expand All @@ -68,16 +71,20 @@ function it_generates_create_routes(
function it_generates_index_routes(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new Index();
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new Index())
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('dummies')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata('app.dummy'),
new Index(),
$resource,
$operation,
);

$route->getPath()->shouldReturn('/dummies');
Expand All @@ -93,15 +100,19 @@ function it_generates_index_routes(
function it_generates_show_routes(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new Show();
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new Show())
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('dummies/{id}')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata('app.dummy'),
$resource,
$operation,
);

Expand All @@ -118,15 +129,19 @@ function it_generates_show_routes(
function it_generates_update_routes(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new Update();
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new Update())
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('dummies/{id}/edit')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata('app.dummy'),
$resource,
$operation,
);

Expand All @@ -143,15 +158,19 @@ function it_generates_update_routes(
function it_generates_delete_routes(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new Delete();
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new Delete())
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('dummies/{id}')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata('app.dummy'),
$resource,
$operation,
);

Expand All @@ -168,15 +187,19 @@ function it_generates_delete_routes(
function it_generates_bulk_delete_routes(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new BulkDelete();
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new BulkDelete())
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('dummies/bulk_delete')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata('app.dummy'),
$resource,
$operation,
);

Expand All @@ -193,15 +216,19 @@ function it_generates_bulk_delete_routes(
function it_generates_bulk_update_routes(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new BulkUpdate();
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new BulkUpdate())
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('dummies/bulk_update')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata('app.dummy'),
$resource,
$operation,
);

Expand All @@ -218,15 +245,19 @@ function it_generates_bulk_update_routes(
function it_generates_custom_operations_routes(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new HttpOperation(methods: ['PATCH'], path: 'dummies/{id}/custom');
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new HttpOperation(methods: ['PATCH'], path: 'dummies/{id}/custom'))
->withResource($resource)
;

$routePathFactory->createRoutePath(Argument::cetera())->willReturn('')->shouldNotBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata('app.dummy'),
$resource,
$operation,
);

Expand All @@ -243,15 +274,20 @@ function it_generates_custom_operations_routes(
function it_generates_routes_with_sections(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new Show();
$resource = (new ResourceMetadata())
->withSection('admin')
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new Show())
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('/dummies/{id}')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata(alias: 'app.dummy', section: 'admin'),
$resource,
$operation,
);

Expand All @@ -269,15 +305,19 @@ function it_generates_routes_with_sections(
function it_generates_routes_with_vars(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new Index(vars: ['subheader' => 'Managing your library']);
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new Index(vars: ['subheader' => 'Managing your library']))
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('/dummies')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata(alias: 'app.dummy'),
$resource,
$operation,
);

Expand All @@ -293,15 +333,19 @@ function it_generates_routes_with_vars(
function it_generates_routes_with_requirements(
OperationRoutePathFactoryInterface $routePathFactory,
): void {
$operation = new Index(routeRequirements: ['type' => 'country|province|zone']);
$resource = (new ResourceMetadata())
->withAlias('app.dummy')
->withPluralName('dummies')
;

$metadata = Metadata::fromAliasAndConfiguration('app.dummy', ['driver' => 'dummy_driver']);
$operation = (new Index(routeRequirements: ['type' => 'country|province|zone']))
->withResource($resource)
;

$routePathFactory->createRoutePath($operation, 'dummies')->willReturn('/dummies')->shouldBeCalled();

$route = $this->create(
$metadata,
new ResourceMetadata(alias: 'app.dummy'),
$resource,
$operation,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ private function getResourceWithDefaults(string $resourceClass, ResourceMetadata
$resource = $resource->withName($resourceConfiguration->getName());
}

if (null === $resource->getPluralName()) {
$resourcePluralName = $resourceConfiguration->getPluralName();

$resource = $resource->withPluralName($resourcePluralName);
}
Comment on lines +137 to +141
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be optimized a little.

Suggested change
if (null === $resource->getPluralName()) {
$resourcePluralName = $resourceConfiguration->getPluralName();
$resource = $resource->withPluralName($resourcePluralName);
}
if (null === $resource->getPluralName()) {
$resource = $resource->withPluralName(
$resourceConfiguration->getPluralName()
);
}


return $resource;
}

Expand All @@ -143,20 +149,6 @@ private function getOperationWithDefaults(ResourceMetadata $resource, Operation

$operation = $operation->withResource($resource);

if (null === $resource->getName()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question about line of code above this code.

        $resourceConfiguration = $this->resourceRegistry->get($resource->getAlias() ?? '');

We probably should assert it as Assert::notNull($resource->getAlias()); as was in some other PR/code.

$resourceName = $resourceConfiguration->getName();

$resource = $resource->withName($resourceName);
$operation = $operation->withResource($resource);
}

if (null === $resource->getPluralName()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess plural name is set somehow else, so this code is removed, but I don't fully understand how or where.

$resourcePluralName = $resourceConfiguration->getPluralName();

$resource = $resource->withPluralName($resourcePluralName);
$operation = $operation->withResource($resource);
}

if (null === $operation->getNormalizationContext()) {
$operation = $operation->withNormalizationContext($resource->getNormalizationContext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Sylius\Resource\Symfony\Routing\Factory;

use Sylius\Resource\Metadata\HttpOperation;
use Sylius\Resource\Metadata\MetadataInterface;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\RegistryInterface;
use Sylius\Resource\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
Expand Down Expand Up @@ -59,17 +58,16 @@ private function createRoutesForResource(RouteCollection $routeCollection, Resou

private function addRouteForOperation(RouteCollection $routeCollection, ResourceMetadata $resource, HttpOperation $operation): void
{
$metadata = $this->resourceRegistry->get($resource->getAlias() ?? '');
$routeName = $operation->getRouteName();

Assert::notNull($routeName, sprintf('Operation %s has no route name. Please define one.', $operation::class));

$route = $this->createRoute($metadata, $resource, $operation);
$route = $this->createRoute($resource, $operation);
$routeCollection->add($routeName, $route);
}

private function createRoute(MetadataInterface $metadata, ResourceMetadata $resource, HttpOperation $operation): Route
private function createRoute(ResourceMetadata $resource, HttpOperation $operation): Route
{
return $this->operationRouteFactory->create($metadata, $resource, $operation);
return $this->operationRouteFactory->create($resource, $operation);
}
}
Loading