Skip to content

Commit be01648

Browse files
committed
Deprecat service "session"
1 parent 3601ef5 commit be01648

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.3
55
---
66

7+
* Add the `RequestStack::getSession` method
78
* Deprecate the `NamespacedAttributeBag` class
89
* added `ResponseFormatSame` PHPUnit constraint
910

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Exception;
13+
14+
/**
15+
* Raised when a session does not exists. This happens in the following cases:
16+
* - the session is not enabled
17+
* - attempt to read a session outside a request context (ie. cli script).
18+
*
19+
* @author Jérémy Derussé <jeremy@derusse.com>
20+
*/
21+
class SessionNotFoundException extends \LogicException implements RequestExceptionInterface
22+
{
23+
public function __construct($message = 'There is currently no session available.', $code = 0, Throwable $previous = null)
24+
{
25+
parent::__construct($message, $code, $previous);
26+
}
27+
}

RequestStack.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\HttpFoundation;
1313

14+
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
15+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
16+
1417
/**
1518
* Request stack that controls the lifecycle of requests.
1619
*
@@ -100,4 +103,18 @@ public function getParentRequest()
100103

101104
return $this->requests[$pos];
102105
}
106+
107+
/**
108+
* Gets the current session.
109+
*
110+
* @throws SessionNotFoundException
111+
*/
112+
public function getSession(): SessionInterface
113+
{
114+
if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) {
115+
return $request->getSession();
116+
}
117+
118+
throw new SessionNotFoundException();
119+
}
103120
}

0 commit comments

Comments
 (0)