Skip to content

Commit c0a0086

Browse files
[9.x] Add session decorator (#40378)
* add decorator * Apply fixes from StyleCI * fix docs Co-authored-by: StyleCI Bot <[email protected]>
1 parent 82cae41 commit c0a0086

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed

src/Illuminate/Http/Request.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
use ArrayAccess;
66
use Closure;
77
use Illuminate\Contracts\Support\Arrayable;
8+
use Illuminate\Session\SymfonySessionDecorator;
89
use Illuminate\Support\Arr;
910
use Illuminate\Support\Str;
1011
use Illuminate\Support\Traits\Macroable;
1112
use RuntimeException;
13+
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
1214
use Symfony\Component\HttpFoundation\ParameterBag;
1315
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
16+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1417

1518
/**
1619
* @method array validate(array $rules, ...$params)
@@ -492,6 +495,24 @@ protected function filterFiles($files)
492495
return $files;
493496
}
494497

498+
/**
499+
* {@inheritdoc}
500+
*/
501+
public function hasSession(bool $skipIfUninitialized = false): bool
502+
{
503+
return ! is_null($this->session);
504+
}
505+
506+
/**
507+
* {@inheritdoc}
508+
*/
509+
public function getSession(): SessionInterface
510+
{
511+
return $this->hasSession()
512+
? new SymfonySessionDecorator($this->session())
513+
: throw new SessionNotFoundException;
514+
}
515+
495516
/**
496517
* Get the session associated with the request.
497518
*
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
namespace Illuminate\Session;
4+
5+
use BadMethodCallException;
6+
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
7+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
8+
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
9+
10+
class SymfonySessionDecorator implements SessionInterface
11+
{
12+
/**
13+
* The underlying Laravel session store.
14+
*
15+
* @var \Illuminate\Session\Store
16+
*/
17+
protected $store;
18+
19+
/**
20+
* Create a new session decorator.
21+
*
22+
* @param \Illuminate\Session\Store $store
23+
* @return void
24+
*/
25+
public function __construct(Store $store)
26+
{
27+
$this->store = $store;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function start(): bool
34+
{
35+
return $this->store->start();
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getId(): string
42+
{
43+
return $this->store->getId();
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function setId(string $id)
50+
{
51+
$this->store->setId($id);
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function getName(): string
58+
{
59+
return $this->store->getName();
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function setName(string $name)
66+
{
67+
$this->store->setName($name);
68+
}
69+
70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function invalidate(int $lifetime = null): bool
74+
{
75+
$this->store->invalidate();
76+
77+
return true;
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
public function migrate(bool $destroy = false, int $lifetime = null): bool
84+
{
85+
$this->store->migrate($destroy);
86+
87+
return true;
88+
}
89+
90+
/**
91+
* {@inheritdoc}
92+
*/
93+
public function save()
94+
{
95+
$this->store->save();
96+
}
97+
98+
/**
99+
* {@inheritdoc}
100+
*/
101+
public function has(string $name): bool
102+
{
103+
return $this->store->has($name);
104+
}
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
public function get(string $name, mixed $default = null): mixed
110+
{
111+
return $this->store->get($name, $default);
112+
}
113+
114+
/**
115+
* {@inheritdoc}
116+
*/
117+
public function set(string $name, mixed $value)
118+
{
119+
$this->store->put($name, $value);
120+
}
121+
122+
/**
123+
* {@inheritdoc}
124+
*/
125+
public function all(): array
126+
{
127+
return $this->store->all();
128+
}
129+
130+
/**
131+
* {@inheritdoc}
132+
*/
133+
public function replace(array $attributes)
134+
{
135+
$this->store->replace($attributes);
136+
}
137+
138+
/**
139+
* {@inheritdoc}
140+
*/
141+
public function remove(string $name): mixed
142+
{
143+
return $this->store->remove($name);
144+
}
145+
146+
/**
147+
* {@inheritdoc}
148+
*/
149+
public function clear()
150+
{
151+
$this->store->flush();
152+
}
153+
154+
/**
155+
* {@inheritdoc}
156+
*/
157+
public function isStarted(): bool
158+
{
159+
return $this->store->isStarted();
160+
}
161+
162+
/**
163+
* {@inheritdoc}
164+
*/
165+
public function registerBag(SessionBagInterface $bag)
166+
{
167+
throw new BadMethodCallException('Method not implemented by Laravel.');
168+
}
169+
170+
/**
171+
* {@inheritdoc}
172+
*/
173+
public function getBag(string $name): SessionBagInterface
174+
{
175+
throw new BadMethodCallException('Method not implemented by Laravel.');
176+
}
177+
178+
/**
179+
* {@inheritdoc}
180+
*/
181+
public function getMetadataBag(): MetadataBag
182+
{
183+
throw new BadMethodCallException('Method not implemented by Laravel.');
184+
}
185+
}

0 commit comments

Comments
 (0)