3 namespace MediaWiki\Rest
;
6 * Shared code between RequestData and RequestFromGlobals
8 abstract class RequestBase
implements RequestInterface
{
10 * @var HeaderContainer|null
12 private $headerCollection;
15 private $pathParams = [];
18 private $cookiePrefix;
22 * @param string $cookiePrefix
24 protected function __construct( $cookiePrefix ) {
25 $this->cookiePrefix
= $cookiePrefix;
29 * Override this in the implementation class if lazy initialisation of
30 * header values is desired. It should call setHeaders().
34 protected function initHeaders() {
37 public function __clone() {
38 if ( $this->headerCollection
!== null ) {
39 $this->headerCollection
= clone $this->headerCollection
;
44 * Erase any existing headers and replace them with the specified header
47 * Call this either from the constructor or from initHeaders() of the
51 * @param string[] $headers The header lines
53 protected function setHeaders( $headers ) {
54 $this->headerCollection
= new HeaderContainer
;
55 $this->headerCollection
->resetHeaders( $headers );
58 public function getHeaders() {
59 if ( $this->headerCollection
=== null ) {
62 return $this->headerCollection
->getHeaders();
65 public function getHeader( $name ) {
66 if ( $this->headerCollection
=== null ) {
69 return $this->headerCollection
->getHeader( $name );
72 public function hasHeader( $name ) {
73 if ( $this->headerCollection
=== null ) {
76 return $this->headerCollection
->hasHeader( $name );
79 public function getHeaderLine( $name ) {
80 if ( $this->headerCollection
=== null ) {
83 return $this->headerCollection
->getHeaderLine( $name );
86 public function setPathParams( $params ) {
87 $this->pathParams
= $params;
90 public function getPathParams() {
91 return $this->pathParams
;
94 public function getPathParam( $name ) {
95 return $this->pathParams
[$name] ??
null;
98 public function getCookiePrefix() {
99 return $this->cookiePrefix
;
102 public function getCookie( $name, $default = null ) {
103 $cookies = $this->getCookieParams();
104 $prefixedName = $this->getCookiePrefix() . $name;
105 if ( array_key_exists( $prefixedName, $cookies ) ) {
106 return $cookies[$prefixedName];