-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpMessaging.php
More file actions
executable file
·90 lines (78 loc) · 2.54 KB
/
Copy pathHttpMessaging.php
File metadata and controls
executable file
·90 lines (78 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Class HttpMessaging
*
* A PSR-7 messaging handler that provides access to standard HTTP
* request and response interfaces. Allows passing custom instances
* or generates default ones using MaplePHP HTTP components.
*
* Useful as a bridge between PSR-compliant libraries and error handling,
* debugging, or general application-level HTTP workflows.
*
* @package MaplePHP\Blunder
* @author Daniel Ronkainen
* @license Apache-2.0 license, Copyright © Daniel Ronkainen
* Don't delete this comment, it's part of the license.
*/
namespace MaplePHP\Blunder;
use MaplePHP\Blunder\Interfaces\HttpMessagingInterface;
use MaplePHP\Http\Environment;
use MaplePHP\Http\Response;
use MaplePHP\Http\ServerRequest;
use MaplePHP\Http\Stream;
use MaplePHP\Http\Uri;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
final class HttpMessaging implements HttpMessagingInterface
{
protected ?ResponseInterface $response = null;
protected ?ServerRequestInterface $request = null;
/**
* Pass your own PSR-7 library HTTP message library instances
* @param ResponseInterface|null $response
* @param ServerRequestInterface|null $request
*/
public function __construct(?ResponseInterface $response = null, ?ServerRequestInterface $request = null)
{
$this->response = $response;
$this->request = $request;
}
/**
* Get PSR response
* @return ResponseInterface
*/
public function response(): ResponseInterface
{
if (!($this->response instanceof ResponseInterface)) {
$stream = new Stream(Stream::TEMP);
$this->response = new Response($stream);
}
return $this->response;
}
/**
* Get PSR request
* @return ServerRequestInterface
*/
public function request(): ServerRequestInterface
{
if (!($this->request instanceof ServerRequestInterface)) {
$env = new Environment();
$this->request = new ServerRequest(new Uri($env->getUriParts()), $env);
}
return $this->request;
}
/**
* Inherit or create new stream instance from PSR-7
* @param mixed|null $stream
* @param string $permission
* @return StreamInterface
*/
public function stream(mixed $stream = null, string $permission = "r+"): StreamInterface
{
if ($stream !== null) {
return new Stream($stream, $permission);
}
return $this->response()->getBody();
}
}