Registry.php 1.13 KB
Newer Older
1
<?php
Tino Goratsch's avatar
Tino Goratsch committed
2 3 4 5 6 7

/**
 * Copyright (c) by the ACP3 Developers.
 * See the LICENSE file at the top-level module directory for licensing details.
 */

8 9 10 11
namespace ACP3\Core;

class Registry
{
12
    private static $registry = [];
13 14

    /**
15
     * Inject an object to the registry.
16
     *
17
     * @param string $key
18
     * @param mixed  $value
19
     */
20
    public function set($key, $value)
21 22 23 24 25 26
    {
        if (isset(self::$registry[$key]) === false) {
            self::$registry[$key] = $value;
        }
    }

27 28
    /**
     * @param string $key
29
     *
30 31 32 33 34 35 36
     * @return bool
     */
    public function has($key)
    {
        return isset(self::$registry[$key]);
    }

37
    /**
38
     * Get an object from the registry.
39
     *
40
     * @param string $key
41 42
     * @param null   $default
     *
43 44
     * @return mixed
     */
45
    public function get($key, $default = null)
46
    {
47
        return $this->has($key) ? self::$registry[$key] : $default;
48 49 50
    }

    /**
51
     * Remove an object from the registry.
52
     *
53
     * @param string $key
54
     */
55
    public function remove($key)
56
    {
57 58
        if (isset(self::$registry[$key])) {
            unset(self::$registry[$key]);
59 60 61
        }
    }
}