Last active
June 6, 2019 21:22
-
-
Save azjezz/2fcc81cc91845661c298410658151c48 to your computer and use it in GitHub Desktop.
Nuxed Hello world
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
use namespace Nuxed\Kernel; | |
use namespace Nuxed\Container; | |
use namespace Nuxed\Http\Router; | |
use namespace Nuxed\Http\Router\Generator; | |
final class Extension extends Kernel\Extension\AbstractExtension { | |
// register container services | |
public function register( | |
Container\ContainerBuilder $builder, | |
): void { | |
$builder->add(HomePageHandler::class, Container\factory(($container) ==> | |
new HomePageHandler($container->get(Generator\IUriGenerator::class)) | |
), true); | |
$builder->add(FooHandler::class, Container\factory(($_) ==> new FooHandler()), true); | |
} | |
<<__Override>> | |
public function route( | |
Router\IRouteCollector $routes, | |
Container\IServiceContainer $container, | |
): void { | |
$routes->get('/', $container->get(HomePageHandler::class), 'home'); | |
$routes->get('/page/foo', $container->get(FooHandler::class), 'foo'); | |
} | |
} |
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
use namespace Nuxed\Http\Server; | |
use namespace Nuxed\Http\Message; | |
final class FooHandler implements Server\IRequestHandler { | |
use Server\RequestHandlerMiddlewareTrait; | |
public async function handle( | |
Message\ServerRequest $request, | |
): Awaitable<Message\Response> { | |
$session = $request->getSession(); | |
$session->set('foo', $session->get('foo', 0) + 1); | |
$ses = <ul></ul>; | |
foreach ($session->items() as $key => $value) { | |
$ses->appendChild(<li>{$key} => {\print_r($value, true)}</li>); | |
} | |
$html = | |
<html> | |
<head> | |
<title>Hello, World!</title> | |
</head> | |
<body> | |
<div> | |
<h1>Hello, World!</h1> | |
<hr /> | |
<h2> Session data : </h2> | |
<div> | |
{$ses} | |
</div> | |
</div> | |
</body> | |
</html>; | |
return Message\Response\html($html); | |
} | |
} |
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
use namespace Nuxed\Http\Server; | |
use namespace Nuxed\Http\Message; | |
use namespace Nuxed\Http\Router\Generator; | |
final class HomePageHandler implements Server\IRequestHandler { | |
use Server\RequestHandlerMiddlewareTrait; | |
public function __construct( | |
private Generator\IUriGenerator $uri, | |
) {} | |
public async function handle( | |
Message\ServerRequest $request, | |
): Awaitable<Message\Response> { | |
$html = | |
<html> | |
<head> | |
<title>Hello, World!</title> | |
</head> | |
<body> | |
<div> | |
<h1>Hello, World!</h1> | |
<hr /> | |
<a href={$this->uri->generate('foo')}>Foo</a> | |
</div> | |
</body> | |
</html>; | |
return Message\Response\html($html); | |
} | |
} |
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
use namespace Nuxed\Http; | |
use namespace Nuxed\Kernel; | |
require __DIR__.'/../vendor/hh_autoload.hh'; | |
<<__EntryPoint>> | |
async function main(): Awaitable<void> { | |
$kernel = new Kernel\Kernel(); | |
$kernel->use(new Kernel\Extension\HttpExtension()); | |
$kernel->use(new Kernel\Extension\EventExtension()); | |
$kernel->use(new Kernel\Extension\SessionExtension()); | |
$kernel->use(new Kernel\Extension\CacheExtension(shape( | |
'cache_dir' => __DIR__.'/cache', | |
))); | |
$kernel->use(new Extension()); | |
$request = Http\Message\ServerRequest::capture(); | |
$response = await $kernel->handle($request); | |
await $kernel->emit($response); | |
await $kernel->terminate($request, $response); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment