fromGlobals() public static method

If any argument is not supplied, the corresponding superglobal value will be used. The ServerRequest created is then passed to the fromServer() method in order to marshal the request URI and headers.
See also: fromServer()
public static fromGlobals ( array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null ) : ServerRequest
$server array $_SERVER superglobal
$query array $_GET superglobal
$body array $_POST superglobal
$cookies array $_COOKIE superglobal
$files array $_FILES superglobal
return ServerRequest
コード例 #1
0
 /**
  * @test
  */
 public function datesAreReadFromQuery()
 {
     $shortCode = 'abc123';
     $this->visitsTracker->info($shortCode, new DateRange(null, new \DateTime('2016-01-01 00:00:00')))->willReturn([])->shouldBeCalledTimes(1);
     $response = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode)->withQueryParams(['endDate' => '2016-01-01 00:00:00']), new Response());
     $this->assertEquals(200, $response->getStatusCode());
 }
コード例 #2
0
ファイル: Server.php プロジェクト: spajak/flow
 /**
  * Get the request from server.
  *
  * @return RequestInterface Server request
  */
 public function getRequest()
 {
     if (!isset($this->request)) {
         $this->request = ServerRequestFactory::fromGlobals();
     }
     return $this->request;
 }
コード例 #3
0
 /**
  * Get Request
  *
  * @param array $routeVariables Route Variables
  *
  * @return HttpMessages_CraftRequest Request
  */
 public function getRequest(array $routeVariables = [])
 {
     $route = craft()->httpMessages_routes->getRoute($routeVariables);
     $serverRequest = \Zend\Diactoros\ServerRequestFactory::fromGlobals();
     $request = HttpMessages_RequestFactory::fromRequest($serverRequest);
     return $request->withRoute($route);
 }
コード例 #4
0
 /**
  * @test
  */
 public function anExceptionsReturnsErrorResponse()
 {
     $page = 3;
     $this->service->listShortUrls($page, null, [], null)->willThrow(\Exception::class)->shouldBeCalledTimes(1);
     $response = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withQueryParams(['page' => $page]), new Response());
     $this->assertEquals(500, $response->getStatusCode());
 }
コード例 #5
0
 public function testOversizedResponse()
 {
     $expectedClass = 'NetworkJs\\Response\\OversizedResponse';
     $moduleName = 'download';
     $this->assertInstanceOf($expectedClass, ResponseFactory::fromValues($moduleName, 10, 5));
     $this->assertInstanceOf($expectedClass, ResponseFactory::fromRequest(ServerRequestFactory::fromGlobals(null, ['module' => $moduleName, 'size' => 10]), 5));
 }
コード例 #6
0
ファイル: IPTest.php プロジェクト: juliangut/janitor
 public function testIsNotExcluded()
 {
     $request = ServerRequestFactory::fromGlobals();
     $request = $request->withHeader('X-Forwarded', '80.80.80.80');
     $excluder = new IP($this->excludedIPs, ['10.10.10.10']);
     self::assertFalse($excluder->isExcluded($request));
 }
コード例 #7
0
ファイル: HandlesHttpRequests.php プロジェクト: yuloh/crux
 /**
  * Handles the request and returns a response.
  *
  * @param  \Psr\Http\Message\ServerRequestInterface|null $request
  * @param  \Psr\Http\Message\ResponseInterface|null      $response
  *
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function handle(ServerRequestInterface $request = null, ResponseInterface $response = null)
 {
     $request = $request ?: ServerRequestFactory::fromGlobals();
     $response = $response ?: new Response();
     $this->emit(Event::REQUEST_RECEIVED, $request, $response);
     return $this->dispatchThroughMiddleware($request, $response);
 }
コード例 #8
0
 /**
  * @test
  */
 public function invalidApiKeyReturnsErrorResponse()
 {
     $this->apiKeyService->getByKey('foo')->willReturn((new ApiKey())->setEnabled(false))->shouldBeCalledTimes(1);
     $request = ServerRequestFactory::fromGlobals()->withParsedBody(['apiKey' => 'foo']);
     $response = $this->action->__invoke($request, new Response());
     $this->assertEquals(401, $response->getStatusCode());
 }
コード例 #9
0
 /**
  * @test
  */
 public function tagsListIsReturnedIfCorrectShortCodeIsProvided()
 {
     $shortCode = 'abc123';
     $this->shortUrlService->setTagsByShortCode($shortCode, [])->willReturn(new ShortUrl())->shouldBeCalledTimes(1);
     $response = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', 'abc123')->withParsedBody(['tags' => []]), new Response());
     $this->assertEquals(200, $response->getStatusCode());
 }
コード例 #10
0
ファイル: Psr7Factory.php プロジェクト: seytar/psx
 public static function createRequest(RequestInterface $request)
 {
     $psrRequest = ServerRequestFactory::fromGlobals()->withUri($request->getUri())->withMethod($request->getMethod())->withBody($request->getBody());
     foreach ($request->getHeaders() as $name => $values) {
         $psrRequest = $psrRequest->withHeader($name, $values);
     }
     return $psrRequest;
 }
