-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.php
76 lines (57 loc) · 2.21 KB
/
worker.php
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
use App\Application\Handlers\HttpErrorHandler;
use App\Application\Handlers\ShutdownHandler;
use App\Application\ResponseEmitter\ResponseEmitter;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
use Spiral\RoadRunner\Worker;
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/app/env_init.php';
// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();
if (false) { // Should be set to true in production
$containerBuilder->enableCompilation(__DIR__ . '/var/cache');
}
// Set up settings
$settings = require __DIR__ . '/app/settings.php';
$settings($containerBuilder);
// Set up dependencies
$dependencies = require __DIR__ . '/app/dependencies.php';
$dependencies($containerBuilder);
// Build PHP-DI Container instance
$container = $containerBuilder->build();
$worker = Worker::create();
$psrFactory = new \Nyholm\Psr7\Factory\Psr17Factory();
$psrWorker = new \Spiral\RoadRunner\Http\PSR7Worker($worker, $psrFactory, $psrFactory, $psrFactory);
$app = AppFactory::create($psrFactory, $container);
$callableResolver = $app->getCallableResolver();
// Register middleware
$middleware = require __DIR__ . '/app/middleware.php';
$middleware($app);
// Register routes
$routes = require __DIR__ . '/app/routes.php';
$routes($app);
/** @var SettingsInterface $settings */
$settings = $container->get(SettingsInterface::class);
$displayErrorDetails = $settings->get('displayErrorDetails');
$logError = $settings->get('logError');
$logErrorDetails = $settings->get('logErrorDetails');
// Create Error Handler
$responseFactory = $app->getResponseFactory();
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);
// Add Routing Middleware
$app->addRoutingMiddleware();
// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logError, $logErrorDetails);
$errorMiddleware->setDefaultErrorHandler($errorHandler);
while ($req = $psrWorker->waitRequest()) {
try {
$response = $app->handle($req);
$psrWorker->respond($response);
} catch (Throwable $e) {
$psrWorker->getWorker()->error((string)$e);
}
}