|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Equip\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use Auryn\Injector; |
|
6
|
|
|
use Equip\Configuration\ConfigurationInterface; |
|
7
|
|
|
use Equip\NativeSession; |
|
8
|
|
|
use Equip\SessionInterface; |
|
9
|
|
|
|
|
10
|
|
|
class SessionConfiguration implements ConfigurationInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @inheritDoc |
|
14
|
|
|
*/ |
|
15
|
1 |
|
public function apply(Injector $injector) |
|
16
|
|
|
{ |
|
17
|
1 |
|
$injector->share(SessionInterface::class); |
|
18
|
|
|
|
|
19
|
1 |
|
$injector->alias( |
|
20
|
1 |
|
SessionInterface::class, |
|
21
|
1 |
|
NativeSession::class |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
1 |
|
$injector->prepare(NativeSession::class, [$this, 'prepareSession']); |
|
25
|
1 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Initialize the session |
|
29
|
|
|
* |
|
30
|
|
|
* @param NativeSession $session |
|
31
|
|
|
* @param Injector $injector |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function prepareSession(NativeSession $session, Injector $injector) |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
// It seems impossible to test this, since it has to be run in a web |
|
38
|
|
|
// server to really be useful and PHPUnit will prevent the session |
|
39
|
|
|
// headers working properly. One possible way to test it is by using |
|
40
|
|
|
// runkit to mock the [native function][1], but that seems extreme and |
|
41
|
|
|
// requires installation of a PECL extension. |
|
42
|
|
|
// |
|
43
|
|
|
// [1]: https://github.com/tcz/phpunit-mockfunction |
|
|
|
|
|
|
44
|
|
|
// |
|
45
|
|
|
// @codeCoverageIgnoreStart |
|
46
|
|
|
if ($this->canSessionStart() && $this->isCli() === false) { |
|
47
|
|
|
$this->startSession(); |
|
48
|
|
|
} |
|
49
|
|
|
// @codeCoverageIgnoreEnd |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Start a new session |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
* |
|
57
|
|
|
* @codeCoverageIgnore |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function startSession() |
|
60
|
|
|
{ |
|
61
|
|
|
session_start(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Check if a session can be started |
|
66
|
|
|
* |
|
67
|
|
|
* @return boolean |
|
68
|
|
|
*/ |
|
69
|
1 |
|
private function canSessionStart() |
|
70
|
|
|
{ |
|
71
|
1 |
|
return session_status() === PHP_SESSION_NONE; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Check if we are running in a command line context |
|
76
|
|
|
* |
|
77
|
|
|
* @return boolean |
|
78
|
|
|
*/ |
|
79
|
1 |
|
private function isCli() |
|
80
|
|
|
{ |
|
81
|
1 |
|
return php_sapi_name() === 'cli'; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.