/** * Constructor. * * @param IOInterface $io * @param TwigFactory $twigFactory * @param string $serverroot * @param string $documentroot * @param int $port * @param string $host */ public function __construct(IOInterface $io, $serverroot, $documentroot, $port, $host) { \Twig_Autoloader::register(); $this->io = $io; $this->port = $port; $this->host = $host; $this->serverroot = $serverroot; $this->documentroot = $documentroot; $this->buildTwig($serverroot); $this->requestHandler = new RequestHandler(function (Request $request) { $serverResponse = new ServerResponse(''); $serverRequest = new ServerRequest($request, $this->documentroot, $this->serverroot); try { $this->handleOnBeforeRequestFunction($serverRequest); $resourcePath = $serverRequest->getPathFilename(); if (file_exists($resourcePath) === true) { $serverResponse->setContent(file_get_contents($resourcePath)); $serverResponse->setContentType($serverRequest->getMimeType()); } else { $serverResponse->setStatusCode(404); $serverResponse->setContent($resourcePath); } $this->handleOnAfterRequestFunction($serverResponse); } catch (\Exception $e) { $serverResponse->setStatusCode(500); $serverResponse->setContent($e->getMessage()); } $this->logRequest($serverRequest->getIp(), $serverRequest->getPath(), $serverResponse->getStatusCode()); return $this->buildFinalResponse($serverResponse); }); $this->requestHandler->listen($port, $host)->enableHttpFoundationRequest(); }
public function testResponse() { $serverResponse = new ServerResponse('Hola user!'); $this->assertEquals('Hola user!', $serverResponse->getContent()); $this->assertEquals(200, $serverResponse->getStatusCode()); $this->assertEquals('text/html', $serverResponse->getContentType()); $serverResponse->setContent('Hola user again!'); $this->assertEquals('Hola user again!', $serverResponse->getContent()); $serverResponse->setStatusCode(204); $this->assertEquals(204, $serverResponse->getStatusCode()); $serverResponse->setContentType('text/xml'); $this->assertEquals('text/xml', $serverResponse->getContentType()); }