Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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;
13
 
14
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
15
use Symfony\Component\HttpFoundation\Session\SessionInterface;
16
 
17
/**
18
 * Request stack that controls the lifecycle of requests.
19
 *
20
 * @author Benjamin Eberlei <kontakt@beberlei.de>
21
 */
22
class RequestStack
23
{
24
    /**
25
     * @var Request[]
26
     */
27
    private array $requests = [];
28
 
29
    /**
30
     * Pushes a Request on the stack.
31
     *
32
     * This method should generally not be called directly as the stack
33
     * management should be taken care of by the application itself.
34
     */
35
    public function push(Request $request)
36
    {
37
        $this->requests[] = $request;
38
    }
39
 
40
    /**
41
     * Pops the current request from the stack.
42
     *
43
     * This operation lets the current request go out of scope.
44
     *
45
     * This method should generally not be called directly as the stack
46
     * management should be taken care of by the application itself.
47
     */
48
    public function pop(): ?Request
49
    {
50
        if (!$this->requests) {
51
            return null;
52
        }
53
 
54
        return array_pop($this->requests);
55
    }
56
 
57
    public function getCurrentRequest(): ?Request
58
    {
59
        return end($this->requests) ?: null;
60
    }
61
 
62
    /**
63
     * Gets the main request.
64
     *
65
     * Be warned that making your code aware of the main request
66
     * might make it un-compatible with other features of your framework
67
     * like ESI support.
68
     */
69
    public function getMainRequest(): ?Request
70
    {
71
        if (!$this->requests) {
72
            return null;
73
        }
74
 
75
        return $this->requests[0];
76
    }
77
 
78
    /**
79
     * Returns the parent request of the current.
80
     *
81
     * Be warned that making your code aware of the parent request
82
     * might make it un-compatible with other features of your framework
83
     * like ESI support.
84
     *
85
     * If current Request is the main request, it returns null.
86
     */
87
    public function getParentRequest(): ?Request
88
    {
89
        $pos = \count($this->requests) - 2;
90
 
91
        return $this->requests[$pos] ?? null;
92
    }
93
 
94
    /**
95
     * Gets the current session.
96
     *
97
     * @throws SessionNotFoundException
98
     */
99
    public function getSession(): SessionInterface
100
    {
101
        if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) {
102
            return $request->getSession();
103
        }
104
 
105
        throw new SessionNotFoundException();
106
    }
107
}