1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
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 <contact@terryl.in>
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 3
    public static function get(string $id)
46
    {
47 3
        if (self::has($id)) {
48 3
            return self::$instances[$id];
49
        }
50

51 3
        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 3
    public static function has(string $id): bool
63
    {
64 3
        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 3
    public static function set(string $id, $entry, bool $overwrite = true): void
77
    {
78 3
        if (!self::has($id) || $overwrite) {
79 3
            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 3
    public static function remove(string $id): void
91
    {
92 3
        if (self::has($id)) {
93 3
            self::$instances[$id] = null;
94 3
            unset(self::$instances[$id]);
95
        }
96
    }
97
}

Read our documentation on viewing source code .

Loading