Context
Context refers to the request context, which is the object of the request level, each request generating a Context instance. The context is automatically destroyed after each request.
The context splits the information of different requests, ensuring that the data between requests does not get confused.
Different from the fpm application, there are different life cycles in the swowt, there are different context
Context
, and the请求
is not a single Http request.
Context interface
Context object implements the basic ContextInterface
interface, so you can use the context to access the data life cycle current request.
You can view the concrete context implementation class by looking at the implementation class functions of PHPStorm.
<?php declare(strict_types=1);
namespace Swoft\Contract;
/**
* Class ContextInterface
*
* @since 2.0
*/
interface ContextInterface
{
/**
* Get value from context
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get(string $key, $default = null);
/**
* Set value to context
*
* @param string $key
* @param mixed $value
*/
public function set(string $key, $value): void;
/**
* Set multi value to context
*
* @param array $map
* [key => value]
*/
public function setMulti(array $map): void;
/**
* Unset key
*
* @param string $key
*/
public function unset(string $key): void
/**
* Clear resource
*/
public function clear(): void;
}
how to use
As long as you are in a coroutine environment, you can quickly get context objects through context()/Context::get()
anywhere.
context()->set('age', 1);
...
$age = context()->get('age');
scenes to be used
- For example, in the http server, after the middleware authenticates the user, the current user information is placed in the context.
- Link tracking
traceid
spanid
is stored in context.
Common context
Http server
The request context in the http server is an instance of Swoft\Http\Server\HttpContext
. It extends the getRequest()
and getResponse()
methods to quickly obtain the http request and response object of the PSR-7 interface specification.
/**
* @return Request
*/
public function getRequest(): Request
{
return $this->request;
}
/**
* @return Response
*/
public function getResponse(): Response
{
return $this->response;
}
Ws server
- Handshake request
The context is an Swoft\WebSocket\Server\Context\WsHandshakeContext
. It is basically similar to http context and has getRequest()
and getResponse()
methods.
- Message request
The context is an Swoft\WebSocket\Server\Context\WsMessageContext
. It also has a custom extension method for getting the data in the message request.
...
/**
* @return int
*/
public function getFd(): int
{
return $this->request->getFd();
}
/**
* @return Frame
*/
public function getFrame(): Frame
{
return $this->request->getFrame();
}
/**
* Get message object.
* Notice: Available only during the messaging phase
*
* @return Message
*/
public function getMessage(): Message
{
return $this->request->getMessage();
}
/**
* @return Request
*/
public function getRequest(): Request
{
return $this->request;
}
/**
* @param Request $request
*/
public function setRequest(Request $request): void
{
$this->request = $request;
}
/**
* @return Response
*/
public function getResponse(): Response
{
return $this->response;
}
Note that the
Request
Response
here refers to the request and response objects of the message phase, which is different from the Http request object when opening the connection.