コード例 #11
0
ファイル: ProxyTest.php プロジェクト: allantatter/php-proxy
 /**
  * @test
  */
 public function to_applies_filters()
 {
     $applied = false;
     $this->proxy->forward(ServerRequestFactory::fromGlobals())->filter(function ($request, $response) use(&$applied) {
         $applied = true;
     })->to('http://www.example.com');
     $this->assertTrue($applied);
 }
コード例 #12
0
 /**
  * @test
  */
 public function aGenericExceptionWillReturnError()
 {
     $this->urlShortener->urlToShortCode(Argument::type(Uri::class), Argument::type('array'))->willThrow(\Exception::class)->shouldBeCalledTimes(1);
     $request = ServerRequestFactory::fromGlobals()->withParsedBody(['longUrl' => 'http://www.domain.com/foo/bar']);
     $response = $this->action->__invoke($request, new Response());
     $this->assertEquals(500, $response->getStatusCode());
     $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);
 }
コード例 #13
0
ファイル: App.php プロジェクト: prezto/espresso
 /**
  *
  */
 public function __construct()
 {
     parent::__construct();
     $this->_middleware = new \ArrayObject();
     $this->_routeMap = new RouteMap();
     $this->_request = ServerRequestFactory::fromGlobals();
     $this->_response = new Response();
 }
コード例 #14
0
ファイル: UrlBuilderTest.php プロジェクト: laasti/directions
 public function testNamedRoutes()
 {
     $routes = new RouteCollection(new HttpMessageStrategy());
     $routes->addRoute('GET', '/user/{id}', function () {
     })->setName('UserProfile');
     $builder = new UrlBuilder(ServerRequestFactory::fromGlobals($this->fakeServerParams()), $routes);
     $this->assertEquals('/site/user/23', $builder->createByName('UserProfile', ['id' => 23]));
 }
コード例 #15
0
 /**
  * @test
  */
 public function nonRestPathsAreNotProcessed()
 {
     $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/non-rest'));
     $test = $this;
     $this->middleware->__invoke($request, new Response(), function ($req) use($request, $test) {
         $test->assertSame($request, $req);
     });
 }
コード例 #16
0
ファイル: CacheWareTest.php プロジェクト: juliangut/cacheware
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     $this->request = ServerRequestFactory::fromGlobals();
     $this->response = new Response();
     $this->callback = function ($request, $response) {
         return $response;
     };
 }
コード例 #17
0
ファイル: PHPDiFactory.php プロジェクト: amir20202000/penny
 /**
  * Initial setup of DI\ContainerBuilder.
  *
  * @param bool $annotation annotation usage.
  *
  * @return DI\ContainerBuilder
  */
 protected static function initialSetupContainerBuilder($annotation)
 {
     $builder = new DI\ContainerBuilder();
     $builder->useAnnotations($annotation);
     $builder->addDefinitions(['request' => ServerRequestFactory::fromGlobals(), 'response' => DI\object(Response::class), 'http_flow_event' => DI\object(ZendHttpFlowEvent::class)->constructor('bootstrap', DI\get('request'), DI\get('response')), 'event_manager' => DI\object(ZendEvmProxy::class), 'dispatcher' => DI\factory(function ($container) {
         return new Dispatcher($container->get('router'), $container);
     })]);
     return $builder;
 }
コード例 #18
0
 /**
  * Test missing routes not being caught.
  *
  * @expectedException \Cake\Routing\Exception\MissingRouteException
  */
 public function testMissingRouteNotCaught()
 {
     $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/missing']);
     $response = new Response();
     $next = function ($req, $res) {
     };
     $middleware = new RoutingMiddleware();
     $middleware($request, $response, $next);
 }
コード例 #19
0
 /**
  * @test
  */
 public function unexpectedExceptionWillReturnError()
 {
     $shortCode = 'abc123';
     $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(\Exception::class)->shouldBeCalledTimes(1);
     $request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
     $response = $this->action->__invoke($request, new Response());
     $this->assertEquals(500, $response->getStatusCode());
     $this->assertTrue(strpos($response->getBody()->getContents(), RestUtils::UNKNOWN_ERROR) > 0);
 }
コード例 #20
0
 public function testPostRequestSavesPostDataInSessionAndReturnsRedirect()
 {
     $request = ServerRequestFactory::fromGlobals(null, null, $this->fullData)->withMethod('POST')->withUri(new Uri('/foo/bar'));
     $this->assertFalse($this->session->offsetExists(Contact::PRG_DATA));
     $resp = $this->contact->dispatch($request, new Response());
     $this->assertInstanceOf(RedirectResponse::class, $resp);
     $this->assertEquals(['/foo/bar'], $resp->getHeader('Location'));
     $this->assertTrue($this->session->offsetExists(Contact::PRG_DATA));
 }
コード例 #21
0
 public function testWithoutSuffix()
 {
     $request = ServerRequestFactory::fromGlobals();
     $response = new Response();
     $rtime = new ResponseTime(['suffix' => false]);
     $response = $rtime($request, $response);
     $this->assertInstanceOf(Response::class, $response);
     $this->assertNotContains('ms', $response->getHeaders()['X-Response-Time'][0]);
 }
