本文整理汇总了PHP中Psr\Http\Message\ServerRequestInterface::getAttributes方法的典型用法代码示例。如果您正苦于以下问题:PHP ServerRequestInterface::getAttributes方法的具体用法?PHP ServerRequestInterface::getAttributes怎么用?PHP ServerRequestInterface::getAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ServerRequestInterface
的用法示例。
在下文中一共展示了ServerRequestInterface::getAttributes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* Execute the middleware. Don't call this method directly; it is used by the `Application` internally.
*
* @internal
*
* @param ServerRequestInterface $request The request object
* @param ResponseInterface $response The response object
* @param callable $next The next middleware handler
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$attributes = $request->getAttributes();
if (!isset($attributes['command'])) {
switch (strtoupper($request->getMethod())) {
case 'GET':
$params = new Registry($request->getQueryParams());
break;
case 'POST':
default:
$params = new Registry($request->getAttributes());
break;
}
$extension = ucfirst(strtolower($params->get('option', 'Article')));
$action = ucfirst(strtolower($params->get('task', 'display')));
$entity = $params->get('entity', 'error');
$id = $params->get('id', null);
$commandClass = "\\Joomla\\Extension\\{$extension}\\Command\\{$action}Command";
if (class_exists($commandClass)) {
$command = new $commandClass($entity, $id, $response->getBody());
$request = $request->withAttribute('command', $command);
}
// @todo Emit afterRouting event
}
return $next($request, $response);
}
示例2: getData
/**
* {@inheritdoc}
*/
public function getData()
{
$headers = [];
foreach ($this->request->getHeaders() as $name => $values) {
$headers[$name] = implode(', ', $values);
}
$data = ['SERVER' => $this->request->getServerParams(), 'QUERY' => $this->request->getQueryParams(), 'COOKIES' => $this->request->getCookieParams(), 'HEADERS' => $headers, 'ATTRIBUTES' => $this->request->getAttributes()];
return $data;
}
示例3: handle
/**
* Execute the middleware. Don't call this method directly; it is used by the `Application` internally.
*
* @internal
*
* @param ServerRequestInterface $request The request object
* @param ResponseInterface $response The response object
* @param callable $next The next middleware handler
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$attributes = $request->getAttributes();
if (!isset($attributes['command'])) {
try {
/** @var RepositoryInterface $repository */
$repository = $this->container->get('Repository')->forEntity(Page::class);
/** @var Page[] $pages */
$pages = $repository->getAll();
$router = new Router();
foreach ($pages as $page) {
$router->get($this->expandUrl($page->url, $page), function () use($page) {
return $page;
});
}
$path = preg_replace('~^/.*?index.php/?~', '', $request->getUri()->getPath());
$route = $router->parseRoute($path);
$page = $route['controller']();
$vars = $route['vars'];
$command = new DisplayPageCommand($page->id, $vars, $request, $response->getBody(), $this->container);
$request = $request->withAttribute('command', $command);
// @todo Emit afterRouting event
} catch (InvalidArgumentException $e) {
// Do nothing
}
}
return $next($request, $response);
}
示例4: createRequest
/**
* {@inheritdoc}
*/
public function createRequest(ServerRequestInterface $psrRequest)
{
$parsedBody = $psrRequest->getParsedBody();
$parsedBody = is_array($parsedBody) ? $parsedBody : array();
$request = new Request($psrRequest->getQueryParams(), $parsedBody, $psrRequest->getAttributes(), $psrRequest->getCookieParams(), $this->getFiles($psrRequest->getUploadedFiles()), $psrRequest->getServerParams(), $psrRequest->getBody()->__toString());
$request->headers->replace($psrRequest->getHeaders());
return $request;
}
示例5: handle
/**
* Execute the middleware. Don't call this method directly; it is used by the `Application` internally.
*
* @internal
*
* @param ServerRequestInterface $request The request object
* @param ResponseInterface $response The response object
* @param callable $next The next middleware handler
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$attributes = $request->getAttributes();
if (!isset($attributes['command']) && $this->isLegacy()) {
// @todo Emit afterRouting event
}
return $next($request, $response);
}
示例6: convertRequestFromPsr7
/**
* Converts a PSR-7 request into an OAuth2 request.
*
* @param ServerRequestInterface $psrRequest
* @return Request
*/
public static function convertRequestFromPsr7(ServerRequestInterface $psrRequest)
{
$headers = [];
foreach ($psrRequest->getHeaders() as $header => $value) {
$headers[$header] = implode(';', $value);
}
return new Request($psrRequest->getQueryParams(), is_array($psrRequest->getParsedBody()) ? $psrRequest->getParsedBody() : [], $psrRequest->getAttributes(), $psrRequest->getCookieParams(), self::getFiles($psrRequest->getUploadedFiles()), $psrRequest->getServerParams(), $psrRequest->getBody()->__toString(), $headers);
}
示例7: createRequest
public static function createRequest(PsrServerRequestInterface $psrRequest)
{
$request = new Request(new Uri($psrRequest->getUri()), $psrRequest->getMethod(), $psrRequest->getHeaders(), $psrRequest->getBody());
$attributes = $psrRequest->getAttributes();
foreach ($attributes as $name => $value) {
$request->setAttribute($name, $value);
}
return $request;
}
示例8: __invoke
/**
* Flatten all input from the request.
*
* @param ServerRequestInterface $request
*
* @return array
*/
public function __invoke(ServerRequestInterface $request)
{
$attrs = $request->getAttributes();
$body = $this->body($request);
$cookies = $request->getCookieParams();
$query = $request->getQueryParams();
$uploads = $request->getUploadedFiles();
// Order matters here! Important values go last!
return array_replace($query, $body, $uploads, $cookies, $attrs);
}
示例9: getRouteParameters
/**
* Returns a map of routing parameters extracted from the request attributes (which mast have been set previously by
* a router).
*
* @param ServerRequestInterface $request
* @return array A map of name => value of all routing parameters set on the request.
*/
static function getRouteParameters(ServerRequestInterface $request)
{
return mapAndFilter($request->getAttributes(), function ($v, &$k) {
if ($k && $k[0] == '@') {
$k = substr($k, 1);
return $v;
}
return null;
});
}
示例10: __construct
/**
* Construct action request.
*
* @param \Psr\Http\Message\ServerRequestInterface $request Request to wrap.
*/
public function __construct(\Psr\Http\Message\ServerRequestInterface $request)
{
parent::__construct($request->getBody());
foreach ($request->getHeaders() as $name => $value) {
$this->setHeader($name, $value);
}
$this->protocolVersion = $request->getProtocolVersion();
$this->method = $request->getMethod();
$this->requestTarget = $request->getRequestTarget();
$this->uri = $request->getUri();
$this->attributes = $request->getAttributes();
$this->cookies = $request->getCookieParams();
$this->data = $request->getParsedBody();
$this->query = $request->getQueryParams();
$this->server = $request->getServerParams();
$this->files = $request->getUploadedFiles();
if (isset($this->server['SCRIPT_NAME'])) {
$this->attributes['basePath'] = dirname($this->server['SCRIPT_NAME']);
$this->attributes['scriptName'] = basename($this->server['SCRIPT_NAME']);
} else {
$this->attributes['basePath'] = '/';
$this->attributes['scriptName'] = 'index.php';
}
if (!isset($this->attributes['path'])) {
$this->attributes['path'] = self::findPath($this);
}
if (!isset($this->attributes['rewrite'])) {
$this->attributes['rewrite'] = false;
}
if (!isset($this->attributes['accepts'])) {
$this->attributes['accepts'] = [];
if (isset($this->server['HTTP_ACCEPT'])) {
$contentTypes = explode(',', $this->server['HTTP_ACCEPT']);
foreach ($contentTypes as $contentType) {
$contentType = explode(';', $contentType);
$this->attributes['accepts'][] = trim(strtolower($contentType[0]));
}
}
}
if (!isset($this->attributes['encodings'])) {
$this->attributes['encodings'] = [];
if (isset($this->server['HTTP_ACCEPT_ENCODING'])) {
$acceptEncodings = explode(',', $this->server['HTTP_ACCEPT_ENCODING']);
foreach ($acceptEncodings as $encoding) {
$this->attributes['encodings'][] = trim(strtolower($encoding));
}
}
}
}
示例11: handle
/**
* @param Request $request
* @param array $routeParams
*
* @return JsonResponse
*/
public function handle(Request $request, array $routeParams = [])
{
$params = ['data' => ['attributes' => $request->getAttributes()]];
$apiResponse = $this->apiClient->send(app('flarum.actor'), 'Flarum\\Api\\Actions\\Users\\CreateAction', $params);
$body = $apiResponse->getBody();
$statusCode = $apiResponse->getStatusCode();
$response = new JsonResponse($body, $statusCode);
if (!empty($body->data->attributes->isActivated)) {
$token = $this->bus->dispatch(new GenerateAccessToken($body->data->id));
// Extend the token's expiry to 2 weeks so that we can set a
// remember cookie
AccessToken::where('id', $token->id)->update(['expires_at' => new DateTime('+2 weeks')]);
return $this->withRememberCookie($response, $token->id);
}
return $response;
}
示例12: invoke
public function invoke($middleware, ServerRequestInterface $request, callable $next) : ResponseInterface
{
if (!$this->invoker) {
$this->invoker = $this->createInvoker();
}
$parameters = $request->getAttributes();
$parameters['request'] = $request;
$parameters['next'] = $next;
$newResponse = $this->invoker->call($middleware, $parameters);
if (is_string($newResponse)) {
// Allow direct string response
$newResponse = new HtmlResponse($newResponse);
} elseif (!$newResponse instanceof ResponseInterface) {
throw new \RuntimeException(sprintf('The controller did not return a response (expected %s, got %s)', ResponseInterface::class, is_object($newResponse) ? get_class($newResponse) : gettype($newResponse)));
}
return $newResponse;
}
示例13: handle
/**
* @param Request $request
* @param array $routeParams
* @return JsonResponse|EmptyResponse
*/
public function handle(Request $request, array $routeParams = [])
{
$params = array_only($request->getAttributes(), ['identification', 'password']);
$data = $this->apiClient->send(app('flarum.actor'), 'Flarum\\Api\\Actions\\TokenAction', $params)->getBody();
// TODO: The client needs to pass through exceptions(?) or the whole
// response so we can look at the response code. For now if there isn't
// any useful data we just assume it's a 401.
if (isset($data->userId)) {
// Extend the token's expiry to 2 weeks so that we can set a
// remember cookie
AccessToken::where('id', $data->token)->update(['expires_at' => new DateTime('+2 weeks')]);
event(new UserLoggedIn($this->users->findOrFail($data->userId), $data->token));
return $this->withRememberCookie(new JsonResponse($data), $data->token);
} else {
return new EmptyResponse(401);
}
}
示例14: createRequest
/**
* {@inheritdoc}
*/
public function createRequest(ServerRequestInterface $psrRequest)
{
$server = array();
$uri = $psrRequest->getUri();
if ($uri instanceof UriInterface) {
$server['SERVER_NAME'] = $uri->getHost();
$server['SERVER_PORT'] = $uri->getPort();
$server['REQUEST_URI'] = $uri->getPath();
$server['QUERY_STRING'] = $uri->getQuery();
}
$server['REQUEST_METHOD'] = $psrRequest->getMethod();
$server = array_replace($server, $psrRequest->getServerParams());
$parsedBody = $psrRequest->getParsedBody();
$parsedBody = is_array($parsedBody) ? $parsedBody : array();
$request = new Request($psrRequest->getQueryParams(), $parsedBody, $psrRequest->getAttributes(), $psrRequest->getCookieParams(), $this->getFiles($psrRequest->getUploadedFiles()), $server, $psrRequest->getBody()->__toString());
$request->headers->replace($psrRequest->getHeaders());
return $request;
}
示例15: __invoke
/**
* Flatten all input from the request
*
* @param ServerRequestInterface $request
*
* @return array
*/
public function __invoke(ServerRequestInterface $request)
{
$attrs = $request->getAttributes();
$body = $request->getParsedBody();
$cookies = $request->getCookieParams();
$query = $request->getQueryParams();
$uploads = $request->getUploadedFiles();
if (empty($body)) {
$body = [];
} elseif (is_object($body)) {
// Because the parsed body may also be represented as an object,
// additional parsing is required. This is a bit dirty but works
// very well for anonymous objects.
$body = json_decode(json_encode($body), true);
}
// Order matters here! Important values go last!
return array_replace($query, $body, $uploads, $cookies, $attrs);
}