-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.php
More file actions
153 lines (135 loc) · 4.03 KB
/
Module.php
File metadata and controls
153 lines (135 loc) · 4.03 KB
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
<?php
namespace Core;
use kernel;
use ReflectionMethod;
/*! \addtogroup Core
* @{
*/
/******************************************************************************/
/**
* Controller class.
*/
abstract class Module
{
protected $kernel = null;
private $errors = array();
private $cache = null;
public function __construct()
{
$this->kernel = kernel::getInstance();
}
/**
* Add pre and post call hooks here, if defined.
*/
// public function __call($method, $args)
// {
// $this->kernel->log(LOG_DEBUG, "$method called with " . count($args));
// // $method = new ReflectionMethod($this, 'input');
// // $args = func_get_args();
// // array_shift($args);
// // $value = $method->invokeArgs($this, $args);
// }
public static function getModuleValue()
{
$argv = func_get_args();
array_unshift($argv, get_called_class());
$reflection = new ReflectionMethod('kernel', 'getModuleValue');
/* return value from current module configuration */
return $reflection->invokeArgs(kernel::getInstance(), $argv);
}
public static function getConfigValue()
{
$argv = func_get_args();
$reflection = new ReflectionMethod('kernel', 'getConfigValue');
/* return value from configuration */
return $reflection->invokeArgs(kernel::getInstance(), $argv);
}
public function setError($error)
{
$this->errors[] = $error;
}
public function getError()
{
$n = count($this->errors);
if ($n < 1) {
return false;
}
return $this->errors[$n - 1];
}
public function logError($message)
{
/* add error to current module log */
$this->setError($message);
/* log error through kernel */
$this->kernel->log(LOG_ERR, $message, get_class($this));
return $message;
}
/**
* Emit a signal.
*/
public function emit($name)
{
$args = func_get_args();
array_unshift($args, get_called_class());
$method = new ReflectionMethod($this->kernel, 'emit');
$method->invokeArgs($this->kernel, $args);
}
/**
* Wrapper method for ease of access to cache.
* Also the key will be prefixed using current class (module) name
* ("<class_name>::$key").
*/
public function cacheGet($key, $default = null)
{
$cache = \kernel::getCacheInstance();
if (!$cache) {
return null;
}
/* prefix cache key with current class(module) name */
$key = get_class($this) . '-' . $key;
$key = preg_replace('@[\{}\()/\@:\\\\]@', '-', $key);
$item = $cache->getItem($key);
if ($item->isHit()) {
return $item->get();
}
return $default;
}
/**
* Wrapper method for ease of access to cache.
* Also the key will be prefixed using current class (module) name
* ("<class_name>::$key").
*/
public function cacheSet($key, $value, $ttl = 600)
{
$cache = \kernel::getCacheInstance();
if (!$cache) {
return null;
}
/* prefix cache key with current class(module) name */
$key = get_class($this) . '-' . $key;
$key = preg_replace('@[\{}\()/\@:\\\\]@', '-', $key);
$item = $cache->getItem($key);
$item->set($value)->expiresAfter($ttl);
$cache->save($item);
return true;
}
/**
* Wrapper method for ease of access to cache.
* Also the key will be prefixed using current class (module) name
* ("<class_name>::$key").
*/
public function cacheDelete($key)
{
if (!$this->cache) {
$this->cache = $this->kernel->getCacheInstance();
if (!$this->cache) {
return null;
}
}
/* prefix cache key with current class(module) name */
$key = get_class($this) . '::' . $key;
$this->cache->delete($key);
return true;
}
}
/*! @} endgroup Core */