-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathM.php
More file actions
212 lines (192 loc) · 6.46 KB
/
M.php
File metadata and controls
212 lines (192 loc) · 6.46 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
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
208
209
210
211
212
<?php
/**
* M PHP Framework
*
* @package M
* @subpackage M.php
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
/**
* This class is used to retreive specific environment variables
* @todo : this makes the package not portable as mysql bin path and pear path are hardcoded.
* find a way to guess these values or store them as environment specific path (i.e. the domain based config file)
*/
class UnresolvedFileException extends Exception {}
class UnresolvedClassException extends Exception {}
class M {
public static function getPearpath()
{
return '/Applications/MAMP/bin/php5/lib/php/';
}
public static function getSQLbinpath()
{
return '/usr/bin/env mysql';
}
public static function getDatabaseDSN()
{
$p = PEAR::getStaticProperty('DB_DataObject','options');
return $p['database'];
}
public static function hook($className,$methodName,$params = array())
{
$hooks = Config::get(strtolower($className).'_hooks');
if(is_array($hooks) && key_exists($methodName, $hooks)) {
foreach($hooks[$methodName] as $hookClass) {
$hookObject = new $hookClass();
$hookObject->apply($params);
}
}
}
public static function addHook($namespace, $methodToHook, $hookClass, $hookClassfile)
{
$hooks = Config::get($namespace.'_hooks');
if(!is_array($hooks[$methodToHook])) {
$hooks[$methodToHook] = array();
}
$hooks[$methodToHook][]= $hookClass;
Mreg::append('autoload', array(
strtolower($hookClass) => $hookClassfile
));
Config::set("{$namespace}_hooks", $hooks);
}
public static function tablesWithPlugin($pluginName)
{
foreach(FileUtils::getAllFiles(APP_ROOT.'app/models/','php') as $file) {
$t = DB_DataObject::factory(strtolower(basename($file,'.php')));
if(PEAR::isError($t)) continue;
$plugs = $t->_getPluginsDef();
if($plugs[$pluginName]) {
$ret[] = $t->tableName();
}
}
return $ret;
}
public static function addPaths($role, $paths)
{
switch($role) {
case 'model':
$options = & PEAR::getStaticProperty('DB_DataObject', 'options');
$options['class_location'] .= ':'.implode(',',$paths);
break;
case 'lang':
foreach($paths as $path) {
T::addPath($path);
}
break;
case 'templates':
$moduloptions = & PEAR::getStaticProperty('Module','global');
if(!is_array($moduloptions['template_dir'])) $moduloptions['template_dir'] = array();
$moduloptions['template_dir'] = array_merge($moduloptions['template_dir'], $paths);
break;
case 'modules':
$dispatchopt = & PEAR::getStaticProperty('Dispatcher', 'global');
if(!is_array($dispatchopt['all']['modulepath'])) $dispatchopt['all']['modulepath'] = array();
$dispatchopt['all']['modulepath'] = array_merge($dispatchopt['all']['modulepath'], $paths);
break;
default:
Mreg::append("{$role}_paths", $paths);
break;
}
}
public static function addPath($role, $path)
{
self::addPaths($role, array($path));
}
public static function getPaths($role)
{
try {
$paths = Mreg::get("{$role}_paths");
} catch(Exception $e) {
$paths = array();
}
return $paths;
}
public static function resolve_class($class, $role, $init_function = null)
{
if(class_exists($class)) return true;
$file = strtolower(str_replace('_','/',$class)) . '.php';
foreach(self::getPaths($role) as $path) {
$full_path = $path.'/'.$file_name;
if(FileUtils::file_exists_incpath($full_path)) {
require_once(self::resolve_file($file, $role));
if($init_function instanceOf Closure) $init_function();
return true;
}
}
throw new UnresolvedFileException("Could not load class {$class} in ".print_r(self::getPaths($role), true));
}
public static function resolve_file($file_name, $role)
{
foreach(self::getPaths($role) as $path) {
$full_path = $path.'/'.$file_name;
if(file_exists($full_path)) return realpath($full_path);
}
throw new UnresolvedFileException("Could not find {$file_name} in ".print_r(self::getPaths($role), true));
}
public static function bootstrap()
{
$paths[]=APP_ROOT.'app/_shared/';
$paths[]=APP_ROOT.'app/'.APP_NAME.'/';
$paths[]=APP_ROOT.'app/';
set_include_path(get_include_path().':'.implode(':',$paths));
if(defined('E_DEPRECATED')) {
ini_set('error_reporting',E_ALL & ~E_STRICT & ~E_NOTICE & ~E_DEPRECATED);
} else {
ini_set('error_reporting',E_ALL & ~E_STRICT & ~E_NOTICE);
}
switch(MODE) {
case 'development' :
ini_set('display_errors',1);
$caching=false;
break;
case 'test' :
ini_set('display_errors',1);
$caching=false;
break;
case 'production' :
ini_set('display_errors',0);
$caching=true;
break;
}
T::setConfig(array(
'path' => APP_ROOT.'app/'.APP_NAME.'/lang/',
'encoding' => 'utf8',
'saveresult' => false,
'driver' => 'reader',
'autoexpire' => (MODE == 'development')
)
);
if(!defined('DEFAULT_LANG')) define('DEFAULT_LANG', 'en');
M::addPath('lang', dirname(__FILE__).'/lang/');
$lang = $_REQUEST['lang'] ? $_REQUEST['lang'] : DEFAULT_LANG;
T::setLang($lang);
M::addPath('templates', APP_ROOT.'app/_shared/templates/');
M::addPath('templates', APP_ROOT.'app/'.APP_NAME.'/templates/');
M::addPath('modules', 'modules');
M::addPath('plugins', realpath(dirname(__FILE__)));
M::addPath('plugins', APP_ROOT.'app/');
$opt = & PEAR::getStaticProperty('Module', 'global');
$opt['caching'] = $caching;
$opt['cacheDir'] = APP_ROOT.'app/'.APP_NAME.'/cache/';
$opt['cacheTime'] = 7200;
$dispatchopt = &PEAR::getStaticProperty('Dispatcher', 'global');
$dispatchopt['all']['loginmodule']='user';
$dispatchopt['all']['loginaction']='login';
$dispatchopt['all']['modulepath']=array('modules');
require APP_ROOT.'app/setup.php';
$setup = new M_setup();
spl_autoload_register('M::m_autoload_db_dataobject');
Mreg::set('setup',$setup);
}
public static function m_autoload_db_dataobject($class)
{
$classname = strtolower($class);
if(strpos($classname, 'dataobjects_') === 0) {
return DB_DataObject::_autoloadClass($classname);
} else {
return false;
}
}
}