Files
expose/app/HttpServer/Controllers/Controller.php
Marcel Pociot 28c4009dff wip
2020-04-25 11:42:40 +02:00

42 lines
1.0 KiB
PHP

<?php
namespace App\HttpServer\Controllers;
use Exception;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServerInterface;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use function GuzzleHttp\Psr7\stream_for;
abstract class Controller implements HttpServerInterface
{
public function onClose(ConnectionInterface $connection)
{
unset($connection->requestBuffer);
unset($connection->contentLength);
unset($connection->request);
}
public function onError(ConnectionInterface $connection, Exception $e)
{
}
public function onMessage(ConnectionInterface $from, $msg)
{
}
protected function getView(string $view, array $data)
{
$templatePath = implode(DIRECTORY_SEPARATOR, explode('.', $view));
$twig = new Environment(
new ArrayLoader([
'template' => file_get_contents(base_path('resources/views/'.$templatePath.'.twig')),
])
);
return stream_for($twig->render('template', $data));
}
}