Created
October 8, 2020 21:39
-
-
Save nixoncode/3fbca0f710296c0b00f0639925e88442 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use DI\ContainerBuilder; | |
use Slim\Http\Response; | |
use DI\Bridge\Slim\Bridge; | |
use Spiral\Goridge\StreamRelay; | |
use Spiral\RoadRunner\PSR7Client; | |
ini_set('display_errors', 'stderr'); | |
require_once __DIR__ . './../vendor/autoload.php'; | |
$containerBuilder = new ContainerBuilder(); | |
$containerBuilder->addDefinitions( | |
[ | |
'name' => 'User', | |
] | |
); | |
$container = $containerBuilder->build(); | |
$app = Bridge::create($container); | |
//add a route handler for / | |
// [{name}] defines an optional param for the route | |
$app->get('/[{name}]', function ( Response $response, $name = null) use ($app) { | |
if (!$name){ // name was not supplied, use the one in the container | |
$name = $app->getContainer()->get('name'); | |
} | |
$response->getBody()->write("Hello $name"); | |
return $response; | |
} | |
); | |
$relay = new StreamRelay(STDIN, STDOUT); | |
$psr7 = new PSR7Client(new \Spiral\RoadRunner\Worker($relay)); | |
while ($req = $psr7->acceptRequest()) { | |
try { | |
//runs the app silently | |
$response = $app->handle($req); | |
//pass the response back to roadrunner psr client | |
$psr7->respond($response); | |
} catch (Throwable $exception) { | |
$psr7->getWorker()->error((string)$exception); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment