diff options
| author | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:01:30 +0200 |
|---|---|---|
| committer | George Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2023-03-12 07:01:30 +0200 |
| commit | 47cbb529f5723b246125ae083a193e11481b89ef (patch) | |
| tree | 31ab20b2fbf12bee1cd994c3d6fb822fa52622e3 /classes/Registry.php | |
| download | classroom-47cbb529f5723b246125ae083a193e11481b89ef.tar.gz classroom-47cbb529f5723b246125ae083a193e11481b89ef.tar.bz2 classroom-47cbb529f5723b246125ae083a193e11481b89ef.zip | |
initializing classroom structure using anom framework
Diffstat (limited to 'classes/Registry.php')
| -rw-r--r-- | classes/Registry.php | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/classes/Registry.php b/classes/Registry.php new file mode 100644 index 0000000..df012e9 --- /dev/null +++ b/classes/Registry.php @@ -0,0 +1,129 @@ +<?php + +/** Registry + * --- + * Regisrty is a global repository of {key: value} pairs, + * where key is a friendly label, and... + * value can be anything (numbet|string|array|object etc) + * + * It has a central role in the framework. + * By default it holds the Request instance and several + * othe core instanses like database connections etc. + */ +class Registry +{ + /** STATIC PROPERTIES + * ------------------------------------------------------------------------- + */ + + /** keeps all ready-to-use records + * --- + * These are all records defined via Registry::set(), and + * vow-records defined (Registry::vow()) AND called (Registry::use()) + * (it does not include the vows that have not carried-out yet ) + */ + private static $records = Array(); + + /** keeps all records that will be carried-out later (if ever) + * --- + * These are registered via Registry::vow() + */ + private static $wish = Array(); + + + /** METHODS + * ------------------------------------------------------------------------- + */ + + /** set + * --- + * store $data to the registry undel the label $key + * @param $key (string) + * @param $data * + */ + static function set($key, $data) + { + // TODO: not use if needed... + // you may include an explicit method for reseting the key + // or even reVOWing a wish; + // or it could be just a 3rd paremeter like + // function set($key, $data, $reset = false) {...} + // function set($key, $function, $reset = false) {...} + + if (!isset(self::$records[$key])) { + + self::$records[$key] = $data; + return true; + + } else { return fasle; } + } + + + /** get + * --- + * get data from registry, + * stored under the label $key + * @param $key (string): the label + * @return data (mixed) + */ + static function get($key) + { + // if defined and not beeing a vow/wish, serve + if (isset(self::$records[$key]) && (!isset(self::$wish[$key]))) { + + return self::$records[$key]; + + } else { return false; } + } + + + /** vow + * --- + * implements a promise of a function operation + * when/if the registry label is called; + * it can store a function, an object handler etc. + * + * example: + * Registry::vow('key', function(){ return new Obj(); }) + * + * The main advantage of using a 'vow' vs a 'set' is + * that a 'vow' will not use system resources to prepare + * the data/object/handler/etc until/if-ever is called. + * + * @param $label (string): label / refference key + * @param function : + */ + static function vow($label, $function) + { + self::$wish[$label] = $function; + } + + + /** use (some vow) + * --- + * this is the method that carries out + * the promissed function (with arguments) + */ + static function use($label, $args=[]) + { + // if already prepered (and is a wish) serve it + if (isset(self::$records[$label]) && isset(self::$wish[$label])) { + + return self::$records[$label]; + + } else { + + // otherwise if is a wish/promise + if (array_key_exists($label, self::$wish)) { + + // prepare, store and serve it + $carryOut = call_user_func_array(self::$wish[$label], $args); + self::$records[$label] = $carryOut; + + return $carryOut; + + } else { return false; } + } + } + +} |
