Skip to content

Revamp ApiSubresource #3689

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

Closed
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
6 changes: 6 additions & 0 deletions src/Annotation/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
* @Attribute("securityMessage", type="string"),
* @Attribute("securityPostDenormalize", type="string"),
* @Attribute("securityPostDenormalizeMessage", type="string"),
* @Attribute("path", type="string"),
* @Attribute("shortName", type="string"),
* @Attribute("stateless", type="bool"),
* @Attribute("subresourceOperations", type="array"),
Expand Down Expand Up @@ -159,6 +160,11 @@ final class ApiResource
*/
public $itemOperations;

/**
* @var string
*/
public $path;

/**
* @var string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public function create(string $resourceClass): ResourceMetadata
{
$resourceMetadata = $this->decorated->create($resourceClass);

if (null === $resourceMetadata->getCollectionOperations()) {
$resourceMetadata = $resourceMetadata->withCollectionOperations(['get' => ['method' => 'GET']]);
}

if (null === $resourceMetadata->getItemOperations()) {
$resourceMetadata = $resourceMetadata->withItemOperations(['get' => ['method' => 'GET']]);
foreach ($resourceMetadata as $path => $operationCollectionMetadata) {
if (null === $operationCollectionMetadata->getCollectionOperations()) {
$resourceMetadata[$path] = $operationCollectionMetadata->withCollectionOperations(['get' => ['method' => 'GET']]);
}

if (null === $operationCollectionMetadata->getItemOperations()) {
$resourceMetadata[$path] = $operationCollectionMetadata->withItemOperations(['get' => ['method' => 'GET']]);
}
}

return $resourceMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use ApiPlatform\Core\Documentation\Documentation;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Metadata\Resource\OperationCollectionMetadata;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Nelmio\ApiDocBundle\Extractor\AnnotationsProviderInterface;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -81,23 +81,23 @@ public function getAnnotations(): array

$annotations = [];
foreach ($resourceNameCollection as $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);

$prefixedShortName = ($iri = $resourceMetadata->getIri()) ? $iri : '#'.$resourceMetadata->getShortName();
$resourceHydraDoc = $this->getResourceHydraDoc($hydraDoc, $prefixedShortName);
if (null === $resourceHydraDoc) {
continue;
}
foreach ($this->resourceMetadataFactory->create($resourceClass) as $resourceMetadata) {
$prefixedShortName = ($iri = $resourceMetadata->getIri()) ? $iri : '#'.$resourceMetadata->getShortName();
$resourceHydraDoc = $this->getResourceHydraDoc($hydraDoc, $prefixedShortName);
if (null === $resourceHydraDoc) {
continue;
}

if (null !== $collectionOperations = $resourceMetadata->getCollectionOperations()) {
foreach ($collectionOperations as $operationName => $operation) {
$annotations[] = $this->getApiDoc(true, $resourceClass, $resourceMetadata, $operationName, $resourceHydraDoc, $entrypointHydraDoc);
if (null !== $collectionOperations = $resourceMetadata->getCollectionOperations()) {
foreach ($collectionOperations as $operationName => $operation) {
$annotations[] = $this->getApiDoc(true, $resourceClass, $resourceMetadata, $operationName, $resourceHydraDoc, $entrypointHydraDoc);
}
}
}

if (null !== $itemOperations = $resourceMetadata->getItemOperations()) {
foreach ($itemOperations as $operationName => $operation) {
$annotations[] = $this->getApiDoc(false, $resourceClass, $resourceMetadata, $operationName, $resourceHydraDoc);
if (null !== $itemOperations = $resourceMetadata->getItemOperations()) {
foreach ($itemOperations as $operationName => $operation) {
$annotations[] = $this->getApiDoc(false, $resourceClass, $resourceMetadata, $operationName, $resourceHydraDoc);
}
}
}
}
Expand All @@ -108,7 +108,7 @@ public function getAnnotations(): array
/**
* Builds ApiDoc annotation from ApiPlatform data.
*/
private function getApiDoc(bool $collection, string $resourceClass, ResourceMetadata $resourceMetadata, string $operationName, array $resourceHydraDoc, array $entrypointHydraDoc = []): ApiDoc
private function getApiDoc(bool $collection, string $resourceClass, OperationCollectionMetadata $resourceMetadata, string $operationName, array $resourceHydraDoc, array $entrypointHydraDoc = []): ApiDoc
{
if ($collection) {
$method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $operationName);
Expand Down
107 changes: 54 additions & 53 deletions src/Bridge/Symfony/Routing/ApiLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Metadata\Resource\OperationCollectionMetadata;
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
use ApiPlatform\Core\PathResolver\OperationPathResolverInterface;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -92,63 +92,64 @@ public function load($data, $type = null): RouteCollection
$this->loadExternalFiles($routeCollection);

foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$resourceMetadata = $resourceMetadata->withAttributes(($resourceMetadata->getAttributes() ?: []) + ['identified_by' => $this->identifiersExtractor->getIdentifiersFromResourceClass($resourceClass)]);
$resourceShortName = $resourceMetadata->getShortName();
foreach ($this->resourceMetadataFactory->create($resourceClass) as $operationCollectionMetadata) {
$operationCollectionMetadata = $operationCollectionMetadata->withAttributes(($operationCollectionMetadata->getAttributes() ?: []) + ['identified_by' => $this->identifiersExtractor->getIdentifiersFromResourceClass($resourceClass)]);
$resourceShortName = $operationCollectionMetadata->getShortName();

if (null === $resourceShortName) {
throw new InvalidResourceException(sprintf('Resource %s has no short name defined.', $resourceClass));
}
if (null === $resourceShortName) {
throw new InvalidResourceException(sprintf('Resource %s has no short name defined.', $resourceClass));
}

if (null !== $collectionOperations = $resourceMetadata->getCollectionOperations()) {
foreach ($collectionOperations as $operationName => $operation) {
$this->addRoute($routeCollection, $resourceClass, $operationName, $operation, $resourceMetadata, OperationType::COLLECTION);
if (null !== $collectionOperations = $operationCollectionMetadata->getCollectionOperations()) {
foreach ($collectionOperations as $operationName => $operation) {
$this->addRoute($routeCollection, $resourceClass, $operationName, $operation, $operationCollectionMetadata, OperationType::COLLECTION);
}
}
}

if (null !== $itemOperations = $resourceMetadata->getItemOperations()) {
foreach ($itemOperations as $operationName => $operation) {
$this->addRoute($routeCollection, $resourceClass, $operationName, $operation, $resourceMetadata, OperationType::ITEM);
if (null !== $itemOperations = $operationCollectionMetadata->getItemOperations()) {
foreach ($itemOperations as $operationName => $operation) {
$this->addRoute($routeCollection, $resourceClass, $operationName, $operation, $operationCollectionMetadata, OperationType::ITEM);
}
}
}

if (null === $this->subresourceOperationFactory) {
continue;
}
if (null === $this->subresourceOperationFactory) {
continue;
}

foreach ($this->subresourceOperationFactory->create($resourceClass) as $operationId => $operation) {
if (null === $controller = $operation['controller'] ?? null) {
$controller = self::DEFAULT_ACTION_PATTERN.'get_subresource';
foreach ($this->subresourceOperationFactory->create($resourceClass) as $operationId => $operation) {
if (null === $controller = $operation['controller'] ?? null) {
$controller = self::DEFAULT_ACTION_PATTERN.'get_subresource';

if (!$this->container->has($controller)) {
throw new RuntimeException(sprintf('There is no builtin action for the %s %s operation. You need to define the controller yourself.', OperationType::SUBRESOURCE, 'GET'));
if (!$this->container->has($controller)) {
throw new RuntimeException(sprintf('There is no builtin action for the %s %s operation. You need to define the controller yourself.', OperationType::SUBRESOURCE, 'GET'));
}
}
}

$routeCollection->add($operation['route_name'], new Route(
$operation['path'],
[
'_controller' => $controller,
'_format' => null,
'_stateless' => $operation['stateless'] ?? $resourceMetadata->getAttribute('stateless'),
'_api_resource_class' => $operation['resource_class'],
'_api_identified_by' => $operation['identified_by'],
'_api_has_composite_identifier' => false,
'_api_subresource_operation_name' => $operation['route_name'],
'_api_subresource_context' => [
'property' => $operation['property'],
'identifiers' => $operation['identifiers'],
'collection' => $operation['collection'],
'operationId' => $operationId,
],
] + ($operation['defaults'] ?? []),
$operation['requirements'] ?? [],
$operation['options'] ?? [],
$operation['host'] ?? '',
$operation['schemes'] ?? [],
['GET'],
$operation['condition'] ?? ''
));
$routeCollection->add($operation['route_name'], new Route(
$operation['path'],
[
'_controller' => $controller,
'_format' => null,
'_stateless' => $operation['stateless'] ?? $operationCollectionMetadata->getAttribute('stateless'),
'_api_resource_class' => $operation['resource_class'],
'_api_identified_by' => $operation['identified_by'],
'_api_has_composite_identifier' => false,
'_api_subresource_operation_name' => $operation['route_name'],
'_api_subresource_context' => [
'property' => $operation['property'],
'identifiers' => $operation['identifiers'],
'collection' => $operation['collection'],
'operationId' => $operationId,
],
] + ($operation['defaults'] ?? []),
$operation['requirements'] ?? [],
$operation['options'] ?? [],
$operation['host'] ?? '',
$operation['schemes'] ?? [],
['GET'],
$operation['condition'] ?? ''
));
}
}
}

Expand Down Expand Up @@ -204,9 +205,9 @@ private function loadExternalFiles(RouteCollection $routeCollection): void
*
* @throws RuntimeException
*/
private function addRoute(RouteCollection $routeCollection, string $resourceClass, string $operationName, array $operation, ResourceMetadata $resourceMetadata, string $operationType): void
private function addRoute(RouteCollection $routeCollection, string $resourceClass, string $operationName, array $operation, OperationCollectionMetadata $operationCollectionMetadata, string $operationType): void
{
$resourceShortName = $resourceMetadata->getShortName();
$resourceShortName = $operationCollectionMetadata->getShortName();

if (isset($operation['route_name'])) {
if (!isset($operation['method'])) {
Expand All @@ -228,10 +229,10 @@ private function addRoute(RouteCollection $routeCollection, string $resourceClas
}
}

$operation['identified_by'] = (array) ($operation['identified_by'] ?? $resourceMetadata->getAttribute('identified_by'));
$operation['has_composite_identifier'] = \count($operation['identified_by']) > 1 ? $resourceMetadata->getAttribute('composite_identifier', true) : false;
$operation['identified_by'] = (array) ($operation['identified_by'] ?? $operationCollectionMetadata->getAttribute('identified_by'));
$operation['has_composite_identifier'] = \count($operation['identified_by']) > 1 ? $operationCollectionMetadata->getAttribute('composite_identifier', true) : false;

$path = trim(trim($resourceMetadata->getAttribute('route_prefix', '')), '/');
$path = trim(trim($operationCollectionMetadata->getAttribute('route_prefix', '')), '/');
$path .= $this->operationPathResolver->resolveOperationPath($resourceShortName, $operation, $operationType, $operationName);

$route = new Route(
Expand Down
3 changes: 2 additions & 1 deletion src/Metadata/Extractor/XmlExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ protected function extractPath(string $path)
foreach ($xml->resource as $resource) {
$resourceClass = $this->resolve((string) $resource['class']);

$this->resources[$resourceClass] = [
$this->resources[$resourceClass][] = [
'path' => $this->phpize($resource, 'path', 'string'),
'shortName' => $this->phpize($resource, 'shortName', 'string'),
'description' => $this->phpize($resource, 'description', 'string'),
'iri' => $this->phpize($resource, 'iri', 'string'),
Expand Down
22 changes: 16 additions & 6 deletions src/Metadata/Extractor/YamlExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ protected function extractPath(string $path)
private function extractResources(array $resourcesYaml, string $path): void
{
foreach ($resourcesYaml as $resourceName => $resourceYaml) {
if (is_numeric($resourceName)) {
// Multi ApiResource declared
$resourceName = array_key_first($resourceYaml);
$resourceYaml = $resourceYaml[$resourceName];
}

$resourceName = $this->resolve($resourceName);

if (null === $resourceYaml) {
Expand All @@ -63,7 +69,8 @@ private function extractResources(array $resourcesYaml, string $path): void
throw new InvalidArgumentException(sprintf('"%s" setting is expected to be null or an array, %s given in "%s".', $resourceName, \gettype($resourceYaml), $path));
}

$this->resources[$resourceName] = [
$resource = [
'path' => $this->phpize($resourceYaml, 'path', 'string'),
'shortName' => $this->phpize($resourceYaml, 'shortName', 'string'),
'description' => $this->phpize($resourceYaml, 'description', 'string'),
'iri' => $this->phpize($resourceYaml, 'iri', 'string'),
Expand All @@ -75,7 +82,8 @@ private function extractResources(array $resourcesYaml, string $path): void
];

if (!isset($resourceYaml['properties'])) {
$this->resources[$resourceName]['properties'] = null;
$resource['properties'] = null;
$this->resources[$resourceName][] = $resource;

continue;
}
Expand All @@ -84,15 +92,17 @@ private function extractResources(array $resourcesYaml, string $path): void
throw new InvalidArgumentException(sprintf('"properties" setting is expected to be null or an array, %s given in "%s".', \gettype($resourceYaml['properties']), $path));
}

$this->extractProperties($resourceYaml, $resourceName, $path);
$this->extractProperties($resourceYaml, $resource, $path);

$this->resources[$resourceName][] = $resource;
}
}

private function extractProperties(array $resourceYaml, string $resourceName, string $path): void
private function extractProperties(array $resourceYaml, array $resource, string $path): void
{
foreach ($resourceYaml['properties'] as $propertyName => $propertyValues) {
if (null === $propertyValues) {
$this->resources[$resourceName]['properties'][$propertyName] = null;
$resource['properties'][$propertyName] = null;

continue;
}
Expand All @@ -104,7 +114,7 @@ private function extractProperties(array $resourceYaml, string $resourceName, st
$propertyValues['subresource']['resourceClass'] = $this->resolve($propertyValues['subresource']['resourceClass']);
}

$this->resources[$resourceName]['properties'][$propertyName] = [
$resource['properties'][$propertyName] = [
'description' => $this->phpize($propertyValues, 'description', 'string'),
'readable' => $this->phpize($propertyValues, 'readable', 'bool'),
'writable' => $this->phpize($propertyValues, 'writable', 'bool'),
Expand Down
Loading