Skip to content

Commit 84a56f8

Browse files
authored
Merge pull request #2014 from codeigniter4/sessionarrayhandler
Added a new Session/ArrayHandler that can be used during testing.
2 parents 439bcdf + 7f0eae4 commit 84a56f8

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
/**
4+
* CodeIgniter
5+
*
6+
* An open source application development framework for PHP
7+
*
8+
* This content is released under the MIT License (MIT)
9+
*
10+
* Copyright (c) 2014-2019 British Columbia Institute of Technology
11+
*
12+
* Permission is hereby granted, free of charge, to any person obtaining a copy
13+
* of this software and associated documentation files (the "Software"), to deal
14+
* in the Software without restriction, including without limitation the rights
15+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
* copies of the Software, and to permit persons to whom the Software is
17+
* furnished to do so, subject to the following conditions:
18+
*
19+
* The above copyright notice and this permission notice shall be included in
20+
* all copies or substantial portions of the Software.
21+
*
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+
* THE SOFTWARE.
29+
*
30+
* @package CodeIgniter
31+
* @author CodeIgniter Dev Team
32+
* @copyright 2014-2019 British Columbia Institute of Technology (https://bcit.ca/)
33+
* @license https://opensource.org/licenses/MIT MIT License
34+
* @link https://codeigniter.com
35+
* @since Version 4.0.0
36+
* @filesource
37+
*/
38+
39+
namespace CodeIgniter\Session\Handlers;
40+
41+
use CodeIgniter\Session\Exceptions\SessionException;
42+
use CodeIgniter\Config\BaseConfig;
43+
use CodeIgniter\Database\BaseConnection;
44+
use Config\Database;
45+
46+
/**
47+
* Session handler using static array for storage.
48+
* Intended only for use during testing.
49+
*/
50+
class ArrayHandler extends BaseHandler implements \SessionHandlerInterface
51+
{
52+
protected static $cache = [];
53+
54+
//--------------------------------------------------------------------
55+
56+
/**
57+
* Open
58+
*
59+
* Ensures we have an initialized database connection.
60+
*
61+
* @param string $savePath Path to session files' directory
62+
* @param string $name Session cookie name
63+
*
64+
* @return boolean
65+
* @throws \Exception
66+
*/
67+
public function open($savePath, $name): bool
68+
{
69+
return true;
70+
}
71+
72+
//--------------------------------------------------------------------
73+
74+
/**
75+
* Read
76+
*
77+
* Reads session data and acquires a lock
78+
*
79+
* @param string $sessionID Session ID
80+
*
81+
* @return string Serialized session data
82+
*/
83+
public function read($sessionID): string
84+
{
85+
return '';
86+
}
87+
88+
//--------------------------------------------------------------------
89+
90+
/**
91+
* Write
92+
*
93+
* Writes (create / update) session data
94+
*
95+
* @param string $sessionID Session ID
96+
* @param string $sessionData Serialized session data
97+
*
98+
* @return boolean
99+
*/
100+
public function write($sessionID, $sessionData): bool
101+
{
102+
return true;
103+
}
104+
105+
//--------------------------------------------------------------------
106+
107+
/**
108+
* Close
109+
*
110+
* Releases locks and closes file descriptor.
111+
*
112+
* @return boolean
113+
*/
114+
public function close(): bool
115+
{
116+
return true;
117+
}
118+
119+
//--------------------------------------------------------------------
120+
121+
/**
122+
* Destroy
123+
*
124+
* Destroys the current session.
125+
*
126+
* @param string $sessionID
127+
*
128+
* @return boolean
129+
*/
130+
public function destroy($sessionID): bool
131+
{
132+
return true;
133+
}
134+
135+
//--------------------------------------------------------------------
136+
137+
/**
138+
* Garbage Collector
139+
*
140+
* Deletes expired sessions
141+
*
142+
* @param integer $maxlifetime Maximum lifetime of sessions
143+
*
144+
* @return boolean
145+
*/
146+
public function gc($maxlifetime): bool
147+
{
148+
return true;
149+
}
150+
151+
//--------------------------------------------------------------------
152+
}

user_guide_src/source/libraries/sessions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ Preference Default Options
441441
CodeIgniter\Session\Handlers\DatabaseHandler
442442
CodeIgniter\Session\Handlers\MemcachedHandler
443443
CodeIgniter\Session\Handlers\RedisHandler
444+
CodeIgniter\Session\Handlers\ArrayHandler
444445
**sessionCookieName** ci_session [A-Za-z\_-] characters only The name used for the session cookie.
445446
**sessionExpiration** 7200 (2 hours) Time in seconds (integer) The number of seconds you would like the session to last.
446447
If you would like a non-expiring session (until browser is closed) set the value to zero: 0
@@ -488,6 +489,7 @@ engines, that you can use:
488489
- CodeIgniter\Session\Handlers\DatabaseHandler
489490
- CodeIgniter\Session\Handlers\MemcachedHandler
490491
- CodeIgniter\Session\Handlers\RedisHandler
492+
- CodeIgniter\Session\Handlers\ArrayHandler
491493

492494
By default, the ``FileHandler`` Driver will be used when a session is initialized,
493495
because it is the safest choice and is expected to work everywhere
@@ -498,6 +500,9 @@ line in your **app/Config/App.php** file, if you chose to do so.
498500
Have it in mind though, every driver has different caveats, so be sure to
499501
get yourself familiar with them (below) before you make that choice.
500502

503+
.. note:: The ArrayHandler is used during testing and stores all data within
504+
a PHP array, while preventing the data from being persisted.
505+
501506
FileHandler Driver (the default)
502507
==================================================================
503508

0 commit comments

Comments
 (0)