\React\Http\Server; use \React\Http\Response; $loop = React\EventLoop\Factory::create(); $cache = new \React\Cache\ArrayCache(); $server = new Server(array( // ΤϥʔϋϯυϦϯά function (ServerRequestInterface $request, callable $next) { $promise = new React\Promise\Promise(function ($resolve) use ($next, $request) { $resolve($next($request)); }); return $promise->then(null, function (Throwable $e) { error_log("ERROR:" . $e->getMessage()); error_log($e->getTraceAsString()); return new Response( 500, array(), 'Internal Server error' ); }); }, // ੩తϑΝΠϧ৴ new \WyriHaximus\React\Http\Middleware\WebrootPreloadMiddleware(__DIR__."/htdocs"), // ηογϣϯ new \WyriHaximus\React\Http\Middleware\SessionMiddleware( 'MySessionCookie', $cache, // Instance implementing React\Cache\CacheInterface [ // Optional array with cookie settings, order matters 0, // expiresAt, int, default '', // path, string, default '', // domain, string, default false, // secure, bool, default false // httpOnly, bool, default ] ), // csrfରࡦ function (ServerRequestInterface $request, callable $next) { // read session $session_obj = $request->getAttribute(\WyriHaximus\React\Http\Middleware\SessionMiddleware::ATTRIBUTE_NAME); $session = $session_obj->getContents(); if(!isset($session['csrf_token'])) { $csrf_token = base64_encode(random_bytes(64)); $session_obj->setContents([ 'csrf_token' => $csrf_token, ]); }else{ $csrf_token = $session['csrf_token']; } // check token if(strtolower($request->getMethod()) === "post"){ $params = $request->getParsedBody(); if(!isset($params['csrf_token']) || $params['csrf_token'] !== $csrf_token){ error_log("invalid csrf_token"); return new Response(400, [], "invalid csrf_token"); } } $request = $request->withAttribute('csrf_token', $csrf_token); return $next($request); }, // ΣϒΞϓϦ function (ServerRequestInterface $request) { // SlimॳظԽ $container = new \Slim\Container; $app = new \Slim\App($container); // reactͷrequestΛಥͬࠐΉ $container['request'] = function ($container) use ($request) { return $request; }; // Register Twig View helper $container['view'] = function ($c) { $view = new \Slim\Views\Twig(TEMPLATES_DIR_PATH, []); $view->addExtension(new \Slim\Views\TwigExtension($c['router'], '/')); return $view; }; // csrf_tokenΛอ࣋͠ɺTwigϔϧύʹ͢ $csrf_token = $request->getAttribute('csrf_token'); $container['csrf_token'] = function ($c) use ($csrf_token){ return $csrf_token; }; $container->view->addExtension(new \Tinitter\Misc\TwigExt\CsrfExtension($csrf_token)); // SlimʹϧʔτΛొ \Tinitter\Route::registration($container); // ࣮ߦ return $app->run(true); } )); $socket = new React\Socket\Server(8888, $loop); $server->listen($socket); $loop->run();