|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Session; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
15
|
|
|
use Spiral\Core\Container\SingletonInterface; |
|
16
|
|
|
use Spiral\Core\FactoryInterface; |
|
17
|
|
|
use Spiral\Session\Config\SessionConfig; |
|
18
|
|
|
use Spiral\Session\Exception\MultipleSessionException; |
|
19
|
|
|
use Spiral\Session\Exception\SessionException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Initiates session instance and configures session handlers. |
|
23
|
|
|
*/ |
|
24
|
|
|
final class SessionFactory implements SingletonInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var SessionConfig */ |
|
27
|
|
|
private $config; |
|
28
|
|
|
|
|
29
|
|
|
/** @var FactoryInterface */ |
|
30
|
|
|
private $factory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param SessionConfig $config |
|
34
|
|
|
* @param FactoryInterface $factory |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(SessionConfig $config, FactoryInterface $factory) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->config = $config; |
|
39
|
|
|
$this->factory = $factory; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $clientSignature User specific token, does not provide full security but |
|
44
|
|
|
* hardens session transfer. |
|
45
|
|
|
* @param string|null $id When null - expect php to create session automatically. |
|
46
|
|
|
* @return SessionInterface |
|
47
|
|
|
* |
|
48
|
|
|
*/ |
|
49
|
|
|
public function initSession(string $clientSignature, string $id = null): SessionInterface |
|
50
|
|
|
{ |
|
51
|
|
|
if ((int)session_status() === PHP_SESSION_ACTIVE) { |
|
52
|
|
|
throw new MultipleSessionException('Unable to initiate session, session already started'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
//Initiating proper session handler |
|
56
|
|
|
if ($this->config->getHandler() !== null) { |
|
57
|
|
|
try { |
|
58
|
|
|
$handler = $this->config->getHandler()->resolve($this->factory); |
|
59
|
|
|
} catch (\Throwable | ContainerExceptionInterface $e) { |
|
60
|
|
|
throw new SessionException($e->getMessage(), $e->getCode(), $e); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
session_set_save_handler($handler, true); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->factory->make(Session::class, [ |
|
67
|
|
|
'clientSignature' => $clientSignature, |
|
68
|
|
|
'lifetime' => $this->config->getLifetime(), |
|
69
|
|
|
'id' => $id |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|