-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRequest.php
118 lines (99 loc) · 3.05 KB
/
Request.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Flasher\Symfony\Http;
use Flasher\Prime\Http\RequestInterface;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Request - Adapter for Symfony's HTTP request.
*
* This class implements PHPFlasher's RequestInterface for Symfony's HTTP request,
* providing a consistent interface for request inspection and session interaction
* regardless of the underlying framework.
*
* Design patterns:
* - Adapter Pattern: Adapts framework-specific request to PHPFlasher's interface
* - Decorator Pattern: Adds PHPFlasher-specific functionality to request objects
* - Null Object Pattern: Gracefully handles missing sessions
*/
final readonly class Request implements RequestInterface
{
/**
* Creates a new Request adapter.
*
* @param SymfonyRequest $request The underlying Symfony request object
*/
public function __construct(private SymfonyRequest $request)
{
}
public function getUri(): string
{
return $this->request->getRequestUri();
}
public function isXmlHttpRequest(): bool
{
return $this->request->isXmlHttpRequest();
}
public function isHtmlRequestFormat(): bool
{
return 'html' === $this->request->getRequestFormat();
}
public function hasSession(): bool
{
return $this->request->hasSession();
}
public function isSessionStarted(): bool
{
$session = $this->getSession();
return $session?->isStarted() ?: false;
}
public function hasType(string $type): bool
{
if (!$this->hasSession() || !$this->isSessionStarted()) {
return false;
}
$session = $this->getSession();
if (!$session instanceof FlashBagAwareSessionInterface) {
return false;
}
return $session->getFlashBag()->has($type);
}
/**
* @return string[]
*/
public function getType(string $type): array
{
$session = $this->getSession();
if (!$session instanceof FlashBagAwareSessionInterface) {
return [];
}
return $session->getFlashBag()->get($type);
}
public function forgetType(string $type): void
{
$this->getType($type);
}
/**
* Gets the session from the request, with graceful handling of missing sessions.
*
* @return SessionInterface|null The session or null if not available
*/
private function getSession(): ?SessionInterface
{
try {
return $this->request->getSession();
} catch (SessionNotFoundException) {
return null;
}
}
public function hasHeader(string $key): bool
{
return $this->request->headers->has($key);
}
public function getHeader(string $key): ?string
{
return $this->request->headers->get($key);
}
}