Skip to content

Commit fe8159d

Browse files
committed
refactor
1 parent 455dc4a commit fe8159d

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2828
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2929
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
30-
use ApiPlatform\Metadata\ResourceClassResolverInterface;
31-
use ApiPlatform\Metadata\Util\TypeHelper;
30+
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
3231
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
3332
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
3433
use ApiPlatform\State\Parameter\ValueCaster;
@@ -44,6 +43,7 @@
4443
*/
4544
final class ParameterResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
4645
{
46+
use ResourceClassInfoTrait;
4747
use StateOptionsTrait;
4848

4949
private array $localPropertyCache = [];
@@ -55,7 +55,6 @@ public function __construct(
5555
private readonly ?ContainerInterface $filterLocator = null,
5656
private readonly ?NameConverterInterface $nameConverter = null,
5757
private readonly ?LoggerInterface $logger = null,
58-
private readonly ?ResourceClassResolverInterface $resourceClassResolver = null,
5958
) {
6059
}
6160

@@ -121,7 +120,7 @@ private function getProperties(string $resourceClass, ?Parameter $parameter = nu
121120
}
122121

123122
if (!$isLastPart) {
124-
$nextClass = $this->getClassFromProperty($propertyMetadata);
123+
$nextClass = $this->getClassNameFromProperty($propertyMetadata);
125124

126125
if (!$nextClass) {
127126
break;
@@ -167,30 +166,6 @@ private function getProperties(string $resourceClass, ?Parameter $parameter = nu
167166
return $this->localPropertyCache[$k];
168167
}
169168

170-
private function getClassFromProperty(ApiProperty $propertyMetadata): ?string
171-
{
172-
if (!($type = $propertyMetadata->getNativeType())) {
173-
return null;
174-
}
175-
176-
$className = $this->extractClassNameFromType($type);
177-
178-
if ($className && $this->resourceClassResolver?->isResourceClass($className)) {
179-
return $className;
180-
}
181-
182-
return null;
183-
}
184-
185-
private function extractClassNameFromType(Type $type): ?string
186-
{
187-
if ($collectionValueType = TypeHelper::getCollectionValueType($type)) {
188-
return TypeHelper::getClassName($collectionValueType);
189-
}
190-
191-
return TypeHelper::getClassName($type);
192-
}
193-
194169
private function getDefaultParameters(Operation $operation, string $resourceClass, int &$internalPriority): Parameters
195170
{
196171
$propertyNames = $properties = [];

src/Metadata/Util/ResourceClassInfoTrait.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
namespace ApiPlatform\Metadata\Util;
1515

16+
use ApiPlatform\Metadata\ApiProperty;
1617
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
1718
use ApiPlatform\Metadata\ResourceClassResolverInterface;
19+
use Symfony\Component\TypeInfo\Type;
1820

1921
/**
2022
* Retrieves information about a resource class.
@@ -63,4 +65,28 @@ private function isResourceClass(string $class): bool
6365
// assume that it's a resource class
6466
return true;
6567
}
68+
69+
private function getTypeFromProperty(ApiProperty $propertyMetadata): ?Type
70+
{
71+
return $propertyMetadata->getNativeType();
72+
}
73+
74+
private function extractClassNameFromType(Type $type): ?string
75+
{
76+
return TypeHelper::getClassName(TypeHelper::getCollectionValueType($type) ?? $type);
77+
}
78+
79+
/**
80+
* Gets the class name from a property metadata if it's a resource class.
81+
*/
82+
protected function getClassNameFromProperty(ApiProperty $propertyMetadata): ?string
83+
{
84+
if (!($type = $this->getTypeFromProperty($propertyMetadata))) {
85+
return null;
86+
}
87+
88+
$className = $this->extractClassNameFromType($type);
89+
90+
return $className && $this->isResourceClass($className) ? $className : null;
91+
}
6692
}

src/Symfony/Bundle/Resources/config/metadata/resource.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@
151151
service('api_platform.filter_locator')->ignoreOnInvalid(),
152152
service('api_platform.name_converter')->ignoreOnInvalid(),
153153
service('logger')->ignoreOnInvalid(),
154-
service('api_platform.resource_class_resolver')->ignoreOnInvalid(),
155154
]);
156155

157156
$services->set('api_platform.metadata.resource.metadata_collection_factory.cached', CachedResourceMetadataCollectionFactory::class)

0 commit comments

Comments
 (0)