Skip to content

Commit 899baef

Browse files
committed
Use plural name from new resource metadata
1 parent 4296466 commit 899baef

File tree

11 files changed

+108
-80
lines changed

11 files changed

+108
-80
lines changed

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ parameters:
8787
- '/Parameter \#2 \$callback of function preg_replace_callback expects callable\(array\<int\|string\, string\>\): string, Closure\(array\)\: mixed given\./'
8888
- '/Parameter \#1 \$objectOrArray of method Symfony\\Component\\PropertyAccess\\PropertyAccessorInterface\:\:getValue\(\) expects array\|object, array\|object\|null given\./'
8989
- '/Parameter \#1 \$min \(0\) of function random_int expects lower number than parameter \#2 \$max \(int\<\-1, max\>\)\./'
90+
- '/Property Sylius\\Resource\\Symfony\\Routing\\Factory\\AttributesOperationRouteFactory\:\:\$resourceRegistry is never read, only written\./'
9091
- '/Method Sylius\\Resource\\Model\\ResourceInterface\:\:getId\(\) has no return type specified\./'
9192
- '/Method Sylius\\Resource\\Model\\TimestampableInterface\:\:setCreatedAt\(\) has no return type specified\./'
9293
- '/Method Sylius\\Resource\\Model\\TimestampableInterface\:\:setUpdatedAt\(\) has no return type specified\./'

src/Bundle/Resources/config/services/routing/resource.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<service id="sylius.routing.resource.route_collection_factory" class="Sylius\Resource\Symfony\Routing\Factory\Resource\ResourceRouteCollectionFactory">
1717
<argument type="service" id="sylius.routing.factory.operation_route" />
1818
<argument type="service" id="sylius.resource_metadata_collection.factory" />
19-
<argument type="service" id="sylius.resource_registry" />
2019
</service>
2120
<service id="Sylius\Resource\Symfony\Routing\Factory\Resource\ResourceRouteCollectionFactoryInterface" alias="sylius.routing.resource.route_collection_factory" />
2221
</services>

src/Component/spec/Symfony/Routing/Factory/AttributesOperationRouteFactorySpec.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ function let(
4242
new AttributesResourceMetadataCollectionFactory(
4343
$resourceRegistry->getWrappedObject(),
4444
new OperationRouteNameFactory(),
45-
'symfony',
4645
),
4746
);
4847
}

src/Component/spec/Symfony/Routing/Factory/OperationRouteFactorySpec.php

