MediaWiki  master
Handler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Rest;
4 
8 
9 abstract class Handler {
10 
15  const PARAM_SOURCE = 'rest-param-source';
16 
18  private $router;
19 
21  private $request;
22 
24  private $config;
25 
28 
31 
33  private $validatedBody;
34 
37 
46  public function init( Router $router, RequestInterface $request, array $config,
48  ) {
49  $this->router = $router;
50  $this->request = $request;
51  $this->config = $config;
52  $this->responseFactory = $responseFactory;
53  }
54 
60  protected function getRouter(): Router {
61  return $this->router;
62  }
63 
70  public function getRequest(): RequestInterface {
71  return $this->request;
72  }
73 
81  public function getConfig(): array {
82  return $this->config;
83  }
84 
92  public function getResponseFactory(): ResponseFactory {
94  }
95 
104  public function validate( Validator $restValidator ) {
105  $validatedParams = $restValidator->validateParams( $this->getParamSettings() );
106  $validatedBody = $restValidator->validateBody( $this->request, $this );
107  $this->validatedParams = $validatedParams;
108  $this->validatedBody = $validatedBody;
109  }
110 
120  protected function getConditionalHeaderUtil() {
121  if ( $this->conditionalHeaderUtil === null ) {
122  $this->conditionalHeaderUtil = new ConditionalHeaderUtil;
123  $this->conditionalHeaderUtil->setValidators(
124  $this->getETag(),
125  $this->getLastModified(),
126  $this->hasRepresentation() );
127  }
129  }
130 
137  public function checkPreconditions() {
138  $status = $this->getConditionalHeaderUtil()->checkPreconditions( $this->getRequest() );
139  if ( $status ) {
140  $response = $this->getResponseFactory()->create();
141  $response->setStatus( $status );
142  return $response;
143  } else {
144  return null;
145  }
146  }
147 
156  $this->getConditionalHeaderUtil()->applyResponseHeaders( $response );
157  }
158 
168  public function getParamSettings() {
169  return [];
170  }
171 
177  public function getBodyValidator( $contentType ) {
178  return new NullBodyValidator();
179  }
180 
188  public function getValidatedParams() {
189  if ( $this->validatedParams === null ) {
190  throw new \RuntimeException( 'getValidatedParams() called before validate()' );
191  }
192  return $this->validatedParams;
193  }
194 
200  public function getValidatedBody() {
201  return $this->validatedBody;
202  }
203 
214  protected function getLastModified() {
215  return null;
216  }
217 
229  protected function getETag() {
230  return null;
231  }
232 
240  protected function hasRepresentation() {
241  return null;
242  }
243 
253  public function needsReadAccess() {
254  return true;
255  }
256 
269  public function needsWriteAccess() {
270  return true;
271  }
272 
286  abstract public function execute();
287 }
ConditionalHeaderUtil $conditionalHeaderUtil
Definition: Handler.php:36
getBodyValidator( $contentType)
Fetch the BodyValidator.
Definition: Handler.php:177
ResponseFactory $responseFactory
Definition: Handler.php:27
$response
validateBody(RequestInterface $request, Handler $handler)
Validate the body of a request.
Definition: Validator.php:126
getConditionalHeaderUtil()
Get a ConditionalHeaderUtil object.
Definition: Handler.php:120
getLastModified()
The subclass should override this to provide the maximum last modified timestamp for the current requ...
Definition: Handler.php:214
getParamSettings()
Fetch ParamValidator settings for parameters.
Definition: Handler.php:168
RequestInterface $request
Definition: Handler.php:21
init(Router $router, RequestInterface $request, array $config, ResponseFactory $responseFactory)
Initialise with dependencies from the Router.
Definition: Handler.php:46
applyConditionalResponseHeaders(ResponseInterface $response)
Modify the response, adding Last-Modified and ETag headers as indicated the values previously returne...
Definition: Handler.php:155
getETag()
The subclass should override this to provide an ETag for the current request.
Definition: Handler.php:229
execute()
Execute the handler.
getRouter()
Get the Router.
Definition: Handler.php:60
const PARAM_SOURCE
(string) ParamValidator constant to specify the source of the parameter.
Definition: Handler.php:15
Generates standardized response objects.
getValidatedBody()
Fetch the validated body.
Definition: Handler.php:200
getConfig()
Get the configuration array for the current route.
Definition: Handler.php:81
checkPreconditions()
Check the conditional request headers and generate a response if appropriate.
Definition: Handler.php:137
The REST router is responsible for gathering handler configuration, matching an input path and HTTP m...
Definition: Router.php:18
array null $validatedParams
Definition: Handler.php:30
validateParams(array $paramSettings)
Validate parameters.
Definition: Validator.php:94
getResponseFactory()
Get the ResponseFactory which can be used to generate Response objects.
Definition: Handler.php:92
validate(Validator $restValidator)
Validate the request parameters/attributes and body.
Definition: Handler.php:104
A request interface similar to PSR-7&#39;s ServerRequestInterface.
needsWriteAccess()
Indicates whether this route requires write access.
Definition: Handler.php:269
getRequest()
Get the current request.
Definition: Handler.php:70
needsReadAccess()
Indicates whether this route requires read rights.
Definition: Handler.php:253
setValidators( $eTag, $lastModified, $hasRepresentation)
Initialize the object with information about the requested resource.
hasRepresentation()
The subclass should override this to indicate whether the resource exists.
Definition: Handler.php:240
Wrapper for ParamValidator.
Definition: Validator.php:30
An interface similar to PSR-7&#39;s ResponseInterface, the primary difference being that it is mutable...
getValidatedParams()
Fetch the validated parameters.
Definition: Handler.php:188