00001 <?php
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 require_once dirname(__FILE__) . '/Locale.php';
00028
00029
00030
00031
00032
00033 define('FASTFRAME_WEBPATH', 1);
00034 define('FASTFRAME_FILEPATH', 2);
00035 define('FASTFRAME_DATAPATH', 3);
00036
00037
00038 define('FASTFRAME_DEFAULT_APP', 'fastframe');
00039
00040
00041
00042
00056
00057 class FF_Registry {
00058
00059
00064 var $apps = array();
00065
00070 var $config = array();
00071
00076 var $appStack = array();
00077
00082 var $servicePaths = array(
00083
00084 'root_apps' => 'apps',
00085 'root_config' => 'config',
00086 'root_language' => 'locale',
00087 'root_libs' => 'lib',
00088 'root_javascript' => 'js',
00089 'root_graphics' => 'graphics',
00090 'root_cache' => 'cache',
00091 'root_data' => 'data',
00092 'root_themes' => 'themes',
00093
00094 'app_libs' => '%app%/lib',
00095 'app_graphics' => '%app%/graphics',
00096 'app_javascript' => '%app%/js',
00097 'app_language' => '%app%/locale',
00098 'app_config' => '%app%/config');
00099
00100
00101
00102
00103 function FF_Registry()
00104 {
00105 include_once FASTFRAME_ROOT . 'config/apps.php';
00106 $this->apps =& $apps;
00107
00108
00109 if (!isset($this->apps[FASTFRAME_DEFAULT_APP])) {
00110 $this->apps[FASTFRAME_DEFAULT_APP] = array();
00111 }
00112
00113
00114
00115 $this->pushApp(FASTFRAME_DEFAULT_APP, false);
00116
00117
00118
00119 error_reporting($this->getConfigParam('error/debug_level'));
00120
00121
00122 @set_time_limit($this->getConfigParam('general/max_exec_time'));
00123
00124
00125 if (!is_null($umask = $this->getConfigParam('general/umask'))) {
00126 umask($umask);
00127 }
00128
00129
00130 foreach (array_keys($this->apps) as $s_name) {
00131
00132 if (isset($this->apps[$s_name]['status']) && $this->apps[$s_name]['status'] == 'disabled') {
00133 unset($this->apps[$s_name]);
00134 }
00135 }
00136 }
00137
00138
00139
00140
00153 function &singleton()
00154 {
00155 static $instance;
00156 if (!isset($instance)) {
00157 $instance = new FF_Registry();
00158 }
00159
00160 return $instance;
00161 }
00162
00163
00164
00165
00177 function importConfig($in_app = FASTFRAME_DEFAULT_APP)
00178 {
00179
00180
00181 if (!isset($this->apps[$in_app])) {
00182 trigger_error("The application $in_app is not a defined application. Check your apps.php file.", E_USER_ERROR);
00183 }
00184
00185
00186 if (!isset($this->config[$in_app])) {
00187 if ($in_app == FASTFRAME_DEFAULT_APP) {
00188 include_once FASTFRAME_ROOT . 'config/conf.php';
00189 }
00190 else {
00191
00192 $s_configFile = isset($this->apps[$in_app]['profile']) ?
00193 $this->apps[$in_app]['profile'] . '/conf.php' : 'conf.php';
00194 $s_appDir = isset($this->apps[$in_app]['app_dir']) ? $this->apps[$in_app]['app_dir'] : $in_app;
00195 $s_file = $this->getAppFile($s_configFile, $s_appDir, 'config');
00196 if (!is_file($s_file)) {
00197 trigger_error('Could not import the config file ' . basename($s_file) . ' for the ' . $in_app . ' application', E_USER_ERROR);
00198 }
00199 else {
00200 include_once $s_file;
00201 }
00202 }
00203
00204 $this->config[$in_app] =& $conf;
00205 }
00206 }
00207
00208
00209
00210
00219 function setLocale($in_app = null)
00220 {
00221 if (is_null($in_app)) {
00222 $in_app = $this->getCurrentApp();
00223 }
00224
00225 FF_Locale::setLang();
00226 if ($in_app == FASTFRAME_DEFAULT_APP) {
00227 FF_Locale::setTextdomain($in_app, $this->getRootFile('', 'language'), FF_Locale::getCharset());
00228 }
00229 else {
00230 FF_Locale::setTextdomain($in_app, $this->getAppFile('', $in_app, 'language'), FF_Locale::getCharset());
00231 }
00232 }
00233
00234
00235
00236
00252 function pushApp($in_app, $in_locale = true)
00253 {
00254
00255 if ($in_app != $this->getCurrentApp()) {
00256
00257 $this->importConfig($in_app);
00258 if ($in_locale) {
00259 $this->setLocale($in_app);
00260 }
00261 }
00262
00263
00264 $this->appStack[] = $in_app;
00265 }
00266
00267
00268
00269
00280 function popCurrentApp()
00281 {
00282 $s_currentApp = array_pop($this->appStack);
00283 $s_app = $this->getCurrentApp();
00284
00285
00286 if (!is_null($s_app) && $s_app != $s_currentApp) {
00287 $this->importConfig($s_app);
00288 $this->setLocale($s_app);
00289 }
00290
00291 return $s_currentApp;
00292 }
00293
00294
00295
00296
00304 function getCurrentApp()
00305 {
00306 return @end($this->appStack);
00307 }
00308
00309
00310
00311
00323 function getFile($in_file, $in_type = FASTFRAME_FILEPATH)
00324 {
00325 $a_pathParts = array();
00326
00327
00328 switch ($in_type) {
00329 case FASTFRAME_WEBPATH:
00330 $a_pathParts = array($this->config[FASTFRAME_DEFAULT_APP]['webserver']['web_root']);
00331 break;
00332 case FASTFRAME_FILEPATH:
00333 $a_pathParts = array($this->config[FASTFRAME_DEFAULT_APP]['webserver']['file_root']);
00334 break;
00335 case FASTFRAME_DATAPATH:
00336 $a_pathParts = array($this->config[FASTFRAME_DEFAULT_APP]['webserver']['data_root']);
00337 break;
00338 default:
00339 break;
00340 }
00341
00342 if (is_array($in_file)) {
00343 $a_pathParts = array_merge($a_pathParts, $in_file);
00344 }
00345 else {
00346 $a_pathParts[] = $in_file;
00347 }
00348
00349 $s_path = implode('/', $a_pathParts);
00350
00351 if ($in_type == FASTFRAME_WEBPATH) {
00352 $s_path = str_replace('
00353 }
00354
00355 return $s_path;
00356 }
00357
00358
00359
00360
00377 function getAppFile($in_filename, $in_app = null, $in_service = '', $in_type = FASTFRAME_FILEPATH)
00378 {
00379
00380 if (FastFrame::isEmpty($in_service)) {
00381 $s_service = '%app%';
00382 }
00383 elseif (isset($this->servicePaths['app_' . $in_service])) {
00384 $s_service = $this->servicePaths['app_' . $in_service];
00385 }
00386 else {
00387 trigger_error("The app service 'app_$in_service' could not be found.", E_USER_ERROR);
00388 }
00389
00390 $s_app = !is_null($in_app) ? $in_app : $this->getCurrentApp();
00391
00392
00393
00394 $s_appDir = str_replace('%app%', $this->getAppParam('app_dir', $s_app, $s_app), $s_service);
00395 return $this->getFile(array($this->servicePaths['root_apps'], $s_appDir, $in_filename), $in_type);
00396 }
00397
00398
00399
00400
00417 function getRootFile($in_filename, $in_service = '', $in_type = FASTFRAME_FILEPATH)
00418 {
00419 if (!empty($in_service) && !isset($this->servicePaths['root_' . $in_service])) {
00420 trigger_error("The service root_$in_service could not be found", E_USER_ERROR);
00421 }
00422
00423 return $this->getFile(array($this->servicePaths['root_' . $in_service], $in_filename), $in_type);
00424 }
00425
00426
00427
00428
00440 function getAppParam($in_param, $in_default = null, $in_app = null)
00441 {
00442 $in_app = is_null($in_app) ? $this->getCurrentApp() : $in_app;
00443 return isset($this->apps[$in_app][$in_param]) ? $this->apps[$in_app][$in_param] : $in_default;
00444 }
00445
00446
00447
00448
00463 function getConfigParam($in_paramPath, $in_default = null, $in_app = null)
00464 {
00465 $in_app = is_null($in_app) ? $this->getCurrentApp() : $in_app;
00466
00467 if (!isset($this->config[$in_app])) {
00468 $this->importConfig($in_app);
00469 }
00470
00471 list($tmp1, $tmp2) = explode('/', $in_paramPath);
00472 if ($tmp2 == '*') {
00473 return isset($this->config[$in_app][$tmp1]) ?
00474 $this->config[$in_app][$tmp1] :
00475 (isset($this->config[FASTFRAME_DEFAULT_APP][$tmp1]) ?
00476 $this->config[FASTFRAME_DEFAULT_APP][$tmp1] : $in_default);
00477 }
00478 else {
00479 return isset($this->config[$in_app][$tmp1][$tmp2]) ?
00480 $this->config[$in_app][$tmp1][$tmp2] :
00481 (isset($this->config[FASTFRAME_DEFAULT_APP][$tmp1][$tmp2]) ?
00482 $this->config[FASTFRAME_DEFAULT_APP][$tmp1][$tmp2] : $in_default);
00483 }
00484 }
00485
00486
00487
00488
00496 function getApps()
00497 {
00498 $a_apps = array();
00499 foreach ($this->apps as $s_app => $a_vals) {
00500 $a_apps[] = $s_app;
00501 }
00502
00503 return $a_apps;
00504 }
00505
00506
00507
00508
00517 function hasApp($in_app)
00518 {
00519 if (isset($this->apps[$in_app]) &&
00520 isset($this->apps[$in_app]['status']) &&
00521 $this->apps[$in_app]['status'] != 'disabled') {
00522 return true;
00523 }
00524 else {
00525 return false;
00526 }
00527 }
00528
00529
00530
00531
00540 function rootPathToWebPath($in_path)
00541 {
00542 $s_fileRoot = $this->getConfigParam('webserver/file_root');
00543 if (strpos($in_path, $s_fileRoot) === 0) {
00544 return substr_replace($in_path, $this->getConfigParam('webserver/web_root'), 0, strlen($s_fileRoot));
00545 }
00546 else {
00547 return $in_path;
00548 }
00549 }
00550
00551
00552 }
00553 ?>