1: <?php
2:
3: namespace Alpha\Util\Http\Session;
4:
5: use Alpha\Util\Logging\Logger;
6: use Alpha\Util\Config\ConfigProvider;
7:
8: /**
9: * Provides a session handle that stores session data in $_SESSION, the default PHP implementation.
10: *
11: * @since 2.0
12: *
13: * @author John Collins <dev@alphaframework.org>
14: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
15: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
16: * All rights reserved.
17: *
18: * <pre>
19: * Redistribution and use in source and binary forms, with or
20: * without modification, are permitted provided that the
21: * following conditions are met:
22: *
23: * * Redistributions of source code must retain the above
24: * copyright notice, this list of conditions and the
25: * following disclaimer.
26: * * Redistributions in binary form must reproduce the above
27: * copyright notice, this list of conditions and the
28: * following disclaimer in the documentation and/or other
29: * materials provided with the distribution.
30: * * Neither the name of the Alpha Framework nor the names
31: * of its contributors may be used to endorse or promote
32: * products derived from this software without specific
33: * prior written permission.
34: *
35: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
36: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
37: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
40: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48: * </pre>
49: */
50: class SessionProviderPHP implements SessionProviderInterface
51: {
52: /**
53: * Trace logger.
54: *
55: * @var Alpha\Util\Logging\Logger
56: *
57: * @since 2.0
58: */
59: private static $logger = null;
60:
61: /**
62: * Constructor.
63: *
64: * @since 2.0
65: */
66: public function __construct()
67: {
68: self::$logger = new Logger('SessionProviderPHP');
69: }
70:
71: /**
72: * {@inheritdoc}
73: */
74: public function init()
75: {
76: if (session_id() == '' && !headers_sent()) {
77: $config = ConfigProvider::getInstance();
78: $url = parse_url($config->get('app.url'));
79: $hostname = $url['host'];
80: session_set_cookie_params(0, '/', $hostname, false, true);
81: session_start();
82: }
83: }
84:
85: /**
86: * {@inheritdoc}
87: */
88: public function destroy()
89: {
90: $_SESSION = array();
91: session_destroy();
92: }
93:
94: /**
95: * {@inheritdoc}
96: */
97: public function get($key)
98: {
99: self::$logger->debug('>>get(key=['.$key.'])');
100:
101: self::$logger->debug('Getting value for key ['.$key.']');
102:
103: if (array_key_exists($key, $_SESSION)) {
104: return $_SESSION[$key];
105: } else {
106: return false;
107: }
108: }
109:
110: /**
111: * {@inheritdoc}
112: */
113: public function set($key, $value)
114: {
115: self::$logger->debug('Setting value for key ['.$key.']');
116:
117: $_SESSION[$key] = $value;
118: }
119:
120: /**
121: * {@inheritdoc}
122: */
123: public function delete($key)
124: {
125: self::$logger->debug('Removing value for key ['.$key.']');
126:
127: unset($_SESSION[$key]);
128: }
129:
130: /**
131: * {@inheritdoc}
132: */
133: public function getID()
134: {
135: return session_id();
136: }
137: }
138: