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\Session\Storage\Proxy;
13:
14: /**
15: * AbstractProxy.
16: *
17: * @author Drak <drak@zikula.org>
18: */
19: abstract class AbstractProxy
20: {
21: /**
22: * Flag if handler wraps an internal PHP session handler (using \SessionHandler).
23: *
24: * @var bool
25: */
26: protected $wrapper = false;
27:
28: /**
29: * @var bool
30: */
31: protected $active = false;
32:
33: /**
34: * @var string
35: */
36: protected $saveHandlerName;
37:
38: /**
39: * Gets the session.save_handler name.
40: *
41: * @return string
42: */
43: public function getSaveHandlerName()
44: {
45: return $this->saveHandlerName;
46: }
47:
48: /**
49: * Is this proxy handler and instance of \SessionHandlerInterface.
50: *
51: * @return bool
52: */
53: public function isSessionHandlerInterface()
54: {
55: return ($this instanceof \SessionHandlerInterface);
56: }
57:
58: /**
59: * Returns true if this handler wraps an internal PHP session save handler using \SessionHandler.
60: *
61: * @return bool
62: */
63: public function isWrapper()
64: {
65: return $this->wrapper;
66: }
67:
68: /**
69: * Has a session started?
70: *
71: * @return bool
72: */
73: public function isActive()
74: {
75: if (PHP_VERSION_ID >= 50400) {
76: return $this->active = \PHP_SESSION_ACTIVE === session_status();
77: }
78:
79: return $this->active;
80: }
81:
82: /**
83: * Sets the active flag.
84: *
85: * Has no effect under PHP 5.4+ as status is detected
86: * automatically in isActive()
87: *
88: * @internal
89: *
90: * @param bool $flag
91: *
92: * @throws \LogicException
93: */
94: public function setActive($flag)
95: {
96: if (PHP_VERSION_ID >= 50400) {
97: throw new \LogicException('This method is disabled in PHP 5.4.0+');
98: }
99:
100: $this->active = (bool) $flag;
101: }
102:
103: /**
104: * Gets the session ID.
105: *
106: * @return string
107: */
108: public function getId()
109: {
110: return session_id();
111: }
112:
113: /**
114: * Sets the session ID.
115: *
116: * @param string $id
117: *
118: * @throws \LogicException
119: */
120: public function setId($id)
121: {
122: if ($this->isActive()) {
123: throw new \LogicException('Cannot change the ID of an active session');
124: }
125:
126: session_id($id);
127: }
128:
129: /**
130: * Gets the session name.
131: *
132: * @return string
133: */
134: public function getName()
135: {
136: return session_name();
137: }
138:
139: /**
140: * Sets the session name.
141: *
142: * @param string $name
143: *
144: * @throws \LogicException
145: */
146: public function setName($name)
147: {
148: if ($this->isActive()) {
149: throw new \LogicException('Cannot change the name of an active session');
150: }
151:
152: session_name($name);
153: }
154: }
155: