| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace LaravelUi5\OData\Http\Controller; |
| 6: | |
| 7: | use Illuminate\Http\Request; |
| 8: | use Illuminate\Routing\Controller; |
| 9: | use LaravelUi5\OData\Exception\BadRequestException; |
| 10: | use LaravelUi5\OData\Exception\InternalServerErrorException; |
| 11: | use LaravelUi5\OData\Exception\NotImplementedException; |
| 12: | use LaravelUi5\OData\Exception\ProtocolException; |
| 13: | use LaravelUi5\OData\Http\CustomQueryOptions; |
| 14: | use LaravelUi5\OData\Http\ODataRequest; |
| 15: | use LaravelUi5\OData\Http\ODataResponse; |
| 16: | use LaravelUi5\OData\Http\ReadGate; |
| 17: | use LaravelUi5\OData\Protocol\Execution\BatchHandler; |
| 18: | use LaravelUi5\OData\Protocol\Planning\QueryPlanner; |
| 19: | use LaravelUi5\OData\Service\Contracts\ODataServiceInterface; |
| 20: | use LaravelUi5\OData\Service\Contracts\ODataServiceRegistryInterface; |
| 21: | use Throwable; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | class OData extends Controller |
| 29: | { |
| 30: | public function __construct(private readonly ReadGate $gate) |
| 31: | { |
| 32: | } |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | public function handle(Request $request, ODataServiceRegistryInterface $resolver): ODataResponse |
| 41: | { |
| 42: | return $this->forService($request, $resolver->resolve($request->path())); |
| 43: | } |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | public function forService(Request $request, ODataServiceInterface $service): ODataResponse |
| 63: | { |
| 64: | try { |
| 65: | $route = $service->route(); |
| 66: | $rawPath = '/' . ltrim($request->path(), '/'); |
| 67: | $path = substr($rawPath, strlen('/' . ltrim($route, '/'))) ?: '/'; |
| 68: | |
| 69: | |
| 70: | $method = strtoupper($request->getMethod()); |
| 71: | |
| 72: | |
| 73: | if ($method === 'HEAD' && trim($path, '/') === '') { |
| 74: | return new ODataResponse(status: 200, headers: [ |
| 75: | 'X-CSRF-Token' => csrf_token(), |
| 76: | ]); |
| 77: | } |
| 78: | |
| 79: | if ($method !== 'GET' && !($method === 'POST' && trim($path, '/') === '$batch')) { |
| 80: | throw new BadRequestException( |
| 81: | 'method_not_allowed', |
| 82: | sprintf('HTTP method %s is not supported on this read-only service.', $method) |
| 83: | ); |
| 84: | } |
| 85: | |
| 86: | |
| 87: | $this->validateQueryOptions($request); |
| 88: | |
| 89: | |
| 90: | |
| 91: | if (trim($path, '/') === '$batch') { |
| 92: | $schema = $service->schema(); |
| 93: | return (new BatchHandler($schema, $service, $this->gate, $request)) |
| 94: | ->handle($request->getContent(), $request->header('Content-Type')); |
| 95: | } |
| 96: | |
| 97: | |
| 98: | $maxPageSize = $this->resolveMaxPageSize($request); |
| 99: | |
| 100: | $planRequest = new ODataRequest( |
| 101: | path: $path, |
| 102: | filter: $request->query('$filter'), |
| 103: | select: $request->query('$select'), |
| 104: | orderBy: $request->query('$orderby'), |
| 105: | top: $request->query('$top') !== null ? (int) $request->query('$top') : null, |
| 106: | skip: $request->query('$skip') !== null ? (int) $request->query('$skip') : null, |
| 107: | expand: $request->query('$expand'), |
| 108: | search: $request->query('$search'), |
| 109: | compute: $request->query('$compute'), |
| 110: | count: $request->query('$count') === 'true', |
| 111: | maxPageSize: $maxPageSize, |
| 112: | customQueryOptions: CustomQueryOptions::fromQuery($request->query()), |
| 113: | ); |
| 114: | |
| 115: | $schema = $service->schema(); |
| 116: | $plan = (new QueryPlanner)->plan($planRequest, $schema); |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | return $this->gate->execute($plan, $request, $schema, $service->endpoint()); |
| 122: | } catch (ProtocolException $e) { |
| 123: | throw $e; |
| 124: | } catch (Throwable $e) { |
| 125: | throw new InternalServerErrorException('internal_error', $e->getMessage(), $e); |
| 126: | } |
| 127: | } |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | private function resolveMaxPageSize(Request $request): ?int |
| 134: | { |
| 135: | |
| 136: | $maxPageSize = null; |
| 137: | $prefer = $request->header('Prefer', ''); |
| 138: | if ($prefer !== '' && $prefer !== null) { |
| 139: | if (preg_match('/(?:odata\.)?maxpagesize\s*=\s*(\d+)/i', $prefer, $m)) { |
| 140: | $maxPageSize = (int) $m[1]; |
| 141: | } |
| 142: | } |
| 143: | |
| 144: | |
| 145: | $paginationDefault = config('odata.pagination.default'); |
| 146: | if ($maxPageSize === null && $paginationDefault !== null) { |
| 147: | $maxPageSize = (int) $paginationDefault; |
| 148: | } |
| 149: | |
| 150: | |
| 151: | $paginationMax = config('odata.pagination.max'); |
| 152: | if ($paginationMax !== null && ($maxPageSize === null || $maxPageSize > (int) $paginationMax)) { |
| 153: | $maxPageSize = (int) $paginationMax; |
| 154: | } |
| 155: | |
| 156: | return $maxPageSize; |
| 157: | } |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | private function validateQueryOptions(Request $request): void |
| 163: | { |
| 164: | $supported = [ |
| 165: | '$filter', '$select', '$orderby', '$top', '$skip', '$count', |
| 166: | '$expand', '$search', '$compute', '$format', '$skiptoken', |
| 167: | '$batch', |
| 168: | ]; |
| 169: | |
| 170: | foreach ($request->query() as $key => $value) { |
| 171: | if (!is_string($key)) { |
| 172: | continue; |
| 173: | } |
| 174: | if (str_starts_with($key, '$') && !in_array(strtolower($key), $supported, true)) { |
| 175: | if (strtolower($key) === '$apply') { |
| 176: | throw new NotImplementedException( |
| 177: | 'not_implemented', |
| 178: | 'The $apply query option is not supported' |
| 179: | ); |
| 180: | } |
| 181: | throw new BadRequestException( |
| 182: | 'invalid_query_option', |
| 183: | sprintf('Unknown system query option: %s', $key) |
| 184: | ); |
| 185: | } |
| 186: | } |
| 187: | } |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | public function callAction($method, $parameters) |
| 194: | { |
| 195: | return parent::callAction($method, array_values($parameters)); |
| 196: | } |
| 197: | } |
| 198: | |