-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
executable file
·72 lines (60 loc) · 1.72 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php declare(strict_types=1);
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Session;
use Koded\Stdlib\Interfaces\ConfigurationFactory;
use SessionHandlerInterface;
/**
* Creates the Session object if not already instantiated,
* or returns an instance of Session.
*
* @param Session $instance [optional]
*
* @return Session
*/
function session(Session $instance = null): Session
{
static $session;
if ($instance) {
return $session = $instance;
}
return $session ?? $session = new PhpSession;
}
/**
* Creates and registers a new session handler.
*
* @param ConfigurationFactory $configuration
*
* @return SessionConfiguration The session configuration instance
* @throws SessionException
*/
function session_register_custom_handler(ConfigurationFactory $configuration): SessionConfiguration
{
$configuration = new SessionConfiguration($configuration);
if (PHP_SESSION_ACTIVE !== session_status()) {
session_set_save_handler(session_create_custom_handler($configuration), false);
}
return $configuration;
}
/**
* Factory for creating a session handler.
*
* @param SessionConfiguration $configuration
*
* @return SessionHandlerInterface
*/
function session_create_custom_handler(SessionConfiguration $configuration): SessionHandlerInterface
{
$handler = __NAMESPACE__ . '\\Handler\\' . ucfirst($configuration->handler()) . 'Handler';
if (false === class_exists($handler, true)) {
throw SessionException::forNotFoundHandler($handler);
}
return new $handler($configuration);
}