1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
<?php
/**
* Implementation of the Registry pattern for global access to commonly used objects throughout the code.
*
* @package Silla.IO
* @subpackage Core
* @author Plamen Nikolov <plamen@athlonsofia.com>
* @copyright Copyright (c) 2015, Silla.io
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3.0 (GPLv3)
*/
namespace Core\Modules\Registry;
/**
* Router Class definition.
*/
final class Registry
{
/**
* Reference to the current instance of the Registry object.
*
* @var object
* @access private
* @static
*/
private static $instance = null;
/**
* Keeps all object references.
*
* @var array
*
* @access private
*/
private $store = array();
/**
* Constructor, does nothing.
*
* @access private
*/
private function __construct()
{
}
/**
* Cloning of Registry is disallowed.
*
* @access public
*
* @return void
*/
public function __clone()
{
trigger_error(__CLASS__ . ' cannot be cloned! It is a singleton.', E_USER_ERROR);
}
/**
* Returns an instance of the registry object.
*
* @access public
* @static
* @final
*
* @return Registry
*/
final public static function getInstance()
{
if (null === self::$instance) {
self::$instance = new Registry();
}
return self::$instance;
}
/**
* Magic method. Alias of set().
*
* @param string $label Variable name.
* @param mixed $object Variable value.
*
* @access public
*
* @return void
*/
public function __set($label, $object)
{
$this->set($label, $object);
}
/**
* Registers an object with the Registry.
*
* @param string $label Variable name.
* @param mixed $object Variable value.
*
* @throws \InvalidArgumentException Registry varialbe name should be string.
* @access public
*
* @return void
*/
public function set($label, &$object)
{
if (!is_string($label)) {
throw new \InvalidArgumentException('Registry variable name should be string.');
}
$this->store[$label] = &$object;
}
/**
* Magic method. Returns a reference to an object in the Registry.
*
* @param string $label Variable name.
*
* @access public
*
* @return mixed
*/
public function __get($label)
{
return $this->get($label);
}
/**
* Returns a reference to an object in the Registry.
*
* @param string $label Variable name.
*
* @throws \InvalidArgumentException Registry variable name should be string.
* @access public
*
* @return mixed
*/
public function get($label)
{
if (!is_string($label)) {
throw new \InvalidArgumentException('Registry variable name should be string.');
}
return isset($this->store[$label]) ? $this->store[$label] : false;
}
/**
* Un-registers an object from the registry.
*
* @param string $label Variable name.
*
* @throws \InvalidArgumentException Registry variable name should be string.
* @access public
*
* @return void
*/
public function remove($label)
{
if (!is_string($label)) {
throw new \InvalidArgumentException('Registry variable name should be string.');
}
if (isset($this->store[$label])) {
unset($this->store[$label]);
}
}
/**
* Checks if there's an object registered under a specific label.
*
* @param string $label Variable name.
*
* @access public
*
* @return boolean
*/
public function __isset($label)
{
return isset($this->store[$label]);
}
/**
* Magic method. Un-registers an object from the registry.
*
* @param string $label Variable name.
*
* @access public
*
* @return void
*/
public function __unset($label)
{
$this->remove($label);
}
}