|
| 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\Laravel\Metadata\Resource\Factory; |
| 15 | + |
| 16 | +use ApiPlatform\Laravel\Eloquent\Metadata\ModelMetadata; |
| 17 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 18 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 19 | + |
| 20 | +/** |
| 21 | + * Decorates the parameter resource metadata factory to add Laravel Eloquent relationship support. |
| 22 | + * |
| 23 | + * This decorator enhances nested property traversal by using Laravel's ModelMetadata |
| 24 | + * to discover relationships when standard property type information is unavailable. |
| 25 | + */ |
| 26 | +final class ParameterResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private readonly ResourceMetadataCollectionFactoryInterface $decorated, |
| 30 | + private readonly ModelMetadata $modelMetadata, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function create(string $resourceClass): ResourceMetadataCollection |
| 35 | + { |
| 36 | + $resourceMetadataCollection = $this->decorated->create($resourceClass); |
| 37 | + |
| 38 | + // Enhance parameters with Laravel Eloquent relationship information |
| 39 | + foreach ($resourceMetadataCollection as $i => $resource) { |
| 40 | + $operations = $resource->getOperations(); |
| 41 | + $stateOptions = $resource->getStateOptions(); |
| 42 | + $modelClass = $stateOptions instanceof \ApiPlatform\Laravel\Eloquent\State\Options ? $stateOptions->getModelClass() : null; |
| 43 | + |
| 44 | + foreach ($operations as $operationName => $operation) { |
| 45 | + $parameters = $operation->getParameters(); |
| 46 | + if (!$parameters) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + $modified = false; |
| 51 | + |
| 52 | + // Enhance each parameter's nested property info with Laravel relationship data |
| 53 | + foreach ($parameters as $key => $parameter) { |
| 54 | + // Use the key if it contains dots, otherwise use the property |
| 55 | + $property = str_contains($key, '.') ? $key : $parameter->getProperty(); |
| 56 | + if (!$property || !str_contains($property, '.')) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + $extraProperties = $parameter->getExtraProperties(); |
| 61 | + $nestedInfo = $extraProperties['nested_property_info'] ?? null; |
| 62 | + |
| 63 | + // If nested_property_info is missing but we have a nested property, |
| 64 | + // the core traversal failed - try to traverse using Laravel relationships |
| 65 | + if (!$nestedInfo && $modelClass) { |
| 66 | + $nestedInfo = $this->traverseNestedProperty($property, $modelClass); |
| 67 | + if ($nestedInfo) { |
| 68 | + $extraProperties['nested_property_info'] = $nestedInfo; |
| 69 | + // Also fix the property to be the full nested path |
| 70 | + $parameters = $parameters->add( |
| 71 | + $key, |
| 72 | + $parameter->withProperty($property)->withExtraProperties($extraProperties) |
| 73 | + ); |
| 74 | + $modified = true; |
| 75 | + } |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + // If we have nested info but some relation_classes are missing, fill them in |
| 80 | + if ($nestedInfo) { |
| 81 | + $relationSegments = $nestedInfo['relation_segments'] ?? []; |
| 82 | + $relationClasses = $nestedInfo['relation_classes'] ?? []; |
| 83 | + |
| 84 | + $needsUpdate = false; |
| 85 | + $updatedRelationClasses = $relationClasses; |
| 86 | + |
| 87 | + for ($idx = 0; $idx < \count($relationSegments); ++$idx) { |
| 88 | + $segment = $relationSegments[$idx]; |
| 89 | + $fromClass = $relationClasses[$idx] ?? null; |
| 90 | + |
| 91 | + if (!$fromClass) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + // Check if we need to determine the target class for this relationship |
| 96 | + $nextIdx = $idx + 1; |
| 97 | + if ($nextIdx < \count($relationSegments) && !isset($updatedRelationClasses[$nextIdx])) { |
| 98 | + $targetClass = $this->modelMetadata->getRelatedModelClass($fromClass, $segment); |
| 99 | + if ($targetClass) { |
| 100 | + $updatedRelationClasses[$nextIdx] = $targetClass; |
| 101 | + $needsUpdate = true; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if ($needsUpdate) { |
| 107 | + // Also update leaf_class if it was missing |
| 108 | + $lastIdx = \count($relationSegments) - 1; |
| 109 | + if ($lastIdx >= 0 && isset($updatedRelationClasses[$lastIdx])) { |
| 110 | + $lastSegment = $relationSegments[$lastIdx]; |
| 111 | + $leafClassCandidate = $this->modelMetadata->getRelatedModelClass($updatedRelationClasses[$lastIdx], $lastSegment); |
| 112 | + if ($leafClassCandidate) { |
| 113 | + $nestedInfo['leaf_class'] = $leafClassCandidate; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + $nestedInfo['relation_classes'] = $updatedRelationClasses; |
| 118 | + $extraProperties['nested_property_info'] = $nestedInfo; |
| 119 | + $parameters = $parameters->add($key, $parameter->withExtraProperties($extraProperties)); |
| 120 | + $modified = true; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if ($modified) { |
| 126 | + $operations = $operations->add($operationName, $operation->withParameters($parameters)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if ($operations !== $resource->getOperations()) { |
| 131 | + $resourceMetadataCollection[$i] = $resource->withOperations($operations); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return $resourceMetadataCollection; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Traverses a nested property path using Laravel Eloquent relationships. |
| 140 | + * |
| 141 | + * @param string $property The nested property path (e.g., "product.productVariations.variantName") |
| 142 | + * @param string $modelClass The starting model class (must be an Eloquent Model) |
| 143 | + * |
| 144 | + * @return array<string, mixed>|null The nested_property_info array, or null if traversal fails |
| 145 | + */ |
| 146 | + private function traverseNestedProperty(string $property, string $modelClass): ?array |
| 147 | + { |
| 148 | + $parts = explode('.', $property); |
| 149 | + $currentClass = $modelClass; |
| 150 | + $relationSegments = []; |
| 151 | + $relationClasses = []; |
| 152 | + |
| 153 | + // Traverse through all parts except the last one (which is the leaf property) |
| 154 | + for ($i = 0; $i < \count($parts) - 1; ++$i) { |
| 155 | + $part = $parts[$i]; |
| 156 | + $relationSegments[] = $part; |
| 157 | + $relationClasses[] = $currentClass; |
| 158 | + |
| 159 | + // Try to resolve the next class using Laravel relationships |
| 160 | + $nextClass = $this->modelMetadata->getRelatedModelClass($currentClass, $part); |
| 161 | + if (!$nextClass) { |
| 162 | + // Can't continue traversal |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + $currentClass = $nextClass; |
| 167 | + } |
| 168 | + |
| 169 | + // The last part is the leaf property - convert to snake_case for database column |
| 170 | + $leafProperty = $this->toSnakeCase($parts[\count($parts) - 1]); |
| 171 | + |
| 172 | + return [ |
| 173 | + 'original_path' => $property, |
| 174 | + 'path_segments' => $parts, |
| 175 | + 'relation_segments' => $relationSegments, |
| 176 | + 'converted_relation_segments' => $relationSegments, // No name conversion for Laravel |
| 177 | + 'relation_classes' => $relationClasses, |
| 178 | + 'leaf_property' => $leafProperty, |
| 179 | + 'leaf_class' => $currentClass, |
| 180 | + ]; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Converts a camelCase string to snake_case. |
| 185 | + */ |
| 186 | + private function toSnakeCase(string $input): string |
| 187 | + { |
| 188 | + return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $input)); |
| 189 | + } |
| 190 | +} |
0 commit comments