|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AlgoWeb\PODataLaravel\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use AlgoWeb\PODataLaravel\Controllers\Controller as BaseController; |
|
6
|
|
|
use AlgoWeb\PODataLaravel\Serialisers\IronicSerialiser; |
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Http\Response; |
|
10
|
|
|
use Illuminate\Support\Facades\App; |
|
11
|
|
|
use Illuminate\Support\Facades\DB; |
|
12
|
|
|
use Illuminate\Support\Facades\Storage; |
|
13
|
|
|
use POData\OperationContext\ServiceHost as ServiceHost; |
|
14
|
|
|
use POData\OperationContext\Web\Illuminate\IlluminateOperationContext as OperationContextAdapter; |
|
15
|
|
|
use POData\SimpleDataService as DataService; |
|
16
|
|
|
|
|
17
|
|
|
class ODataController extends BaseController |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Display a listing of the resource. |
|
21
|
|
|
* |
|
22
|
|
|
* @return \Illuminate\Http\Response |
|
23
|
|
|
*/ |
|
24
|
|
|
public function index(Request $request) |
|
25
|
|
|
{ |
|
26
|
|
|
$dump = $this->isDumping(); |
|
27
|
|
|
$dryRun = $this->isDryRun(); |
|
28
|
|
|
$commitCall = $dryRun ? 'rollBack' : 'commit'; |
|
29
|
|
|
|
|
30
|
|
|
try { |
|
31
|
|
|
DB::beginTransaction(); |
|
32
|
|
|
$context = new OperationContextAdapter($request); |
|
33
|
|
|
$host = new ServiceHost($context, $request); |
|
34
|
|
|
$host->setServiceUri('/odata.svc/'); |
|
35
|
|
|
|
|
36
|
|
|
$query = App::make('odataquery'); |
|
37
|
|
|
$meta = App::make('metadata'); |
|
38
|
|
|
|
|
39
|
|
|
$service = new DataService($query, $meta, $host); |
|
40
|
|
|
$cereal = new IronicSerialiser($service, null); |
|
41
|
|
|
$service = new DataService($query, $meta, $host, $cereal); |
|
42
|
|
|
$pageSize = $this->getAppPageSize(); |
|
43
|
|
|
if (null !== $pageSize) { |
|
44
|
|
|
$service->maxPageSize = intval($pageSize); |
|
45
|
|
|
} |
|
46
|
|
|
$service->handleRequest(); |
|
47
|
|
|
|
|
48
|
|
|
$odataResponse = $context->outgoingResponse(); |
|
49
|
|
|
|
|
50
|
|
|
if (true === $dump) { |
|
51
|
|
|
// iff XTest header is set, containing class and method name |
|
52
|
|
|
// dump outgoing odataResponse, metadata, and incoming request |
|
53
|
|
|
$xTest = $request->header('XTest'); |
|
54
|
|
|
$date = Carbon::now(0); |
|
55
|
|
|
$timeString = $date->toTimeString(); |
|
56
|
|
|
$xTest = (null !== $xTest) ? $xTest |
|
57
|
|
|
: $request->method() . ';' . str_replace('/', '-', $request->path()) . ';' . $timeString . ';'; |
|
58
|
|
|
if (null != $xTest) { |
|
|
|
|
|
|
59
|
|
|
$reflectionClass = new \ReflectionClass('Illuminate\Http\Request'); |
|
60
|
|
|
$reflectionProperty = $reflectionClass->getProperty('userResolver'); |
|
61
|
|
|
$reflectionProperty->setAccessible(true); |
|
62
|
|
|
$reflectionProperty->setValue($request, null); |
|
63
|
|
|
$reflectionProperty = $reflectionClass->getProperty('routeResolver'); |
|
64
|
|
|
$reflectionProperty->setAccessible(true); |
|
65
|
|
|
$reflectionProperty->setValue($request, null); |
|
66
|
|
|
$cerealRequest = serialize($request); |
|
67
|
|
|
$cerealMeta = serialize($meta); |
|
68
|
|
|
$cerealResponse = serialize($odataResponse); |
|
69
|
|
|
Storage::put($xTest . 'request', $cerealRequest); |
|
70
|
|
|
Storage::put($xTest . 'metadata', $cerealMeta); |
|
71
|
|
|
Storage::put($xTest . 'response', $cerealResponse); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$content = $odataResponse->getStream(); |
|
76
|
|
|
|
|
77
|
|
|
$headers = $odataResponse->getHeaders(); |
|
78
|
|
|
$responseCode = $headers[\POData\Common\ODataConstants::HTTPRESPONSE_HEADER_STATUS_CODE]; |
|
79
|
|
|
$responseCode = isset($responseCode) ? intval($responseCode) : 200; |
|
80
|
|
|
$response = new Response($content, $responseCode); |
|
81
|
|
|
$response->setStatusCode($headers['Status']); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
foreach ($headers as $headerName => $headerValue) { |
|
84
|
|
|
if (null !== $headerValue) { |
|
85
|
|
|
$response->headers->set($headerName, $headerValue); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
DB::$commitCall(); |
|
89
|
|
|
} catch (\Exception $e) { |
|
90
|
|
|
DB::rollBack(); |
|
91
|
|
|
throw $e; |
|
92
|
|
|
} |
|
93
|
|
|
return $response; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function isDumping() |
|
100
|
|
|
{ |
|
101
|
|
|
$configDump = env('APP_DUMP_REQUESTS', false); |
|
102
|
|
|
return true === $configDump; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Is application dry-running (ie, not committing) non-READ requests? |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function isDryRun() |
|
111
|
|
|
{ |
|
112
|
|
|
$configDump = env('APP_DRY_RUN', false); |
|
113
|
|
|
return true === $configDump; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getAppPageSize() |
|
120
|
|
|
{ |
|
121
|
|
|
return env('APP_PAGE_SIZE', null); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|