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
<?php
namespace oroboros\core\traits\patterns\behavioral;
trait StaticRegistry {
private static $_registry_values = [];
protected static function _get($key, $subkey = null) {
if (!is_string($key) || (isset($subkey) && !is_string($subkey) ) ) {
throw new \oroboros\core\utilities\exception\Exception('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
if (!array_key_exists($key, self::$_registry_values)) {
throw new \oroboros\core\utilities\exception\Exception('Registry value: ' . $key . ' is not defined at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
if (array_key_exists($key, self::$_registry_values) && isset($subkey) && ((is_array(self::$_registry_values[$key]) && !isset(self::$_registry_values[$key][$subkey])) || (is_object(self::$_registry_values[$key]) && !isset(self::$_registry_values[$key]->$subkey)))) {
throw new \oroboros\core\utilities\exception\Exception('Registry subvalue: ' . $subkey . ' is not defined in registry key: ' . $key . ' at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
return (!isset($subkey) ? self::$_registry_values[$key] : (is_object(self::$_registry_values[$key]) ? self::$_registry_values[$key]->$subkey : self::$_registry_values[$key][$subkey]));
}
protected static function _register($key, $value, $subkey = null) {
if (!is_string($key) || ((isset($subkey) && !is_string($subkey) ) ) ) {
throw new \oroboros\core\utilities\exception\Exception('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
if (!isset($subkey)) {
self::$_registry_values[$key] = $value;
} else {
if (is_array(self::$_registry_values[$key])) {
self::$_registry_values[$key][$subkey] = $value;
} elseif (is_object(self::$_registry_values[$key])) {
self::$_registry_values[$key]->$subkey = $value;
} else {
throw new \oroboros\core\utilities\exception\Exception('Specified registry key: ' . $key . ' cannot accept subvalues because it is not an array or object at ' . __LINE__ . ' of ' . __METHOD__, self::_EXCEPTION_CODE_GENERAL_BAD_PARAMETERs);
}
}
}
protected static function _unregister($key, $subkey = null) {
if (!is_string($key) || (isset($subkey) && !is_string($subkey) ) ) {
throw new \oroboros\core\utilities\exception\Exception('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
if (isset(self::$_registry_values[$key]) && !isset($subkey)) {
unset(self::$_registry_values[$key]);
return true;
} elseif (isset($subkey) && is_array(self::$_registry_values[$key]) && isset(self::$_registry_values[$key][$subkey])) {
unset(self::$_registry_values[$key][$subkey]);
return true;
} elseif (isset($subkey) && is_object(self::$_registry_values[$key]) && isset(self::$_registry_values[$key]->$subkey)) {
unset(self::$_registry_values[$key]->$subkey);
return true;
}
return false;
}
protected static function _registryKeys($key = null) {
if (isset($key) && !is_string($key)) {
throw new \oroboros\core\utilities\exception\Exception('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
if (isset($key) && !isset(self::$_registry_values[$key])) {
throw new \oroboros\core\utilities\exception\Exception('Specified key: ' . $key . ' does not exist in registry at ' . __LINE__ . ' of ' . __METHOD__);
} elseif (isset($key) && isset(self::$_registry_values[$key]) && (!(is_array(self::$_registry_values[$key]) || is_object(self::$_registry_values[$key])))) {
throw new \oroboros\core\utilities\exception\Exception('Specified key: ' . $key . ' is not an array or object, and keys cannot be returned at ' . __LINE__ . ' of ' . __METHOD__);
} else {
return array_keys(((isset($key)) ? ((is_object(self::$_registry_values[$key])) ? get_object_vars(self::$_registry_values[$key]) : self::$_registry_values[$key]) : self::$_registry_values));
}
}
protected static function _check($key, $subkey = null) {
if (!is_string($key) || (isset($subkey) && !is_string($subkey))) {
throw new \oroboros\core\utilities\exception\Exception('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
return ((isset($subkey)) ? (array_key_exists($key, self::$_registry_values) && ((is_array(self::$_registry_values[$key])) ? array_key_exists($subkey, self::$_registry_values[$key]): ((is_object(self::$_registry_values[$key])) ? isset(self::$_registry_values[$key]->$subkey) : false ) )) : (array_key_exists($key, self::$_registry_values)));
}
}