/** * Method to boot Windwalker Application and get rid of global conflicts. * * @return void */ public function boot() { $autoload = __DIR__ . '/../vendor/autoload.php'; if (!is_file($autoload)) { exit('Please run <code>$ composer install</code> First.'); } include_once $autoload; include_once __DIR__ . '/../etc/define.php'; $uri = defined('WINDWALKER_TEST_URI') ? WINDWALKER_TEST_URI : 'http://windwalker.io/starter/flower/sakura'; $path = defined('WINDWALKER_TEST_SCRIPT_PATH') ? WINDWALKER_TEST_SCRIPT_PATH : '/starter/'; $uri = new \Windwalker\Uri\PsrUri($uri); $_SERVER['HTTP_HOST'] = $uri->getHost(); $_SERVER['REQUEST_URI'] = $uri->getPath(); $_SERVER['SCRIPT_NAME'] = $path; $_SERVER['PHP_SELF'] = $uri->getPath(); $request = ServerRequestFactory::createFromGlobals($_SERVER); $app = new TestApplication($request); define('WINDWALKER_DEBUG', true); $_SERVER['HTTP_HOST'] = 'windwalker.io'; $app->boot(); $app->bootRouting(); }
/** * Create a Server instance * * Creates a server instance from the callback and the following * PHP environmental values: * * @param callable $handler The server handler. * @param array $server The server variable. Typically this will be the $_SERVER superglobal. * @param array $query The GET uri query. Typically this will be the $_GET superglobal. * @param array $body The POST body. Typically this will be the $_POST superglobal. * @param array $cookies The cookies. Typically this will be the $_COOKIE superglobal * @param array $files The uploaded file data. Typically this will be the $_FILES superglobal. * * @return static The Server instance. */ public static function createFromGlobals($handler = null, array $server = array(), array $query = array(), array $body = array(), array $cookies = array(), array $files = array()) { $request = ServerRequestFactory::createFromGlobals($server, $query, $body, $cookies, $files); return new static($handler, $request); }
<?php /** * Part of Windwalker project. * * @copyright Copyright (C) 2016 LYRASOFT. All rights reserved. * @license GNU General Public License version 2 or later. */ use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; include_once __DIR__ . '/../../../../vendor/autoload.php'; //show($request = \Windwalker\Http\ServerRequestFactory::fromGlobals()); // //show($request->getUri()); $server = \Windwalker\Http\WebHttpServer::createFromRequest(function ($request, ResponseInterface $response, $finalHandler) { // $response = $response->withHeader('Content-Type', 'application/json'); // $response->getBody()->write('Hello World!'); $response = new \Windwalker\Http\Response\XmlResponse('<root><f>中文 World!</f></root>'); $response = $response->withHeader('asd', 123); $response = $finalHandler($request, $response); return $response; }, \Windwalker\Http\Request\ServerRequestFactory::createFromGlobals(), new \Windwalker\Http\Response\HtmlResponse()); //$server->getOutput() $server->listen(function ($request, $response) use($server) { return $server->compress($response); });
/** * Method to test prepareUri(). * * @param array $servers * @param array $headers * @param string $expected * * @covers \Windwalker\Http\Request\ServerRequestFactory::prepareUri * * @dataProvider prepareUri_Provider */ public function testPrepareUri($servers, $headers, $expected) { $uri = ServerRequestFactory::prepareUri($servers, $headers); $this->assertEquals($expected, $uri->__toString()); }
/** * Method to test getUriData(). * * @return void * * @covers \Windwalker\Http\WebHttpServer::getUriData */ public function testGetUriData() { $server = new WebHttpServer(function () { }, ServerRequestFactory::createFromGlobals(array('HTTPS' => 'off', 'SERVER_NAME' => 'example.com', 'SERVER_PORT' => '8080', 'QUERY_STRING' => '?a=b&c=d', 'REQUEST_URI' => '/flower/sakura/index.php/foo/bar?a=wrong', 'SCRIPT_NAME' => '/flower/sakura/index.php'))); $uri = $server->getUriData(); $this->assertEquals('http://example.com:8080/flower/sakura/index.php/foo/bar?a=b&c=d', $uri->full); $this->assertEquals('http://example.com:8080/flower/sakura/index.php/foo/bar', $uri->current); $this->assertEquals('index.php', $uri->script); $this->assertEquals('http://example.com:8080/flower/sakura', $uri->root); $this->assertEquals('foo/bar', $uri->route); $this->assertEquals('http://example.com:8080', $uri->host); $this->assertEquals('/flower/sakura', $uri->path); $server = new WebHttpServer(function () { }, new ServerRequest(array('SCRIPT_NAME' => '/flower/sakura/index.php'), array(), 'http://example.com:8080/flower/sakura/index.php/foo/bar?a=b&c=d', 'GET')); $this->assertEquals($uri, $server->getUriData()); }