Repository navigation
Expand file tree
/
Copy pathworker-oxphp.php
More file actions
85 lines (79 loc) · 3.04 KB
/
Copy pathworker-oxphp.php
File metadata and controls
85 lines (79 loc) · 3.04 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
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
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
use App\Environment;
use OxPHP\Server\Worker;
use Psr\Log\LogLevel;
use Yiisoft\Di\StateResetter;
use Yiisoft\ErrorHandler\ErrorHandler;
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
use Yiisoft\ErrorHandler\Renderer\PlainTextRenderer;
use Yiisoft\Log\Logger;
use Yiisoft\Log\StreamTarget;
use Yiisoft\PsrEmitter\SapiEmitter;
use Yiisoft\Yii\Http\Application;
use Yiisoft\Yii\Http\Handler\ThrowableHandler;
use Yiisoft\Yii\Runner\ApplicationRunner;
use Yiisoft\Yii\Runner\Http\RequestFactory;
require_once __DIR__ . '/src/bootstrap.php';
$runner = new class (
rootPath: __DIR__,
debug: Environment::appDebug(),
checkEvents: Environment::appDebug(),
environment: Environment::appEnv(),
bootstrapGroup: 'bootstrap-web',
eventsGroup: 'events-web',
diGroup: 'di-web',
diProvidersGroup: 'di-providers-web',
diDelegatesGroup: 'di-delegates-web',
diTagsGroup: 'di-tags-web',
paramsGroup: 'params-web',
nestedParamsGroups: ['params'],
nestedEventsGroups: ['events'],
) extends ApplicationRunner {
public function run(): void
{
$temporaryErrorHandler = new ErrorHandler(
new Logger([(new StreamTarget())->setLevels([LogLevel::EMERGENCY, LogLevel::ERROR, LogLevel::WARNING])]),
new PlainTextRenderer(),
);
$temporaryErrorHandler->register();
$container = $this->getContainer();
$errorHandler = $container->get(ErrorHandler::class);
$temporaryErrorHandler->unregister();
if ($this->debug) {
$errorHandler->debug();
}
$errorHandler->register();
$this->runBootstrap();
$this->checkEvents();
$application = $container->get(Application::class);
$requestFactory = $container->get(RequestFactory::class);
$emitter = new SapiEmitter();
$maxRequests = (int) ($_SERVER['MAX_REQUESTS'] ?? 0);
$requests = 0;
$application->start();
oxphp_worker(static function () use ($application, $container, $requestFactory, $emitter, $maxRequests, &$requests): void {
$startTime = microtime(true);
$request = $requestFactory->create()->withAttribute('applicationStartTime', $startTime);
$response = null;
try {
try {
$response = $application->handle($request);
} catch (Throwable $throwable) {
$response = $container->get(ErrorCatcher::class)->process($request, new ThrowableHandler($throwable));
}
$emitter->emit($response);
} finally {
$application->afterEmit($response);
// Resolve after handling so lazily created services are included.
$container->get(StateResetter::class)->reset();
gc_collect_cycles();
if ($maxRequests > 0 && ++$requests >= $maxRequests) {
Worker::current()->scheduleExit();
}
}
});
$application->shutdown();
}
};
$runner->run();