Skip to content

Commit 6fa2a60

Browse files
authored
chore: Merge pull request #80 from WebFiori/dev
Make class request concrete.
2 parents b9eb529 + 5cabecc commit 6fa2a60

30 files changed

Lines changed: 1740 additions & 915 deletions

.github/workflows/php83.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ jobs:
3232
name: Code Quality
3333
needs: test
3434
uses: WebFiori/workflows/.github/workflows/quality-sonarcloud.yaml@main
35-
with:
36-
coverage-file: 'php-8.3-coverage.xml'
3735
secrets:
3836
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
3937

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ php-cs-fixer-v2.phar
1313
php-cs-fixer.phar
1414
.php-cs-fixer.cache
1515
*.Identifier
16+
/tests/.phpunit.cache
17+
/.vscode

WebFiori/Http/APIFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is licensed under MIT License.
44
*
5-
* Copyright (c) 2019 Ibrahim BinAlshikh
5+
* Copyright (c) 2019 WebFiori Framework
66
*
77
* For more information on the license, please visit:
88
* https://github.com/WebFiori/http/blob/master/LICENSE

WebFiori/Http/APITestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is licensed under MIT License.
44
*
5-
* Copyright (c) 2024 Ibrahim BinAlshikh
5+
* Copyright (c) 2019 WebFiori Framework
66
*
77
* For more information on the license, please visit:
88
* https://github.com/WebFiori/http/blob/master/LICENSE
@@ -294,6 +294,7 @@ private function unset(array &$arr, array $params, WebServicesManager $m, array
294294
$_SERVER['HTTP_'.strtoupper($trHeader)] = $trVal;
295295
}
296296
}
297+
$m->setRequest(Request::createFromGlobals());
297298
$m->process();
298299

