00001 <?php
00002 require_once dirname(__FILE__) . '/lib/helpers/array.cls.php';
00003
00004
00005
00006
00007
00008
00009
00010 class Load {
00011 const ORDER_OVERLOAD = 'overload';
00012 const ORDER_DECORATORS = 'decorators';
00013
00014
00015
00016
00017
00018
00019 private static $loaded = array();
00020 private static $module_dirs = array();
00021 private static $base_dir_cache = array();
00022 private static $base_dir_subdir_cache = array();
00023
00024
00025
00026
00027 private static $module_base_dirs = array();
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 public static function filename_to_classname($filename, $appendix = array(), $extension_to_strip = '') {
00053 $filename = basename($filename, '.php');
00054 $filename = basename($filename, '.' . $extension_to_strip);
00055
00056 $appendix = Arr::force($appendix, false);
00057 foreach($appendix as $t) {
00058 $filename .= '.' . $t;
00059 }
00060
00061 $filename = str_replace('_', '.', $filename);
00062 $filename = String::plain_ascii($filename, '.', true);
00063 $cls = '';
00064 $fragments = explode('.', $filename);
00065 foreach($fragments as $f) {
00066 $cls .= ucfirst($f);
00067 }
00068
00069 return $cls;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079 public static function helpers($classes) {
00080 $args = func_get_args();
00081 self::do_load('lib/helpers', $args);
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091 public static function components($classes) {
00092 $args = func_get_args();
00093 self::do_load('lib/components', $args);
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103 public static function interfaces($classes) {
00104 $args = func_get_args();
00105 self::do_load('lib/interfaces', $args);
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115 public static function models($models) {
00116 $args = func_get_args();
00117 self::do_load('model/classes', $args, 'model', false);
00118 self::do_load('model/classes', $args, 'facade', false);
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128 public static function tools($tools) {
00129 $args = func_get_args();
00130 self::do_load('controller/tools', $args);
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140 public static function directories($directories) {
00141 $basedirs = self::get_base_directories(self::ORDER_DECORATORS);
00142 $arr_directories = self::to_array(func_get_args());
00143 foreach($arr_directories as $directory) {
00144 $found = false;
00145 foreach($basedirs as $basedir) {
00146 $path = $basedir . $directory;
00147 if (is_dir($path)) {
00148 $found = true;
00149 foreach (gyro_glob($path . '/*.php') as $inc) {
00150 include_once($inc);
00151 }
00152 }
00153 }
00154 if (!$found) {
00155 throw new Exception("Directory $directory not found");
00156 }
00157 }
00158 }
00159
00160
00161
00162
00163
00164
00165
00166 public static function get_files_from_directory($directory, $pattern = '*.php') {
00167 $basedirs = self::get_base_directories_subdirs($directory, self::ORDER_DECORATORS);
00168 $found = false;
00169 $ret = array();
00170 foreach($basedirs as $path) {
00171 $found = true;
00172 foreach (gyro_glob($path . $pattern) as $inc) {
00173 $ret[basename($inc)] = $inc;
00174 }
00175 }
00176
00177 if (!$found) {
00178 throw new Exception("Directory $directory not found");
00179 }
00180
00181 return $ret;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191 public static function commands($commands) {
00192 $args = func_get_args();
00193 self::do_load('behaviour/commands', $args, 'cmd');
00194 }
00195
00196
00197
00198
00199
00200
00201 public static function add_module_base_dir($dir) {
00202
00203
00204
00205 self::$module_base_dirs[] = rtrim(realpath($dir), '/') . '/';
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 public static function enable_module($modules) {
00222 $modules = self::to_array($modules);
00223 foreach($modules as $module) {
00224 $mod_name = $module;
00225 if (substr($module, 0, 1) != '/') {
00226 foreach(self::$module_base_dirs as $module_base_dir) {
00227 $module = $module_base_dir . $mod_name;
00228 if (file_exists($module)) {
00229 break;
00230 }
00231 }
00232 }
00233 if (substr($module, -1, 1) != '/') {
00234 $module .= '/';
00235 }
00236
00237 if (!in_array($module, self::$module_dirs)) {
00238
00239 if (!is_dir($module)) {
00240 throw new Exception("Module $module not found");
00241 }
00242 if (file_exists($module . 'enabled.inc.php')) {
00243 include_once($module . 'enabled.inc.php');
00244 }
00245 self::$module_dirs[$mod_name] = $module;
00246 }
00247 }
00248 self::$base_dir_cache = array();
00249 }
00250
00251
00252
00253
00254
00255
00256
00257 public static function get_module_dir($module) {
00258 return Arr::get_item(self::$module_dirs, $module, false);
00259 }
00260
00261
00262
00263
00264
00265
00266 public static function get_loaded_modules() {
00267 return array_keys(self::$module_dirs);
00268 }
00269
00270
00271
00272
00273
00274
00275 public static function is_module_loaded($modname) {
00276 return in_array($modname, self::get_loaded_modules());
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286 public static function files($files, $order = self::ORDER_OVERLOAD) {
00287 $files = self::to_array($files);
00288 $ret = true;
00289 foreach($files as $file) {
00290 $ret = $ret && self::do_include_file($file, false, $order);
00291 }
00292 return $ret;
00293 }
00294
00295
00296
00297
00298
00299
00300
00301 public static function first_file($files) {
00302 $files = self::to_array(func_get_args());
00303 $ret = true;
00304 foreach($files as $file) {
00305 $ret = $ret && self::do_include_file($file, true);
00306 }
00307 return $ret;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317 private static function do_include_file($file, $include_first_only = false, $order = self::ORDER_OVERLOAD) {
00318 $found = false;
00319 $basedirs = self::get_base_directories($order);
00320 foreach($basedirs as $basedir) {
00321 $path = $basedir . $file;
00322 if (file_exists($path)) {
00323 include_once $path;
00324 $found = true;
00325 if ($include_first_only) {
00326 break;
00327 }
00328 }
00329 }
00330 return $found;
00331 }
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 public static function classes_in_directory($directory, $classes, $extension = 'cls', $required = true) {
00345 return self::do_load($directory, $classes, $extension, $required);
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 private static function do_load($directory, $classes, $extension = 'cls', $required = true) {
00358 $extension = '.' . $extension . '.php';
00359 $basedirs = self::get_base_directories_subdirs($directory);
00360 $classes = self::to_array($classes);
00361 $ret = false;
00362 foreach($classes as $class) {
00363
00364 $class_path = strtolower($class) . $extension;
00365
00366 if (self::is_loaded($directory, $class_path)) {
00367 continue;
00368 }
00369
00370 $found = false;
00371
00372 foreach($basedirs as $basedir) {
00373 $path = $basedir . $class_path;
00374 if (file_exists($path)) {
00375 self::mark_loaded($directory, $class_path, $path);
00376 include_once $path;
00377 $found = true;
00378 $ret = true;
00379 break;
00380 }
00381 }
00382 if (!$found && $required) {
00383
00384 $msg = "Classfile $class on path $directory not found";
00385 throw new Exception($msg);
00386 }
00387 }
00388 return $ret;
00389 }
00390
00391
00392
00393
00394 private static function get_base_directories_subdirs($subdirectory, $order = self::ORDER_OVERLOAD) {
00395 $key = $subdirectory . '#' . $order;
00396 $ret = Arr::get_item(self::$base_dir_subdir_cache, $key, false);
00397 if ($ret === false) {
00398 $ret = array();
00399 $basedirs = self::get_base_directories($order);
00400 foreach($basedirs as $basedir) {
00401 $path = $basedir . $subdirectory . '/';
00402 if (file_exists($path)) {
00403 $ret[] = $path;
00404 }
00405 }
00406 self::$base_dir_subdir_cache[$key] = $ret;
00407 }
00408 return $ret;
00409 }
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425 public static function get_base_directories($order = self::ORDER_OVERLOAD) {
00426 $ret = Arr::get_item(self::$base_dir_cache, $order, false);
00427 if ($ret !== false) {
00428 return $ret;
00429 }
00430
00431 $ret = self::$module_dirs;
00432 switch($order) {
00433 case self::ORDER_OVERLOAD:
00434 $ret = array_reverse($ret);
00435 array_unshift($ret, APP_INCLUDE_ABSPATH);
00436 $ret[] = GYRO_CORE_DIR;
00437 break;
00438 case self::ORDER_DECORATORS:
00439 array_unshift($ret, GYRO_CORE_DIR);
00440 $ret[] = APP_INCLUDE_ABSPATH;
00441 break;
00442 default:
00443 throw new Exception('Invalid sort order in Load::get_base_directories');
00444 break;
00445 }
00446
00447 self::$base_dir_cache[$order] = $ret;
00448 return $ret;
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459 private static function mark_loaded($directory, $class, $path) {
00460 self::$loaded[$directory][$class] = $path;
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470 private static function is_loaded($directory, $class) {
00471 return isset(self::$loaded[$directory][$class]);
00472 }
00473
00474 public static function autoload($class_name) {
00475 $class_name = strtolower($class_name);
00476 if (substr($class_name, 0, 3) == 'dao') {
00477 self::models(substr($class_name, 3));
00478 }
00479 }
00480
00481
00482
00483
00484
00485
00486
00487
00488 private static function to_array($params) {
00489 $params = Arr::force($params);
00490 $ret = array();
00491 foreach($params as $p) {
00492 $ret = array_merge($ret, Arr::force($p));
00493 }
00494 return $ret;
00495 }
00496 }
00497
00498 if (!function_exists('__autoload')) {
00499 function __autoload($class_name) {
00500 Load::autoload($class_name);
00501 }
00502 }