コード例 #22
0
 public function register()
 {
     $this->forge->singleton(['routing', Routing::class], function () {
         $routing = new Routing(new HttpRequest(ServerRequestFactory::fromGlobals()), new HttpResponse(new Response()));
         $routing->makeDispatcher(HTTP . "routes.php");
         return $routing;
     });
     $this->provides += [Routing::class];
 }
コード例 #23
0
 /**
  * Use the register method to register items with the container via the
  * protected $this->container property or the `getContainer` method
  * from the ContainerAwareTrait.
  */
 public function register()
 {
     $this->container->share(ServerRequestInterface::class, function () {
         return ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
     });
     $this->container->share(ResponseInterface::class, function () {
         return new Response();
     });
 }
コード例 #24
0
ファイル: PreviewActionTest.php プロジェクト: shlinkio/shlink
 /**
  * @test
  */
 public function previewExceptionReturnsNotFound()
 {
     $shortCode = 'abc123';
     $this->urlShortener->shortCodeToUrl($shortCode)->willThrow(PreviewGenerationException::class)->shouldBeCalledTimes(1);
     $resp = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), new Response(), function ($req, $resp) {
         return $resp;
     });
     $this->assertEquals(500, $resp->getStatusCode());
 }
コード例 #25
0
ファイル: QrCodeActionTest.php プロジェクト: shlinkio/shlink
 /**
  * @test
  */
 public function aCorrectRequestReturnsTheQrCodeResponse()
 {
     $shortCode = 'abc123';
     $this->urlShortener->shortCodeToUrl($shortCode)->willReturn(new ShortUrl())->shouldBeCalledTimes(1);
     $resp = $this->action->__invoke(ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode), new Response(), function ($req, $resp) {
         return $resp;
     });
     $this->assertInstanceOf(QrCodeResponse::class, $resp);
     $this->assertEquals(200, $resp->getStatusCode());
 }
コード例 #26
0
ファイル: Cycle.php プロジェクト: chuchiy/phpex
 public function __construct($request = null, $responseHeaderSender = null, $responseBody = 'php://output')
 {
     $this->request = $request ? $request : ServerRequestFactory::fromGlobals();
     $this->responseBody = $responseBody;
     $this->responseHeaderSender = $responseHeaderSender ? $responseHeaderSender : [new ResponseSender(), 'emitStatusLineAndHeaders'];
     $this->client = new Client($this->request);
     $parsedbody = $this->request->getParsedBody() ? $this->request->getParsedBody() : [];
     $this->params = $this->request->getCookieParams() + $parsedbody + $this->request->getQueryParams();
     $this->reply = new Reply();
 }
コード例 #27
0
ファイル: Router.php プロジェクト: monomelodies/reroute
 /**
  * Constructor. In most cases you won't need to worry about the constructor
  * arguments, but optionally you can pass a path part all routes _must_
  * match (e.g. if Reroute only needs to catch parts of your project).
  *
  * @param string $url The path part _all_ URLs for this router must fall
  *  under in order to match.
  * @param League\Pipeline\Pipeline $pipe Optional pipeline to chain onto.
  * @return void
  */
 public function __construct($url = null, Pipeline $pipe = null)
 {
     $this->request = ServerRequestFactory::fromGlobals();
     $this->url = $this->normalize($url);
     $this->pipeline = new PipelineBuilder();
     if (isset($pipe)) {
         $this->pipeline->add($pipe);
     }
     self::$matchedArguments = [];
 }
コード例 #28
0
 /**
  * {@inheritDoc}
  */
 public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null)
 {
     $request = parent::fromGlobals($server, $query, $body, $cookies, $files);
     list($base, $webroot) = static::getBase($request);
     $request = $request->withAttribute('base', $base)->withAttribute('webroot', $webroot);
     if ($base) {
         $request = static::updatePath($base, $request);
     }
     return $request;
 }
コード例 #29
0
ファイル: DiactorosDefinition.php プロジェクト: Mosaic/Mosaic
 /**
  * @return array|Definition[]
  */
 public function getDefinitions() : array
 {
     return [RequestContract::class => function () {
         return new Request(ServerRequestFactory::fromGlobals());
     }, ResponseContract::class => function () {
         return new Response(new DiactorosResponse());
     }, ResponseFactoryContract::class => function () {
         return new ResponseFactory();
     }];
 }
コード例 #30
0
ファイル: RedirectTest.php プロジェクト: juliangut/janitor
 public function testRedirectScheduled()
 {
     $watcher = $this->getMockBuilder(ScheduledWatcherInterface::class)->disableOriginalConstructor()->getMock();
     $watcher->expects(self::once())->method('getEnd')->will(self::returnValue(new \DateTime()));
     $handler = new Redirect('http://example.com');
     /* @var \Psr\Http\Message\ResponseInterface $response */
     $response = $handler(ServerRequestFactory::fromGlobals(), new Response('php://temp'), $watcher);
     self::assertEquals(302, $response->getStatusCode());
     self::assertTrue($response->hasHeader('Expires'));
 }