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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* @see https://github.com/laminas/laminas-session for the canonical source repository
* @copyright https://github.com/laminas/laminas-session/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-session/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\Session\Service;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\Session\Config\ConfigInterface;
use Laminas\Session\Container;
use Laminas\Session\ManagerInterface;
use Laminas\Session\SaveHandler\SaveHandlerInterface;
use Laminas\Session\SessionManager;
use Laminas\Session\Storage\StorageInterface;
class SessionManagerFactory implements FactoryInterface
{
/**
* Default configuration for manager behavior
*
* @var array
*/
protected $defaultManagerConfig = [
'enable_default_container_manager' => true,
];
/**
* Create session manager object (v3 usage).
*
* Will consume any combination (or zero) of the following services, when
* present, to construct the SessionManager instance:
*
* - Laminas\Session\Config\ConfigInterface
* - Laminas\Session\Storage\StorageInterface
* - Laminas\Session\SaveHandler\SaveHandlerInterface
*
* The first two have corresponding factories inside this namespace. The
* last, however, does not, due to the differences in implementations, and
* the fact that save handlers will often be written in userland. As such
* if you wish to attach a save handler to the manager, you will need to
* write your own factory, and assign it to the service name
* "Laminas\Session\SaveHandler\SaveHandlerInterface", (or alias that name
* to your own service).
*
* You can configure limited behaviors via the "session_manager" key of the
* Config service. Currently, these include:
*
* - enable_default_container_manager: whether to inject the created instance
* as the default manager for Container instances. The default value for
* this is true; set it to false to disable.
* - validators: ...
*
* @param ContainerInterface $container
* @param string $requestedName
* @param array $options
* @return SessionManager
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = null;
$storage = null;
$saveHandler = null;
$validators = [];
$managerConfig = $this->defaultManagerConfig;
$options = [];
if ($container->has(ConfigInterface::class)) {
$config = $container->get(ConfigInterface::class);
if (! $config instanceof ConfigInterface) {
throw new ServiceNotCreatedException(sprintf(
'SessionManager requires that the %s service implement %s; received "%s"',
ConfigInterface::class,
ConfigInterface::class,
(is_object($config) ? get_class($config) : gettype($config))
));
}
}
if ($container->has(StorageInterface::class)) {
$storage = $container->get(StorageInterface::class);
if (! $storage instanceof StorageInterface) {
throw new ServiceNotCreatedException(sprintf(
'SessionManager requires that the %s service implement %s; received "%s"',
StorageInterface::class,
StorageInterface::class,
(is_object($storage) ? get_class($storage) : gettype($storage))
));
}
}
if ($container->has(SaveHandlerInterface::class)) {
$saveHandler = $container->get(SaveHandlerInterface::class);
if (! $saveHandler instanceof SaveHandlerInterface) {
throw new ServiceNotCreatedException(sprintf(
'SessionManager requires that the %s service implement %s; received "%s"',
SaveHandlerInterface::class,
SaveHandlerInterface::class,
(is_object($saveHandler) ? get_class($saveHandler) : gettype($saveHandler))
));
}
}
// Get session manager configuration, if any, and merge with default configuration
if ($container->has('config')) {
$configService = $container->get('config');
if (isset($configService['session_manager'])
&& is_array($configService['session_manager'])
) {
$managerConfig = array_merge($managerConfig, $configService['session_manager']);
}
if (isset($managerConfig['validators'])) {
$validators = $managerConfig['validators'];
}
if (isset($managerConfig['options'])) {
$options = $managerConfig['options'];
}
}
$managerClass = class_exists($requestedName) ? $requestedName : SessionManager::class;
if (! is_subclass_of($managerClass, ManagerInterface::class)) {
throw new ServiceNotCreatedException(sprintf(
'SessionManager requires that the %s service implement %s',
$managerClass,
ManagerInterface::class
));
}
$manager = new $managerClass($config, $storage, $saveHandler, $validators, $options);
// If configuration enables the session manager as the default manager for container
// instances, do so.
if (isset($managerConfig['enable_default_container_manager'])
&& $managerConfig['enable_default_container_manager']
) {
Container::setDefaultManager($manager);
}
return $manager;
}
/**
* Create a SessionManager instance (v2 usage)
*
* @param ServiceLocatorInterface $services
* @param null|string $canonicalName
* @param string $requestedName
* @return SessionManager
*/
public function createService(
ServiceLocatorInterface $services,
$canonicalName = null,
$requestedName = SessionManager::class
) {
return $this($services, $requestedName);
}
}