-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcakephp-worker.php
More file actions
48 lines (41 loc) · 1.41 KB
/
cakephp-worker.php
File metadata and controls
48 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
<?php
declare(strict_types=1);
/**
* @todo add note about creating a hardlink to the worker as an option:
*
* ```console
* ln vendor/cakedc/cakephp-roadrunner/worker/cakephp-worker.php cakephp-worker.php
* ```
*/
ini_set('display_errors', 'stderr');
// You may need to change the `$rootDirectory` depending on where you've copied this file. This sample assumes the
// worker is in the same location as your `vendor` directory.
$rootDirectory = __DIR__;
include $rootDirectory . '/vendor/autoload.php';
use CakeDC\Roadrunner\Bridge;
use CakeDC\Roadrunner\ErrorHandler;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\StreamFactory;
use Laminas\Diactoros\UploadedFileFactory;
use Spiral\RoadRunner\Http\PSR7Worker;
use Spiral\RoadRunner\Worker;
$bridge = new Bridge($rootDirectory);
$psr7 = new PSR7Worker(Worker::create(), new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory());
while (true) {
try {
$request = $psr7->waitRequest();
if (!$request instanceof ServerRequest) { // Termination request received
break;
}
} catch (\Throwable $e) {
$psr7->respond(ErrorHandler::response(400, $e));
continue;
}
try {
$response = $bridge->handle($request);
$psr7->respond($response);
} catch (\Throwable $e) {
$psr7->respond(ErrorHandler::response(500, $e));
}
}