<?php
require __DIR__ . '/../vendor/autoload.php';
Swoole\Runtime::enableCoroutine(true,SWOOLE_HOOK_ALL|SWOOLE_HOOK_CURL);
$http = new Swoole\Http\Server("0.0.0.0", 3322, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$http->set([
'document_root' => __DIR__,
'enable_static_handler' => true,
"task_worker_num"=>2,
"task_enable_coroutine"=>true,
"task_use_object"=>true
]);
$app = require __DIR__ . '/../bootstrap/app.php';
$app->terminating(function (\Illuminate\Foundation\Application $application) {
$auth = $application['auth'];
$reflect_class = new ReflectionClass($auth);
$property_guards = $reflect_class->getProperty("guards");
$property_guards->setAccessible(true);
if ($property_guards->getValue($auth)){
$property_guards->setValue($auth, []);
}
});
$http->app = $app;
$app->instance("swoole_http", $http);
$http->on("request", function (swoole_http_request $swoole_request, swoole_http_response $swoole_response) use ($http) {
$app = $http->app;
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
Illuminate\Http\Request::enableHttpMethodParameterOverride();
if (!isset($swoole_request->server["REQUEST_URI"])) {
$swoole_request->server["REQUEST_URI"] = $swoole_request->server["request_uri"];
}
$symfony_request = new Symfony\Component\HttpFoundation\Request($swoole_request->get ?: [], $swoole_request->post ?: [], [], $swoole_request->cookie ?: [], $swoole_request->files ?: [], $swoole_request->server ?: []);
$laravel_request = Illuminate\Http\Request::createFromBase($symfony_request);
$laravel_response = $kernel->handle($laravel_request);
$content = $laravel_response->getContent();
$swoole_response->status($laravel_response->getStatusCode());
foreach ($laravel_response->headers->allPreserveCaseWithoutCookies() as $name => $values) {
foreach ($values as $value) {
$swoole_response->header($name, $value);
}
}
foreach ($laravel_response->headers->getCookies() as $cookie) {
list($cookie_key, $cookie_val) = explode("=", $cookie);
$swoole_response->cookie($cookie_key, $cookie_val);
}
$swoole_response->end($content);
$kernel->terminate($laravel_request, $laravel_response);
});
$http->on("task",function ($serv, Swoole\Server\Task $task){});
$http->start();
本作品採用《CC 協議》,轉載必須註明作者和本文連結