本文整理汇总了PHP中Slim\Http\Request::createFromEnvironment方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::createFromEnvironment方法的具体用法?PHP Request::createFromEnvironment怎么用?PHP Request::createFromEnvironment使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Slim\Http\Request
的用法示例。
在下文中一共展示了Request::createFromEnvironment方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: requestFactory
/**
* @param $method
* @param $path
* @param $body
* @param $options
* @return Request
*/
protected function requestFactory($method, $path, $body = [], $options = [])
{
$uri = Uri::createFromString($path);
$headers = new Headers();
$cookies = [];
$_POST['_METHOD'] = $method;
if (strtolower($method) != 'get' && is_array($body)) {
foreach ($body as $key => $value) {
$_POST[$key] = $value;
}
}
$envMethod = 'POST';
if (strtolower($method) == 'get') {
$envMethod = 'GET';
}
$env = Environment::mock(['REQUEST_URI' => $path, 'REQUEST_METHOD' => $envMethod, 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo']);
$serverParams = $env->all();
$body = $this->buildBody($body);
//echo $body->getContents();
// @todo
// $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, []);
$request = Request::createFromEnvironment($env);
unset($_POST);
return $request;
}
示例2: request
/**
* Perform request
*
* @param string $method
* @param string $uri
* @param array $params
* @param array $server
* @param string $content
*
* @throws \Slim\Exception\MethodNotAllowedException
* @throws \Slim\Exception\NotFoundException
*/
public function request($method, $uri, array $params = [], array $server = [], $content = null)
{
$method = strtoupper($method);
switch ($method) {
case 'POST':
case 'PUT':
case 'PATCH':
case 'DELETE':
$this->server['slim.input'] = http_build_query($params);
$query = '';
break;
case 'GET':
default:
$query = http_build_query($params);
break;
}
$server = array_merge($this->server, $server, ['CONTENT_TYPE' => 'application/json', 'REQUEST_URI' => $uri, 'REQUEST_METHOD' => $method, 'QUERY_STRING' => $query]);
$env = Http\Environment::mock($server);
$request = Http\Request::createFromEnvironment($env);
$response = new Http\Response();
// dirty hack to set body of request :(
if (!is_null($content)) {
\Closure::bind(function ($request) use($content) {
$request->bodyParsed = $content;
}, null, $request)->__invoke($request);
}
$response = $this->app->__invoke($request, $response);
$this->request = $request;
$this->response = $response;
}
示例3: register
/**
* {@inheritdoc}
*/
public function register()
{
$this->getContainer()->share('settings', function () {
return new Collection($this->defaultSettings);
});
$this->getContainer()->share('environment', function () {
return new Environment($_SERVER);
});
$this->getContainer()->share('request', function () {
return Request::createFromEnvironment($this->getContainer()->get('environment'));
});
$this->getContainer()->share('response', function () {
$headers = new Headers(['Content-Type' => 'text/html']);
$response = new Response(200, $headers);
return $response->withProtocolVersion($this->getContainer()->get('settings')['httpVersion']);
});
$this->getContainer()->share('router', function () {
return new Router();
});
$this->getContainer()->share('foundHandler', function () {
return new RequestResponse();
});
$this->getContainer()->share('errorHandler', function () {
return new Error($this->getContainer()->get('settings')['displayErrorDetails']);
});
$this->getContainer()->share('notFoundHandler', function () {
return new NotFound();
});
$this->getContainer()->share('notAllowedHandler', function () {
return new NotAllowed();
});
$this->getContainer()->share('callableResolver', function () {
return new CallableResolver($this->getContainer());
});
}
示例4: setUp
public function setUp()
{
$this->encryption = Mockery::mock(CookieEncryptionInterface::class);
$this->request = Request::createFromEnvironment(Environment::mock());
$this->response = new Response();
$this->capturedRequest = null;
}
示例5: setUp
public function setUp()
{
$this->request = Request::createFromEnvironment(Environment::mock());
$this->response = new Response();
$this->logger = new MemoryLogger();
$this->config = ['error' => 'critical', 'not-allowed' => 'info', 'not-found' => 'info'];
}
示例6: requestFactory
public function requestFactory($method, $path)
{
$environment = Environment::mock(['REQUEST_METHOD' => $method, 'REQUEST_URI' => $path, 'QUERY_STRING' => 'foo=bar']);
$request = Request::createFromEnvironment($environment);
$request->withMethod('GET');
return $request;
}
示例7: runApp
/**
* Process the application given a request method and URI
*
* @param string $requestMethod the request method (e.g. GET, POST, etc.)
* @param string $requestUri the request URI
* @param array|object|null $requestData the request data
* @return \Slim\Http\Response
*/
public function runApp($requestMethod, $requestUri, $requestData = null)
{
// Create a mock environment for testing with
$environment = Environment::mock(['REQUEST_METHOD' => $requestMethod, 'REQUEST_URI' => $requestUri]);
// Set up a request object based on the environment
$request = Request::createFromEnvironment($environment);
// Add request data, if it exists
if (isset($requestData)) {
$request = $request->withParsedBody($requestData);
}
// Set up a response object
$response = new Response();
// Use the application settings
$settings = (require __DIR__ . '/../../src/settings.php');
// Instantiate the application
$app = new App($settings);
// Set up dependencies
require __DIR__ . '/../../src/dependencies.php';
// Register middleware
if ($this->withMiddleware) {
require __DIR__ . '/../../src/middleware.php';
}
// Register routes
require __DIR__ . '/../../src/routes.php';
// Process the application
$response = $app->process($request, $response);
// Return the response
return $response;
}
示例8: setUp
public function setUp()
{
$container = new \Slim\Container(include "./Skeleton/Config/test.config.php");
$this->controller = $container[DefaultController::class];
$this->response = new Response();
$this->request = Request::createFromEnvironment(Environment::mock());
$this->args = [];
}
示例9: testReturnStream
/**
* @dataProvider environmentsProvider
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @param Environment $env
* @param string $output
*/
public function testReturnStream($env, $output)
{
$request = Request::createFromEnvironment($env);
ob_start();
tao_helpers_Http::returnStream($this->getStream(), null, $request);
$result = ob_get_clean();
$this->assertEquals($output, $result);
}
示例10: __invoke
/**
* @param \swoole_http_request $request
* @param \swoole_http_response $response
* @throws \Exception
*/
public function __invoke($request, $response)
{
$this->app->getContainer()['environment'] = $this->app->getContainer()->factory(function () {
return new Environment($_SERVER);
});
$this->app->getContainer()['request'] = $this->app->getContainer()->factory(function ($container) {
return Request::createFromEnvironment($container['environment']);
});
$this->app->getContainer()['response'] = $this->app->getContainer()->factory(function ($container) {
$headers = new Headers(['Content-Type' => 'text/html']);
$response = new Response(200, $headers);
return $response->withProtocolVersion($container->get('settings')['httpVersion']);
});
/**
* @var ResponseInterface $appResponse
*/
$appResponse = $this->app->run(true);
// set http header
foreach ($appResponse->getHeaders() as $key => $value) {
$filter_header = function ($header) {
$filtered = str_replace('-', ' ', $header);
$filtered = ucwords($filtered);
return str_replace(' ', '-', $filtered);
};
$name = $filter_header($key);
foreach ($value as $v) {
$response->header($name, $v);
}
}
// set http status
$response->status($appResponse->getStatusCode());
// send response to browser
if (!$this->isEmptyResponse($appResponse)) {
$body = $appResponse->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
$settings = $this->app->getContainer()->get('settings');
$chunkSize = $settings['responseChunkSize'];
$contentLength = $appResponse->getHeaderLine('Content-Length');
if (!$contentLength) {
$contentLength = $body->getSize();
}
$totalChunks = ceil($contentLength / $chunkSize);
$lastChunkSize = $contentLength % $chunkSize;
$currentChunk = 0;
while (!$body->eof() && $currentChunk < $totalChunks) {
if (++$currentChunk == $totalChunks && $lastChunkSize > 0) {
$chunkSize = $lastChunkSize;
}
$response->write($body->read($chunkSize));
if (connection_status() != CONNECTION_NORMAL) {
break;
}
}
$response->end();
}
}
示例11: testRunWithoutObjTypeIs404
/**
*
*/
public function testRunWithoutObjTypeIs404()
{
$request = Request::createFromEnvironment(Environment::mock());
$response = new Response();
$res = $this->obj->run($request, $response);
$this->assertEquals(404, $res->getStatusCode());
$res = $this->obj->results();
$this->assertFalse($res['success']);
}
示例12: testGetPayload
/**
* @dataProvider environmentsProvider
* @preserveGlobalState disabled
*
* @param Environment $env
*/
public function testGetPayload($env)
{
$request = Request::createFromEnvironment($env);
$datatableRequest = new DatatableRequest($request);
$datatablePayload = new ConcreteDatatablePayload($datatableRequest);
$datatablePayload->setSearchService($this->getSearchServiceMock($datatableRequest));
$payload = $datatablePayload->getPayload();
$this->assertTrue(is_array($payload));
}
示例13: testRunWithInvalidCredentials
/**
* Assert that
*/
public function testRunWithInvalidCredentials()
{
$this->createUser('foo', 'foobar');
$request = Request::createFromEnvironment(Environment::mock(['QUERY_STRING' => 'username=test&password=test123']));
$response = new Response();
$res = $this->obj->run($request, $response);
$this->assertFalse($this->obj->success());
$this->assertEquals(403, $res->getStatusCode());
}
示例14: testRunWithInvalidRecaptchaReturns404
public function testRunWithInvalidRecaptchaReturns404()
{
$request = Request::createFromEnvironment(Environment::mock(['QUERY_STRING' => 'token=foobar&username=foobar&password=foo&password_confirm=foo&g-recaptcha-response=foobar']));
$response = new Response();
$res = $this->obj->run($request, $response);
$this->assertEquals(404, $res->getStatusCode());
$res = $this->obj->results();
$this->assertFalse($res['success']);
}
示例15: __construct
/**
* Create new container
*
* @param array $settings Associative array of settings. User settings are in a 'settings' sub-array
*/
public function __construct($settings = [])
{
$userSettings = [];
if (isset($settings['settings'])) {
$userSettings = $settings['settings'];
unset($settings['settings']);
}
// Add settings factory that also collects the default settings
$defaultSettings = $this->defaultSettings;
$settings['factories']['settings'] = function ($c) use($userSettings, $defaultSettings) {
return array_merge($defaultSettings, $userSettings);
};
// Add default services if they aren't added already
if (!isset($settings['environment'])) {
$settings['factories']['environment'] = function ($c) {
return new Environment($_SERVER);
};
}
if (!isset($settings['request'])) {
$settings['factories']['request'] = function ($c) {
return Request::createFromEnvironment($c['environment']);
};
}
if (!isset($settings['response'])) {
$settings['factories']['response'] = function ($c) {
$headers = new Headers(['Content-Type' => 'text/html']);
$response = new Response(200, $headers);
return $response->withProtocolVersion($c['settings']['httpVersion']);
};
}
if (!isset($settings['router'])) {
$settings['factories']['router'] = function ($c) {
return new Router();
};
}
if (!isset($settings['callableResolver'])) {
$settings['factories']['callableResolver'] = function ($c) {
return new CallableResolver($c);
};
}
if (!isset($settings['foundHandler'])) {
$settings['invokables']['foundHandler'] = RequestResponse::class;
}
if (!isset($settings['errorHandler'])) {
$settings['factories']['errorHandler'] = function ($c) {
return new Error($c->get('settings')['displayErrorDetails']);
};
}
if (!isset($settings['notFoundHandler'])) {
$settings['invokables']['notFoundHandler'] = NotFound::class;
}
if (!isset($settings['notAllowedHandler'])) {
$settings['invokables']['notAllowedHandler'] = NotAllowed::class;
}
parent::__construct($settings);
}