|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Molniya - Flash Messenger |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify it |
|
8
|
|
|
* under the terms of the GNU Affero General Public License as published by |
|
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or (at your |
|
10
|
|
|
* option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
* @category Factory |
|
21
|
|
|
* @package Jnjxp\Molniya |
|
22
|
|
|
* @author Jake Johns <[email protected]> |
|
23
|
|
|
* @copyright 2016 Jake Johns |
|
24
|
|
|
* @license http://www.gnu.org/licenses/agpl-3.0.txt AGPL V3 |
|
25
|
|
|
* @link https://github.com/jnjxp/jnjxp.molniya |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace Jnjxp\Molniya; |
|
29
|
|
|
|
|
30
|
|
|
use Aura\Session\Session; |
|
31
|
|
|
use Aura\Session\SessionFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* FlashMessengerFactory |
|
35
|
|
|
* |
|
36
|
|
|
* @category Factory |
|
37
|
|
|
* @package Jnjxp\Molniya |
|
38
|
|
|
* @author Jake Johns <[email protected]> |
|
39
|
|
|
* @license http://www.gnu.org/licenses/agpl-3.0.txt AGPL V3 |
|
40
|
|
|
* @link https://github.com/jnjxp/jnjxp.molniya |
|
41
|
|
|
*/ |
|
42
|
|
|
class FlashMessengerFactory |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* Session |
|
46
|
|
|
* |
|
47
|
|
|
* @var Session |
|
48
|
|
|
* |
|
49
|
|
|
* @access protected |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $session; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* __construct |
|
55
|
|
|
* |
|
56
|
|
|
* @param Session $session DESCRIPTION |
|
57
|
|
|
* |
|
58
|
|
|
* @access public |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function __construct(Session $session = null) |
|
61
|
|
|
{ |
|
62
|
4 |
|
$this->session = $session; |
|
63
|
4 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* NewInstance |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $name Segment namespace |
|
69
|
|
|
* |
|
70
|
|
|
* @return FlashMessenger |
|
71
|
|
|
* |
|
72
|
|
|
* @access public |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function newInstance($name = 'Jnjxp\Molniya') |
|
75
|
|
|
{ |
|
76
|
4 |
|
$session = $this->getSession(); |
|
77
|
4 |
|
$segment = $session->getSegment($name); |
|
78
|
4 |
|
return new FlashMessenger($segment); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set Session |
|
83
|
|
|
* |
|
84
|
|
|
* @param Session $session DESCRIPTION |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
* |
|
88
|
|
|
* @access public |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function setSession(Session $session) |
|
91
|
|
|
{ |
|
92
|
2 |
|
$this->session = $session; |
|
93
|
2 |
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* GetSession |
|
98
|
|
|
* |
|
99
|
|
|
* @return mixed |
|
100
|
|
|
* |
|
101
|
|
|
* @access protected |
|
102
|
|
|
*/ |
|
103
|
4 |
|
protected function getSession() |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
4 |
|
if (null == $this->session) { |
|
106
|
1 |
|
$this->session = (new SessionFactory)->newInstance($_COOKIE); |
|
107
|
1 |
|
} |
|
108
|
4 |
|
return $this->session; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: