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
|
<?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; }
}
}
}
|