299300
foreach ($params as $key => $val) {

WebFiori/Http/AbstractWebService.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is licensed under MIT License.
44
*
5-
* Copyright (c) 2019 Ibrahim BinAlshikh
5+
* Copyright (c) 2019 WebFiori Framework
66
*
77
* For more information on the license, please visit:
88
* https://github.com/WebFiori/http/blob/master/LICENSE
@@ -36,7 +36,12 @@ abstract class AbstractWebService implements JsonI {
3636
*
3737
*/
3838
const I = 'info';
39-
39+
/**
40+
* A constant which is used to indicate that the message that will be
41+
* sent is of type success.
42+
*
43+
*/
44+
const S = 'success';
4045
/**
4146
* The name of the service.
4247
*
@@ -65,6 +70,13 @@ abstract class AbstractWebService implements JsonI {
6570
*
6671
*/
6772
private $reqMethods;
73+
/**
74+
* The request instance used by the service.
75+
*
76+
* @var Request
77+
*
78+
*/
79+
private $request;
6880
/**
6981
* This is used to indicate if authentication is required when the service
7082
* is called.
@@ -123,6 +135,7 @@ public function __construct(string $name) {
123135
$this->requireAuth = true;
124136
$this->sinceVersion = '1.0.0';
125137
$this->serviceDesc = '';
138+
$this->request = Request::createFromGlobals();
126139
}
127140
/**
128141
* Returns an array that contains all possible requests methods at which the
@@ -326,10 +339,21 @@ public final function addResponseDescription(string $description) {
326339
* ('basic', 'bearer', 'digest', etc...). The 'credentials' will contain
327340
* the credentials which can be used to authenticate the client.
328341
*
329-
* @throws InvalidArgumentException
330342
*/
331343
public function getAuthHeader() {
332-
return Request::getAuthHeader();
344+
if ($this->request !== null) {
345+
return $this->request->getAuthHeader();
346+
}
347+
return null;
348+
}
349+
350+
/**
351+
* Sets the request instance for the service.
352+
*
353+
* @param mixed $request The request instance (Request, etc.)
354+
*/
355+
public function setRequest($request) {
356+
$this->request = $request;
333357
}
334358
/**
335359
* Returns the description of the service.
@@ -690,11 +714,11 @@ public function send(string $contentType, $data, int $code = 200) {
690714
* will be not included in response. Default is empty string. Default is null.
691715
*
692716
*/
693-
public function sendResponse(string $message, string $type = '', int $code = 200, mixed $otherInfo = '') {
717+
public function sendResponse(string $message, int $code = 200, string $type = '', mixed $otherInfo = '') {
694718
$manager = $this->getManager();
695719

696720
if ($manager !== null) {
697-
$manager->sendResponse($message, $type, $code, $otherInfo);
721+
$manager->sendResponse($message, $code, $type, $otherInfo);
698722
}
699723
}
700724
/**

WebFiori/Http/HeadersPool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is licensed under MIT License.
44
*
5-
* Copyright (c) 2022 Ibrahim BinAlshikh
5+
* Copyright (c) 2019 WebFiori Framework
66
*
77
* For more information on the license, please visit:
88
* https://github.com/WebFiori/http/blob/master/LICENSE

WebFiori/Http/HttpCookie.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is licensed under MIT License.
44
*
5-
* Copyright (c) 2022 Ibrahim BinAlshikh
5+
* Copyright (c) 2019 WebFiori Framework
66
*
77
* For more information on the license, please visit:
88
* https://github.com/WebFiori/http/blob/master/LICENSE
@@ -57,7 +57,7 @@ class HttpCookie {
5757
* <li>path: /</li>
5858
* <li>secure: true</li>
5959
* <li>http only: true</li>
60-
* <li>domain: The domain at which the library is operating from.</li>
60+
* <li>domain: The domain at which the cookie is operating from.</li>
6161
* <li>same site: Lax</li>
6262
* <li>expires: 0</li>
6363
* <li>value: sha256 hash</li>
@@ -68,7 +68,7 @@ public function __construct() {
6868
$this->cookieName = 'new-cookie';
6969
$this->path = '/';
7070
$this->secure = true;
71-
$this->domain = Request::getUri()->getHost();
71+
$this->domain = null;
7272
$this->sameSite = 'Lax';
7373
$this->val = hash('sha256', date('Y-m-d H:i:s'));
7474
$this->expires = 0;

WebFiori/Http/HttpHeader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is licensed under MIT License.
44
*
5-
* Copyright (c) 2022 Ibrahim BinAlshikh
5+
* Copyright (c) 2019 WebFiori Framework
66
*
77
* For more information on the license, please visit:
88
* https://github.com/WebFiori/http/blob/master/LICENSE

WebFiori/Http/HttpMessage.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2024 WebFiori Framework
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/http/blob/master/LICENSE
9+
*/
10+
namespace WebFiori\Http;
11+
12+
/**
13+
* Base class for HTTP messages (Request and Response).
14+
*
15+
* @author Ibrahim
16+
*/
17+
class HttpMessage {
18+
/**
19+
* @var HeadersPool
20+
*/
21+
private $headersPool;
22+
23+
/**
24+
* @var string
25+
*/
26+
private $body;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $protocolVersion;
32+
33+
/**
34+
* @var string
35+
*/
36+
private $requestMethod;
37+
38+
/**
39+
* Creates new instance of the class.
40+
*/
41+
public function __construct() {
42+
$this->headersPool = new HeadersPool();
43+
$this->body = '';
44+
$this->protocolVersion = '1.1';
45+
$this->requestMethod = 'GET';
46+
}
47+
48+
/**
49+
* Returns the headers pool.
50+
*
51+
* @return HeadersPool
52+
*/
53+
public function getHeadersPool() : HeadersPool {
54+
return $this->headersPool;
55+
}
56+
57+
/**
58+
* Returns the value(s) of specific HTTP header.
59+
*
60+
* @param string $name The name of the header.
61+
*
62+
* @return array
63+
*/
64+
public function getHeader(string $name) : array {
65+
return $this->headersPool->getHeader($name);
66+
}
67+
68+
/**
69+
* Returns an array that contains all headers.
70+
*
71+
* @return array
72+
*/
73+
public function getHeaders() : array {
74+
return $this->headersPool->getHeaders();
75+
}
76+
77+
/**
78+
* Checks if specific header exists.
79+
*
80+
* @param string $name The name of the header.
81+
* @param string|null $val Optional header value to check.
82+
*
83+
* @return bool
84+
*/
85+
public function hasHeader(string $name, ?string $val = '') : bool {
86+
return $this->headersPool->hasHeader($name, $val);
87+
}
88+
89+
/**
90+
* Removes specific header.
91+
*
92+
* @param string $name The name of the header.
93+
* @param string|null $val Optional header value to remove.
94+
*
95+
* @return bool
96+
*/
97+
public function removeHeader(string $name, ?string $val = '') : bool {
98+
return $this->headersPool->removeHeader($name, $val);
99+
}
100+
101+
/**
102+
* Adds a header to the message.
103+
*
104+
* @param string $name The name of the header.
105+
* @param string $value The value of the header.
106+
* @param string|null $replaceValue Optional value to replace.
107+
*
108+
* @return bool
109+
*/
110+
public function addHeader(string $name, string $value, ?string $replaceValue = '') : bool {
111+
return $this->headersPool->addHeader($name, $value, $replaceValue);
112+
}
113+
114+
/**
115+
* Gets the body of the message.
116+
*
117+
* @return string
118+
*/
119+
public function getBody() : string {
120+
return $this->body;
121+
}
122+
123+
/**
124+
* Sets the body of the message.
125+
*
126+
* @param string $body
127+
*/
128+
public function setBody(string $body) {
129+
$this->body = $body;
130+
}
131+
132+
/**
133+
* Gets the protocol version.
134+
*
135+
* @return string
136+
*/
137+
public function getProtocolVersion() : string {
138+
return $this->protocolVersion;
139+
}
140+
141+
/**
142+
* Sets the protocol version.
143+
*
144+
* @param string $version
145+
*/
146+
public function setProtocolVersion(string $version) {
147+
$this->protocolVersion = $version;
148+
}
149+
150+
/**
151+
* Gets the request method.
152+
*
153+
* @return string
154+
*/
155+
public function getRequestMethod() : string {
156+
return $this->requestMethod;
157+
}
158+
159+
/**
160+
* Sets the request method.
161+
*
162+
* @param string $method
163+
*/
164+
public function setRequestMethod(string $method) {
165+
$this->requestMethod = $method;
166+
}
167+
}

WebFiori/Http/ManagerInfoService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* This file is licensed under MIT License.
44
*
5-
* Copyright (c) 2020 Ibrahim BinAlshikh
5+
* Copyright (c) 2019 WebFiori Framework
66
*
77
* For more information on the license, please visit:
88
* https://github.com/WebFiori/http/blob/master/LICENSE

0 commit comments

Comments
 (0)