forked from Gneiss-Project/Gneiss-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.php
207 lines (193 loc) · 6.55 KB
/
factory.php
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace modules\core;
use modules\core\interfaces as i;
use modules\core\classes as c;
/**
* Gneiss::factory creates instances of modules and module objects.
* Copyright (C) 2015-2016 Matthew David Brown
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author lordmatt
*/
class factory extends c\module_lib {
protected $cache = array();
protected $CONF;
protected $helpers = array();
protected function __construct() {}
protected function __clone() {}
/**
*
* @staticvar null $me
* @return \modules\core\factory
*/
public static function &instence(){
static $me = null;
if($me===null || !is_object($me)){
$me = new factory();
}
return $me;
}
/**
* Legacy shortcut.
* @return \modules\core\debug
*/
public function &debug(){
return $this->get_module_lib('core', 'debug');
}
/**
* Alias of get_module_lib($name,$name)
* @param string $name
* @return \modules\{$name}\{$name}
*/
public function &get_module($name){
if(isset($this->cache[$name][$name]) && is_object($this->cache[$name][$name])){
return $this->cache[$name][$name];
}
$mynameis = "core\\factory\\get_module({$name})";
if(!ctype_alnum($name)){
// bad, bad, bad. Only give me letters and numbers
$this->debug()->log($mynameis,'notAlphaNumeric');
$error = new \modules\core\error('Module notAlphaNumeric', 500);
return $error;
}
return $this->get_module_lib($name, $name);
}
/**
* @param string $module
* @param string $lib
* @return modules\{$module}\{$lib}
*/
public function &get_module_lib($module,$lib){
$class = "modules\\{$module}\\{$lib}";
if( isset($this->cache[$module][$lib]) && is_object($this->cache[$module][$lib]) ){
return $this->cache[$module][$lib];
}
$mynameis = "core\\factory\\get_module_lib({$module},{$lib})";
try {
if(class_exists($class)){
try {
$this->cache[$module][$lib] = new $class();
if($this->cache[$module][$lib] instanceof i\module){
$this->cache[$module][$lib]->init();
}
return $this->cache[$module][$lib];
} catch (\Exception $e) {
// var_dump($e->getMessage());
// do something clever here
$this->debug()->log($mynameis,print_r($e,true),true);
}
// give up
$this->debug()->log($mynameis,'tried and failed');
$error = new \modules\core\error('tried and failed', 500);
return $error;
}else{
// not here
$this->debug()->log($mynameis,'not a class'.$module,true);
$error = new \modules\core\error($mynameis,'not a class', 500);
return $error;
}
} catch (\Exception $e) {
// var_dump($e->getMessage());
// do something clever here
$this->debug()->log($mynameis,print_r($e,true),true);
}
$error = new \modules\core\error('Exception during module creation', 500);
return $error;
}
private function load_CONF(){
if(!is_array($this->CONF)){
include $this->get_file_path() . '/config.php';
if(!is_array($CONF)){
$CONF=array();
}
$settings_path = $this->get_file_path() . '/settings/';
if(is_array($CONF['settings']) && file_exists($settings_path)){
$CONF['settings'][] = 'development';
foreach($CONF['settings'] as $setting){
$file = $settings_path.$setting.'.php';
if(file_exists($file)){
include $file;
}
}
$this->CONF = $CONF;
}
}
}
/**
* Push additional values into the Gneiss config stack
* @param string $var
* @param mixed $val
*/
public function set_config($var,$val){
$this->load_CONF();
$this->CONF[$var]=$val;
}
/**
* Get Gneiss config values.
* @param string $var
* @param mixed $default
* @return mixed
*/
public function get_config($var,$default=''){
$this->load_CONF();
if(isset($this->CONF[$var])){
return $this->CONF[$var];
}else{
$val = $this->module()->model()->get_config($var);
if($val!==FALSE){
$default = $val;
}
$this->set_config($var, $default);
return $this->CONF[$var];
}
}
/**
* Returns FALSE if config not set or table not ready.
* @param string $module
* @param string $var
* @return mixed
*/
public function get_module_config($module,$var){
return $this->module()->model()->get_module_config($module,$var);
}
/**
*
* @param string $message
* @param int $code
* @param array $data
* @return \modules\core\error
*/
public function new_error($message,$code=0,$data=array()){
$error = new error($message,$code,$data);
$this->debug()->log("ERROR:{$code}:{$message}", $data);
return $error;
}
protected function populate_helpers(){
if( count($this->helpers)<1 ){
$data = array();
core::get()->notify('helpers',$data);
#$this->debug()->log("TRACE:helpers", $data);
$this->helpers=&$data;
}
}
public function &get_helper($what){
$this->populate_helpers();
if(isset($this->helpers[$what])){
return $this->helpers[$what];
}
$what = new error("{$what} not found",404,array());
return $what;
}
}