Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 52 additions & 51 deletions src/guide/tutorial/using-yii-with-swoole.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,86 +46,87 @@ Create an entry script, `server.php`:

declare(strict_types=1);

use App\Environment;
use Ilex\SwoolePsr7\SwooleResponseConverter;
use Ilex\SwoolePsr7\SwooleServerRequestConverter;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Yii\Web\Application;
use Yiisoft\Config\Config;
use Psr\Log\LogLevel;
use Swoole\Http\Server;
use Swoole\HTTP\Request;
use Swoole\HTTP\Response;
use Yiisoft\Di\StateResetter;
use Yiisoft\ErrorHandler\ErrorHandler;
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer;
use Yiisoft\Log\Logger;
use Yiisoft\Log\StreamTarget;
use Yiisoft\Yii\Http\Application;
use Yiisoft\Yii\Http\Handler\ThrowableHandler;
use Yiisoft\Yii\Runner\Http\HttpApplicationRunner;

ini_set('display_errors', 'stderr');

define('YII_ENV', getenv('env') ?: 'production');
require_once dirname(__DIR__) . '/vendor/autoload.php';

$config = new Config(
dirname(__DIR__),
'/config/packages',
null,
[
'params',
'events',
'events-web',
'events-console',
]
require_once __DIR__ . '/src/bootstrap.php';

$runner = new HttpApplicationRunner(
rootPath: __DIR__,
debug: Environment::appDebug(),
checkEvents: Environment::appDebug(),
environment: Environment::appEnv(),
temporaryErrorHandler: new ErrorHandler(
new Logger(
[
(new StreamTarget())->setLevels([
LogLevel::EMERGENCY,
LogLevel::ERROR,
LogLevel::WARNING,
]),
],
),
new HtmlRenderer(),
),
);

$containerConfig = ContainerConfig::create()
->withDefinitions($config->get('web'))
->withProviders($config->get('providers-web'));
$container = new Container($containerConfig);

$bootstrapList = $config->get('bootstrap-web');
foreach ($bootstrapList as $callback) {
if (!(is_callable($callback))) {
$type = is_object($callback) ? get_class($callback) : gettype($callback);

throw new \RuntimeException("Bootstrap callback must be callable, $type given.");
}
$callback($container);
}

$container = $runner->getContainer();
$application = $container->get(Application::class);
$errorCatcher = $container->get(ErrorCatcher::class);
$stateResetter = $container->get(StateResetter::class);

$serverRequestFactory = new \Ilex\SwoolePsr7\SwooleServerRequestConverter(
$serverRequestFactory = new SwooleServerRequestConverter(
$container->get(ServerRequestFactoryInterface::class),
$container->get(UriFactoryInterface::class),
$container->get(UploadedFileFactoryInterface::class),
$container->get(StreamFactoryInterface::class)
);

$server = new Swoole\Http\Server('0.0.0.0', 9501);
$server = new Server('0.0.0.0', 9501);

$server->on('start', static function (Swoole\Http\Server $server) use ($application) {
$server->on('start', static function (Server $server) use ($application) {
$application->start();
echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$server->on('request', static function (
Swoole\Http\Request $request,
Swoole\Http\Response $response,
) use ($serverRequestFactory, $application, $container) {
$psr7Response = null;
Request $request,
Response $response
) use ($serverRequestFactory, $application, $errorCatcher, $stateResetter) {
try {
$requestContainer = clone $container;
$psr7Request = $serverRequestFactory->createFromSwoole($request);
$psr7Response = $application->handle($psr7Request);

$converter = new \Ilex\SwoolePsr7\SwooleResponseConverter($response);
$converter->send($psr7Response);
} catch (\Throwable $t) {
// TODO: render it properly
$response->end($t->getMessage());
} finally {
$application->afterEmit($psr7Response ?? null);
$container->get(\Yiisoft\Di\StateResetter::class)->reset();
$container = $requestContainer;
(new SwooleResponseConverter($response))->send($psr7Response);
} catch (Throwable $throwable) {
$handler = new ThrowableHandler($throwable);
$psr7Response = $errorCatcher->process($request, $handler);
(new SwooleResponseConverter($response))->send($psr7Response);
}
$application->afterEmit($psr7Response);
$stateResetter->reset();
});

$server->on('shutdown', static function (Swoole\Http\Server $server) use ($application) {
$server->on('shutdown', static function (Server $server) use ($application) {
$application->shutdown();
});

Expand Down