Lines changed: 82 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Sylius\Resource\Metadata\Delete;
2222
use Sylius\Resource\Metadata\HttpOperation;
2323
use Sylius\Resource\Metadata\Index;
24-
use Sylius\Resource\Metadata\Metadata;
2524
use Sylius\Resource\Metadata\ResourceMetadata;
2625
use Sylius\Resource\Metadata\Show;
2726
use Sylius\Resource\Metadata\Update;
@@ -43,15 +42,19 @@ function it_is_initializable(): void
4342
function it_generates_create_routes(
4443
OperationRoutePathFactoryInterface $routePathFactory,
4544
): void {
46-
$operation = new Create();
45+
$resource = (new ResourceMetadata())
46+
->withAlias('app.dummy')
47+
->withPluralName('dummies')
48+
;
4749

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

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

5256
$route = $this->create(
53-
$metadata,
54-
new ResourceMetadata('app.dummy'),
57+
$resource,
5558
$operation,
5659
);
5760

@@ -68,16 +71,20 @@ function it_generates_create_routes(
6871
function it_generates_index_routes(
6972
OperationRoutePathFactoryInterface $routePathFactory,
7073
): void {
71-
$operation = new Index();
74+
$resource = (new ResourceMetadata())
75+
->withAlias('app.dummy')
76+
->withPluralName('dummies')
77+
;
7278

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

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

7785
$route = $this->create(
78-
$metadata,
79-
new ResourceMetadata('app.dummy'),
80-
new Index(),
86+
$resource,
87+
$operation,
8188
);
8289

8390
$route->getPath()->shouldReturn('/dummies');
@@ -93,15 +100,19 @@ function it_generates_index_routes(
93100
function it_generates_show_routes(
94101
OperationRoutePathFactoryInterface $routePathFactory,
95102
): void {
96-
$operation = new Show();
103+
$resource = (new ResourceMetadata())
104+
->withAlias('app.dummy')
105+
->withPluralName('dummies')
106+
;
97107

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

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

102114
$route = $this->create(
103-
$metadata,
104-
new ResourceMetadata('app.dummy'),
115+
$resource,
105116
$operation,
106117
);
107118

@@ -118,15 +129,19 @@ function it_generates_show_routes(
118129
function it_generates_update_routes(
119130
OperationRoutePathFactoryInterface $routePathFactory,
120131
): void {
121-
$operation = new Update();
132+
$resource = (new ResourceMetadata())
133+
->withAlias('app.dummy')
134+
->withPluralName('dummies')
135+
;
122136

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

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

127143
$route = $this->create(
128-
$metadata,
129-
new ResourceMetadata('app.dummy'),
144+
$resource,
130145
$operation,
131146
);
132147

@@ -143,15 +158,19 @@ function it_generates_update_routes(
143158
function it_generates_delete_routes(
144159
OperationRoutePathFactoryInterface $routePathFactory,
145160
): void {
146-
$operation = new Delete();
161+
$resource = (new ResourceMetadata())
162+
->withAlias('app.dummy')
163+
->withPluralName('dummies')
164+
;
147165

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

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

152172
$route = $this->create(
153-
$metadata,
154-
new ResourceMetadata('app.dummy'),
173+
$resource,
155174
$operation,
156175
);
157176

@@ -168,15 +187,19 @@ function it_generates_delete_routes(
168187
function it_generates_bulk_delete_routes(
169188
OperationRoutePathFactoryInterface $routePathFactory,
170189
): void {
171-
$operation = new BulkDelete();
190+
$resource = (new ResourceMetadata())
191+
->withAlias('app.dummy')
192+
->withPluralName('dummies')
193+
;
172194

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

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

177201
$route = $this->create(
178-
$metadata,
179-
new ResourceMetadata('app.dummy'),
202+
$resource,
180203
$operation,
181204
);
182205

@@ -193,15 +216,19 @@ function it_generates_bulk_delete_routes(
193216
function it_generates_bulk_update_routes(
194217
OperationRoutePathFactoryInterface $routePathFactory,
195218
): void {
196-
$operation = new BulkUpdate();
219+
$resource = (new ResourceMetadata())
220+
->withAlias('app.dummy')
221+
->withPluralName('dummies')
222+
;
197223

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

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

202230
$route = $this->create(
203-
$metadata,
204-
new ResourceMetadata('app.dummy'),
231+
$resource,
205232
$operation,
206233
);
207234

@@ -218,15 +245,19 @@ function it_generates_bulk_update_routes(
218245
function it_generates_custom_operations_routes(
219246
OperationRoutePathFactoryInterface $routePathFactory,
220247
): void {
221-
$operation = new HttpOperation(methods: ['PATCH'], path: 'dummies/{id}/custom');
248+
$resource = (new ResourceMetadata())
249+
->withAlias('app.dummy')
250+
->withPluralName('dummies')
251+
;
222252

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

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

227259
$route = $this->create(
228-
$metadata,
229-
new ResourceMetadata('app.dummy'),
260+
$resource,
230261
$operation,
231262
);
232263

@@ -243,15 +274,20 @@ function it_generates_custom_operations_routes(
243274
function it_generates_routes_with_sections(
244275
OperationRoutePathFactoryInterface $routePathFactory,
245276
): void {
246-
$operation = new Show();
277+
$resource = (new ResourceMetadata())
278+
->withSection('admin')
279+
->withAlias('app.dummy')
280+
->withPluralName('dummies')
281+
;
247282

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

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

252289
$route = $this->create(
253-
$metadata,
254-
new ResourceMetadata(alias: 'app.dummy', section: 'admin'),
290+
$resource,
255291
$operation,
256292
);
257293

@@ -269,15 +305,19 @@ function it_generates_routes_with_sections(
269305
function it_generates_routes_with_vars(
270306
OperationRoutePathFactoryInterface $routePathFactory,
271307
): void {
272-
$operation = new Index(vars: ['subheader' => 'Managing your library']);
308+
$resource = (new ResourceMetadata())
309+
->withAlias('app.dummy')
310+
->withPluralName('dummies')
311+
;
273312

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

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

278319
$route = $this->create(
279-
$metadata,
280-
new ResourceMetadata(alias: 'app.dummy'),
320+
$resource,
281321
$operation,
282322
);
283323

src/Component/src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ private function getResourceWithDefaults(string $resourceClass, ResourceMetadata
134134
$resource = $resource->withName($resourceConfiguration->getName());
135135
}
136136

137+
if (null === $resource->getPluralName()) {
138+
$resourcePluralName = $resourceConfiguration->getPluralName();
139+
140+
$resource = $resource->withPluralName($resourcePluralName);
141+
}
142+
137143
return $resource;
138144
}
139145

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

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

146-
if (null === $resource->getName()) {
147-
$resourceName = $resourceConfiguration->getName();
148-
149-
$resource = $resource->withName($resourceName);
150-
$operation = $operation->withResource($resource);
151-
}
152-
153-
if (null === $resource->getPluralName()) {
154-
$resourcePluralName = $resourceConfiguration->getPluralName();
155-
156-
$resource = $resource->withPluralName($resourcePluralName);
157-
$operation = $operation->withResource($resource);
158-
}
159-
160152
if (null === $operation->getNormalizationContext()) {
161153
$operation = $operation->withNormalizationContext($resource->getNormalizationContext());
162154
}

src/Component/src/Symfony/Routing/Factory/AttributesOperationRouteFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Sylius\Resource\Symfony\Routing\Factory;
1515

1616
use Sylius\Resource\Metadata\HttpOperation;
17-
use Sylius\Resource\Metadata\MetadataInterface;
1817
use Sylius\Resource\Metadata\Operations;
1918
use Sylius\Resource\Metadata\RegistryInterface;
2019
use Sylius\Resource\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
@@ -59,17 +58,16 @@ private function createRoutesForResource(RouteCollection $routeCollection, Resou
5958

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

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

67-
$route = $this->createRoute($metadata, $resource, $operation);
65+
$route = $this->createRoute($resource, $operation);
6866
$routeCollection->add($routeName, $route);
6967
}
7068

71-
private function createRoute(MetadataInterface $metadata, ResourceMetadata $resource, HttpOperation $operation): Route
69+
private function createRoute(ResourceMetadata $resource, HttpOperation $operation): Route
7270
{
73-
return $this->operationRouteFactory->create($metadata, $resource, $operation);
71+
return $this->operationRouteFactory->create($resource, $operation);
7472
}
7573
}

src/Component/src/Symfony/Routing/Factory/OperationRouteFactory.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
use Gedmo\Sluggable\Util\Urlizer;
1717
use Sylius\Resource\Metadata\HttpOperation;
18-
use Sylius\Resource\Metadata\MetadataInterface;
1918
use Sylius\Resource\Metadata\ResourceMetadata;
2019
use Sylius\Resource\Symfony\Routing\Factory\RoutePath\OperationRoutePathFactoryInterface;
2120
use Symfony\Component\Routing\Route;
21+
use Webmozart\Assert\Assert;
2222

2323
/**
2424
* @experimental
@@ -30,9 +30,9 @@ public function __construct(
3030
) {
3131
}
3232

33-
public function create(MetadataInterface $metadata, ResourceMetadata $resource, HttpOperation $operation): Route
33+
public function create(ResourceMetadata $resource, HttpOperation $operation): Route
3434
{
35-
$routePath = $operation->getPath() ?? $this->getDefaultRoutePath($metadata, $operation);
35+
$routePath = $operation->getPath() ?? $this->getDefaultRoutePath($resource, $operation);
3636

3737
if (null !== $routePrefix = $operation->getRoutePrefix()) {
3838
$routePath = $routePrefix . '/' . $routePath;
@@ -49,14 +49,17 @@ public function create(MetadataInterface $metadata, ResourceMetadata $resource,
4949
);
5050
}
5151

52-
private function getDefaultRoutePath(MetadataInterface $metadata, HttpOperation $operation): string
52+
private function getDefaultRoutePath(ResourceMetadata $resource, HttpOperation $operation): string
5353
{
54-
return $this->getDefaultRoutePathForOperation($metadata, $operation);
54+
return $this->getDefaultRoutePathForOperation($resource, $operation);
5555
}
5656

57-
private function getDefaultRoutePathForOperation(MetadataInterface $metadata, HttpOperation $operation): string
57+
private function getDefaultRoutePathForOperation(ResourceMetadata $resource, HttpOperation $operation): string
5858
{
59-
$rootPath = sprintf('%s', Urlizer::urlize($metadata->getPluralName()));
59+
$pluralName = $resource->getPluralName();
60+
Assert::notNull($pluralName, 'Plural name of the resource should be defined');
61+
62+
$rootPath = sprintf('%s', Urlizer::urlize($pluralName));
6063

6164
if (null !== $path = $operation->getPath()) {
6265
return $path;

src/Component/src/Symfony/Routing/Factory/OperationRouteFactoryInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Sylius\Resource\Symfony\Routing\Factory;
1515

1616
use Sylius\Resource\Metadata\HttpOperation;
17-
use Sylius\Resource\Metadata\MetadataInterface;
1817
use Sylius\Resource\Metadata\ResourceMetadata;
1918
use Symfony\Component\Routing\Route;
2019

@@ -23,5 +22,5 @@
2322
*/
2423
interface OperationRouteFactoryInterface
2524
{
26-
public function create(MetadataInterface $metadata, ResourceMetadata $resource, HttpOperation $operation): Route;
25+
public function create(ResourceMetadata $resource, HttpOperation $operation): Route;
2726
}

0 commit comments

Comments
 (0)