|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Shieldon package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Terry L. <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* php version 7.1.0 |
|
11
|
|
|
* |
|
12
|
|
|
* @category Web-security |
|
13
|
|
|
* @package Shieldon |
|
14
|
|
|
* @author Terry Lin <[email protected]> |
|
15
|
|
|
* @copyright 2019 terrylinooo |
|
16
|
|
|
* @license https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT |
|
17
|
|
|
* @link https://github.com/terrylinooo/shieldon |
|
18
|
|
|
* @see https://shieldon.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types=1); |
|
22
|
|
|
|
|
23
|
|
|
namespace Shieldon\Firewall; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This is a very, very simple container. |
|
27
|
|
|
* For storing Shieldon releated instances and variables.. |
|
28
|
|
|
*/ |
|
29
|
|
|
class Container |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* The container. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $instances = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Find an entry of the container by its identifier and returns it. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $id Identifier of the entry to look for. |
|
42
|
|
|
* |
|
43
|
|
|
* @return mixed Entry. |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function get(string $id) |
|
46
|
|
|
{ |
|
47
|
|
|
if (self::has($id)) { |
|
48
|
|
|
return self::$instances[$id]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Return true if the container can return an entry for the given identifier. |
|
56
|
|
|
* Return false otherwise. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $id Identifier of the entry to look for. |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function has(string $id): bool |
|
63
|
|
|
{ |
|
64
|
|
|
return isset(self::$instances[$id]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set an entry into container. |
|
69
|
|
|
* |
|
70
|
|
|
* @param object $id Identifier of the entry to look for. |
|
71
|
|
|
* @param mixed $entry Entry. |
|
72
|
|
|
* @param bool $overwrite Overwrite it even exists. |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function set(string $id, $entry, bool $overwrite = true): void |
|
77
|
|
|
{ |
|
78
|
|
|
if (!self::has($id) || $overwrite) { |
|
79
|
|
|
self::$instances[$id] = $entry; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Unset an entry of the Container. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $id Identifier of the entry to look for. |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function remove(string $id): void |
|
91
|
|
|
{ |
|
92
|
|
|
if (self::has($id)) { |
|
93
|
|
|
self::$instances[$id] = null; |
|
94
|
|
|
unset(self::$instances[$id]); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|