|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Metadata\Resource\Factory; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\Parameter; |
| 17 | +use ApiPlatform\Metadata\Parameters; |
| 18 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 19 | + |
| 20 | +/** |
| 21 | + * Adds default parameters from the global configuration to all resources and operations. |
| 22 | + * |
| 23 | + * @author Maxence Castel <maxence.castel59@gmail.com> |
| 24 | + */ |
| 25 | +final class DefaultParametersResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @param array<string, array<string, mixed>> $defaultParameters Array where keys are parameter class names and values are their configuration |
| 29 | + */ |
| 30 | + public function __construct( |
| 31 | + private readonly array $defaultParameters = [], |
| 32 | + private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function create(string $resourceClass): ResourceMetadataCollection |
| 40 | + { |
| 41 | + $resourceMetadataCollection = new ResourceMetadataCollection($resourceClass); |
| 42 | + |
| 43 | + if ($this->decorated) { |
| 44 | + $resourceMetadataCollection = $this->decorated->create($resourceClass); |
| 45 | + } |
| 46 | + |
| 47 | + if (empty($this->defaultParameters)) { |
| 48 | + return $resourceMetadataCollection; |
| 49 | + } |
| 50 | + |
| 51 | + $defaultParams = $this->buildDefaultParameters(); |
| 52 | + |
| 53 | + foreach ($resourceMetadataCollection as $i => $resource) { |
| 54 | + $resourceParameters = $resource->getParameters() ?? new Parameters(); |
| 55 | + $mergedResourceParameters = $this->mergeParameters($resourceParameters, $defaultParams); |
| 56 | + $resource = $resource->withParameters($mergedResourceParameters); |
| 57 | + |
| 58 | + foreach ($operations = $resource->getOperations() ?? [] as $operationName => $operation) { |
| 59 | + $operationParameters = $operation->getParameters() ?? new Parameters(); |
| 60 | + $mergedOperationParameters = $this->mergeParameters($operationParameters, $defaultParams); |
| 61 | + $operations->add((string) $operationName, $operation->withParameters($mergedOperationParameters)); |
| 62 | + } |
| 63 | + |
| 64 | + if ($operations) { |
| 65 | + $resource = $resource->withOperations($operations); |
| 66 | + } |
| 67 | + |
| 68 | + foreach ($graphQlOperations = $resource->getGraphQlOperations() ?? [] as $operationName => $operation) { |
| 69 | + $operationParameters = $operation->getParameters() ?? new Parameters(); |
| 70 | + $mergedOperationParameters = $this->mergeParameters($operationParameters, $defaultParams); |
| 71 | + $graphQlOperations[$operationName] = $operation->withParameters($mergedOperationParameters); |
| 72 | + } |
| 73 | + |
| 74 | + if ($graphQlOperations) { |
| 75 | + $resource = $resource->withGraphQlOperations($graphQlOperations); |
| 76 | + } |
| 77 | + |
| 78 | + $resourceMetadataCollection[$i] = $resource; |
| 79 | + } |
| 80 | + |
| 81 | + return $resourceMetadataCollection; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Builds Parameter objects from the default configuration array. |
| 86 | + * |
| 87 | + * @return array<string, Parameter> Array of Parameter objects indexed by their key |
| 88 | + */ |
| 89 | + private function buildDefaultParameters(): array |
| 90 | + { |
| 91 | + $parameters = []; |
| 92 | + |
| 93 | + foreach ($this->defaultParameters as $parameterClass => $config) { |
| 94 | + if (!is_subclass_of($parameterClass, Parameter::class)) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + $key = $config['key'] ?? null; |
| 99 | + if (!$key) { |
| 100 | + $key = (new \ReflectionClass($parameterClass))->getShortName(); |
| 101 | + } |
| 102 | + |
| 103 | + $identifier = $key; |
| 104 | + |
| 105 | + $parameter = $this->createParameterFromConfig($parameterClass, $config); |
| 106 | + $parameters[$identifier] = $parameter; |
| 107 | + } |
| 108 | + |
| 109 | + return $parameters; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Creates a Parameter instance from configuration. |
| 114 | + * |
| 115 | + * @param class-string<Parameter> $parameterClass The parameter class name |
| 116 | + * @param array<string, mixed> $config The configuration array |
| 117 | + * |
| 118 | + * @return Parameter The created parameter instance |
| 119 | + */ |
| 120 | + private function createParameterFromConfig(string $parameterClass, array $config): Parameter |
| 121 | + { |
| 122 | + return new $parameterClass( |
| 123 | + key: $config['key'] ?? null, |
| 124 | + schema: $config['schema'] ?? null, |
| 125 | + openApi: null, |
| 126 | + provider: null, |
| 127 | + filter: $config['filter'] ?? null, |
| 128 | + property: $config['property'] ?? null, |
| 129 | + description: $config['description'] ?? null, |
| 130 | + properties: null, |
| 131 | + required: $config['required'] ?? false, |
| 132 | + priority: $config['priority'] ?? null, |
| 133 | + hydra: $config['hydra'] ?? null, |
| 134 | + constraints: $config['constraints'] ?? null, |
| 135 | + security: $config['security'] ?? null, |
| 136 | + securityMessage: $config['security_message'] ?? null, |
| 137 | + extraProperties: $config['extra_properties'] ?? [], |
| 138 | + filterContext: null, |
| 139 | + nativeType: null, |
| 140 | + castToArray: null, |
| 141 | + castToNativeType: null, |
| 142 | + castFn: null, |
| 143 | + default: $config['default'] ?? null, |
| 144 | + filterClass: $config['filter_class'] ?? null, |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Merges default parameters with operation-specific parameters. |
| 150 | + * |
| 151 | + * @param Parameters $operationParameters The parameters already defined on the operation |
| 152 | + * @param array<string, Parameter> $defaultParams The default parameters to merge |
| 153 | + * |
| 154 | + * @return Parameters The merged parameters |
| 155 | + */ |
| 156 | + private function mergeParameters(Parameters $operationParameters, array $defaultParams): Parameters |
| 157 | + { |
| 158 | + $merged = new Parameters($defaultParams); |
| 159 | + |
| 160 | + foreach ($operationParameters as $key => $param) { |
| 161 | + $merged->add($key, $param); |
| 162 | + } |
| 163 | + |
| 164 | + return $merged; |
| 165 | + } |
| 166 | +} |
0 commit comments