| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace LaravelUi5\OData; |
| 6: | |
| 7: | use LaravelUi5\OData\Edm\Container\EntitySet; |
| 8: | use LaravelUi5\OData\Service\Builder\EdmBuilder; |
| 9: | use LaravelUi5\OData\Service\Builder\ResolverMapBuilder; |
| 10: | use LaravelUi5\OData\Service\Builder\RuntimeSchemaBuilder; |
| 11: | use LaravelUi5\OData\Service\Cache\EdmxLoader; |
| 12: | use LaravelUi5\OData\Service\Cache\ResolverMapLoader; |
| 13: | use LaravelUi5\OData\Service\Contracts\CustomEntitySetInterface; |
| 14: | use LaravelUi5\OData\Service\Contracts\EdmBuilderInterface; |
| 15: | use LaravelUi5\OData\Service\Contracts\ODataServiceInterface; |
| 16: | use LaravelUi5\OData\Service\Contracts\RuntimeSchemaBuilderInterface; |
| 17: | use LaravelUi5\OData\Service\Contracts\RuntimeSchemaInterface; |
| 18: | use LaravelUi5\OData\Edm\Property\NavigationProperty; |
| 19: | use LaravelUi5\OData\Service\Contracts\VirtualExpandResolverInterface; |
| 20: | use LaravelUi5\OData\Service\Discovery\ModelDiscovery; |
| 21: | use LaravelUi5\OData\Service\Resolver\ResolverMap; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | class ODataService implements ODataServiceInterface |
| 58: | { |
| 59: | private ?RuntimeSchemaInterface $cachedSchema = null; |
| 60: | private ?ModelDiscovery $discovery = null; |
| 61: | |
| 62: | |
| 63: | private array $customEntitySets = []; |
| 64: | |
| 65: | public function __construct( |
| 66: | private readonly string $serviceUriValue = '', |
| 67: | private readonly string $namespaceValue = '', |
| 68: | ) {} |
| 69: | |
| 70: | |
| 71: | |
| 72: | public function serviceUri(): string |
| 73: | { |
| 74: | return $this->serviceUriValue; |
| 75: | } |
| 76: | |
| 77: | public function namespace(): string |
| 78: | { |
| 79: | return $this->namespaceValue; |
| 80: | } |
| 81: | |
| 82: | public function cachedMetadataXMLPath(): ?string |
| 83: | { |
| 84: | return null; |
| 85: | } |
| 86: | |
| 87: | public function endpoint(): string |
| 88: | { |
| 89: | $prefix = rtrim(config('odata.prefix', 'odata'), '/'); |
| 90: | $uri = $this->serviceUri(); |
| 91: | $route = ($uri === '') ? $prefix : $prefix . '/' . $uri; |
| 92: | |
| 93: | return url($route) . '/'; |
| 94: | } |
| 95: | |
| 96: | public function route(): string |
| 97: | { |
| 98: | $prefix = rtrim(config('odata.prefix', 'odata'), '/'); |
| 99: | $uri = $this->serviceUri(); |
| 100: | |
| 101: | return ($uri === '') ? $prefix : $prefix . '/' . $uri; |
| 102: | } |
| 103: | |
| 104: | public function schema(): RuntimeSchemaInterface |
| 105: | { |
| 106: | if ($this->cachedSchema === null) { |
| 107: | |
| 108: | $edmx = EdmxLoader::forService($this); |
| 109: | $resolverMap = ResolverMapLoader::forService($this); |
| 110: | |
| 111: | if ($edmx !== null && $resolverMap !== null) { |
| 112: | $runtimeBuilder = new RuntimeSchemaBuilder($edmx); |
| 113: | $resolverMap->applyTo($runtimeBuilder); |
| 114: | $this->bindFunctions($runtimeBuilder); |
| 115: | $this->cachedSchema = $runtimeBuilder->build(); |
| 116: | |
| 117: | return $this->cachedSchema; |
| 118: | } |
| 119: | |
| 120: | |
| 121: | $builder = (new EdmBuilder())->version(config('odata.version', '4.0')); |
| 122: | $builder = $this->configure($builder); |
| 123: | |
| 124: | |
| 125: | |
| 126: | $this->applyCustomEntitySets($builder); |
| 127: | |
| 128: | if ($this->discovery !== null) { |
| 129: | $this->applyVirtualExpandsToDiscovery(); |
| 130: | $this->discovery->apply($builder, $this->namespace()); |
| 131: | } |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | $this->applyVirtualExpandsToBuilder($builder); |
| 137: | |
| 138: | $edmx = $builder->build(); |
| 139: | |
| 140: | |
| 141: | $mapBuilder = new ResolverMapBuilder($edmx); |
| 142: | |
| 143: | if ($this->discovery !== null) { |
| 144: | $this->discovery->registerOnMap($mapBuilder); |
| 145: | } |
| 146: | |
| 147: | $this->applyCustomEntitySetBindings($mapBuilder); |
| 148: | $this->registerBindings($mapBuilder); |
| 149: | $resolverMap = $mapBuilder->build(); |
| 150: | |
| 151: | $runtimeBuilder = new RuntimeSchemaBuilder($edmx); |
| 152: | $resolverMap->applyTo($runtimeBuilder); |
| 153: | $this->bindFunctions($runtimeBuilder); |
| 154: | $this->cachedSchema = $runtimeBuilder->build(); |
| 155: | } |
| 156: | |
| 157: | return $this->cachedSchema; |
| 158: | } |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | public function resolverMap(): ResolverMap |
| 166: | { |
| 167: | |
| 168: | $this->schema(); |
| 169: | |
| 170: | |
| 171: | $edmx = $this->cachedSchema->getEdmx(); |
| 172: | $mapBuilder = new ResolverMapBuilder($edmx); |
| 173: | |
| 174: | if ($this->discovery !== null) { |
| 175: | $this->discovery->registerOnMap($mapBuilder); |
| 176: | } |
| 177: | |
| 178: | $this->applyCustomEntitySetBindings($mapBuilder); |
| 179: | $this->registerBindings($mapBuilder); |
| 180: | |
| 181: | return $mapBuilder->build(); |
| 182: | } |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | protected function configure(EdmBuilderInterface $builder): EdmBuilderInterface |
| 193: | { |
| 194: | return $builder->namespace($this->namespace()); |
| 195: | } |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | protected function registerBindings(ResolverMapBuilder $map): void |
| 205: | { |
| 206: | |
| 207: | } |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | protected function bindFunctions(RuntimeSchemaBuilderInterface $builder): void |
| 216: | { |
| 217: | |
| 218: | } |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | protected function discoverModel(string $modelClass): static |
| 228: | { |
| 229: | $this->discovery ??= new ModelDiscovery(); |
| 230: | $this->discovery->add($modelClass); |
| 231: | |
| 232: | return $this; |
| 233: | } |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | protected function discoverCustomEntitySet(string $resolverClass): static |
| 245: | { |
| 246: | $this->customEntitySets[] = $resolverClass; |
| 247: | |
| 248: | return $this; |
| 249: | } |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | private function applyCustomEntitySets(EdmBuilderInterface $builder): void |
| 255: | { |
| 256: | $namespace = $this->namespace(); |
| 257: | |
| 258: | foreach ($this->customEntitySets as $resolverClass) { |
| 259: | $instance = new $resolverClass(); |
| 260: | $entityType = $instance->entityType($namespace); |
| 261: | $setName = $instance->entitySetName(); |
| 262: | |
| 263: | $builder->addEntityType($entityType); |
| 264: | $builder->addEntitySet(new EntitySet($setName, $entityType)); |
| 265: | } |
| 266: | } |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | private function applyVirtualExpandsToDiscovery(): void |
| 273: | { |
| 274: | $namespace = $this->namespace(); |
| 275: | |
| 276: | foreach ($this->customEntitySets as $resolverClass) { |
| 277: | $instance = new $resolverClass(); |
| 278: | |
| 279: | if (!($instance instanceof VirtualExpandResolverInterface)) { |
| 280: | continue; |
| 281: | } |
| 282: | |
| 283: | $targetType = $instance->entityType($namespace); |
| 284: | $targetSetName = $instance->entitySetName(); |
| 285: | |
| 286: | foreach ($instance->expandsOn() as $parentTypeName => $navName) { |
| 287: | $this->discovery->addVirtualExpand( |
| 288: | $parentTypeName, |
| 289: | $navName, |
| 290: | $targetType, |
| 291: | $targetSetName, |
| 292: | ); |
| 293: | } |
| 294: | } |
| 295: | } |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | |
| 304: | |
| 305: | private function applyVirtualExpandsToBuilder(EdmBuilderInterface $builder): void |
| 306: | { |
| 307: | $namespace = $this->namespace(); |
| 308: | $discoveredTypes = $this->discovery?->getDiscoveredTypeNames() ?? []; |
| 309: | |
| 310: | foreach ($this->customEntitySets as $resolverClass) { |
| 311: | $instance = new $resolverClass(); |
| 312: | |
| 313: | if (!($instance instanceof VirtualExpandResolverInterface)) { |
| 314: | continue; |
| 315: | } |
| 316: | |
| 317: | $targetType = $instance->entityType($namespace); |
| 318: | $targetSetName = $instance->entitySetName(); |
| 319: | |
| 320: | foreach ($instance->expandsOn() as $parentTypeName => $navName) { |
| 321: | |
| 322: | if (in_array($parentTypeName, $discoveredTypes, true)) { |
| 323: | continue; |
| 324: | } |
| 325: | |
| 326: | $builder->injectNavigationProperty( |
| 327: | $parentTypeName, |
| 328: | new NavigationProperty( |
| 329: | name: $navName, |
| 330: | targetType: $targetType, |
| 331: | isCollection: true, |
| 332: | ), |
| 333: | $targetSetName, |
| 334: | ); |
| 335: | } |
| 336: | } |
| 337: | } |
| 338: | |
| 339: | |
| 340: | |
| 341: | |
| 342: | private function applyCustomEntitySetBindings(ResolverMapBuilder $map): void |
| 343: | { |
| 344: | $container = $map->getEdmx()->getEntityContainer(); |
| 345: | |
| 346: | foreach ($this->customEntitySets as $resolverClass) { |
| 347: | $instance = new $resolverClass(); |
| 348: | $setName = $instance->entitySetName(); |
| 349: | $set = $container->getEntitySet($setName); |
| 350: | |
| 351: | if ($set !== null) { |
| 352: | $map->custom($set, $resolverClass); |
| 353: | } |
| 354: | } |
| 355: | } |
| 356: | } |
| 357: | |