1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlgoWeb\PODataLaravel\Controllers; |
6
|
|
|
|
7
|
|
|
use AlgoWeb\PODataLaravel\Controllers\Controller as BaseController; |
8
|
|
|
use AlgoWeb\PODataLaravel\Serialisers\IronicSerialiser; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Http\Response; |
11
|
|
|
use Illuminate\Support\Facades\App; |
12
|
|
|
use Illuminate\Support\Facades\DB; |
13
|
|
|
use POData\Configuration\EntitySetRights; |
14
|
|
|
use POData\Configuration\ServiceConfiguration; |
15
|
|
|
use POData\OperationContext\ServiceHost as ServiceHost; |
16
|
|
|
use AlgoWeb\PODataLaravel\OperationContext\Web\Illuminate\IlluminateOperationContext as OperationContextAdapter; |
17
|
|
|
use POData\Providers\Metadata\IMetadataProvider; |
18
|
|
|
use POData\SimpleDataService as DataService; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ODataController |
22
|
|
|
* @package AlgoWeb\PODataLaravel\Controllers |
23
|
|
|
*/ |
24
|
|
|
class ODataController extends BaseController |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Display a listing of the resource. |
28
|
|
|
* |
29
|
|
|
* @param Request $request |
30
|
|
|
* @throws \Exception |
31
|
|
|
* @return \Illuminate\Http\Response |
32
|
|
|
*/ |
33
|
|
|
public function index(Request $request) |
34
|
|
|
{ |
35
|
|
|
$dryRun = $this->isDryRun(); |
36
|
|
|
$commitCall = $dryRun ? 'rollBack' : 'commit'; |
37
|
|
|
|
38
|
|
|
try { |
39
|
|
|
DB::beginTransaction(); |
40
|
|
|
$context = new OperationContextAdapter($request); |
41
|
|
|
$host = new ServiceHost($context); |
42
|
|
|
$host->setServiceUri('/odata.svc/'); |
43
|
|
|
|
44
|
|
|
$query = App::make('odataquery'); |
45
|
|
|
$meta = App::make('metadata'); |
46
|
|
|
|
47
|
|
|
$config = $this->makeConfig($meta); |
48
|
|
|
$pageSize = $this->getAppPageSize(); |
49
|
|
|
if (null !== $pageSize) { |
50
|
|
|
$config->setEntitySetPageSize('*', $pageSize); |
51
|
|
|
} else { |
52
|
|
|
$config->setEntitySetPageSize('*', 400); |
53
|
|
|
} |
54
|
|
|
$config->setEntitySetAccessRule('*', EntitySetRights::ALL()); |
55
|
|
|
$config->setAcceptCountRequests(true); |
56
|
|
|
$config->setAcceptProjectionRequests(true); |
57
|
|
|
|
58
|
|
|
$service = new DataService($query, $meta, $host, null, null, $config); |
59
|
|
|
$cereal = new IronicSerialiser($service, null); |
60
|
|
|
$service = new DataService($query, $meta, $host, $cereal, null, $config); |
61
|
|
|
|
62
|
|
|
$service->handleRequest(); |
63
|
|
|
|
64
|
|
|
$odataResponse = $context->outgoingResponse(); |
65
|
|
|
|
66
|
|
|
$content = $odataResponse->getStream(); |
67
|
|
|
|
68
|
|
|
$headers = $odataResponse->getHeaders(); |
69
|
|
|
$responseCode = $headers[\POData\Common\ODataConstants::HTTPRESPONSE_HEADER_STATUS_CODE]; |
70
|
|
|
$response = new Response($content, intval($responseCode)); |
71
|
|
|
|
72
|
|
|
foreach ($headers as $headerName => $headerValue) { |
73
|
|
|
if (null !== $headerValue) { |
74
|
|
|
$response->headers->set($headerName, $headerValue); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
DB::$commitCall(); |
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
DB::rollBack(); |
80
|
|
|
throw $e; |
81
|
|
|
} |
82
|
|
|
return $response; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Is application dry-running (ie, not committing) non-READ requests? |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
protected function isDryRun() |
91
|
|
|
{ |
92
|
|
|
$configDump = env('APP_DRY_RUN', false); |
93
|
|
|
return true === $configDump; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return int|null |
98
|
|
|
*/ |
99
|
|
|
protected function getAppPageSize() |
100
|
|
|
{ |
101
|
|
|
/** @var mixed|null $size */ |
102
|
|
|
$size = env('APP_PAGE_SIZE', null); |
103
|
|
|
return null !== $size ? intval($size) : null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param IMetadataProvider|null $meta |
108
|
|
|
* @return ServiceConfiguration |
109
|
|
|
*/ |
110
|
|
|
protected function makeConfig(?IMetadataProvider $meta): ServiceConfiguration |
111
|
|
|
{ |
112
|
|
|
$config = new ServiceConfiguration($meta); |
113
|
|
|
return $config; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|