Registry.php

Go to the documentation of this file.
00001 <?php
00003 // {{{ license
00004 
00005 // +----------------------------------------------------------------------+
00006 // | FastFrame Application Framework                                      |
00007 // +----------------------------------------------------------------------+
00008 // | Copyright (c) 2002-2006 The Codejanitor Group                        |
00009 // +----------------------------------------------------------------------+
00010 // | This source file is subject to the GNU Lesser Public License (LGPL), |
00011 // | that is bundled with this package in the file LICENSE, and is        |
00012 // | available at through the world-wide-web at                           |
00013 // | http://www.fsf.org/copyleft/lesser.html                              |
00014 // | If you did not receive a copy of the LGPL and are unable to          |
00015 // | obtain it through the world-wide-web, you can get it by writing the  |
00016 // | Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
00017 // | MA 02111-1307, USA.                                                  |
00018 // +----------------------------------------------------------------------+
00019 // | Authors: The Horde Team <http://www.horde.org>                       |
00020 // |          Jason Rust <jrust@codejanitor.com>                          |
00021 // |          Dan Allen <dan@mojavelinux.com>                             |
00022 // +----------------------------------------------------------------------+
00023 
00024 // }}}
00025 // {{{ includes
00026 
00027 require_once dirname(__FILE__) . '/Locale.php';
00028 
00029 // }}}
00030 // {{{ constants/globals
00031 
00032 // types of filepaths that can be generated
00033 define('FASTFRAME_WEBPATH',  1);
00034 define('FASTFRAME_FILEPATH', 2);
00035 define('FASTFRAME_DATAPATH', 3);
00036 
00037 // the default application
00038 define('FASTFRAME_DEFAULT_APP', 'fastframe');
00039 
00040 // }}}
00041 // {{{ class FF_Registry
00042 
00056 // }}}
00057 class FF_Registry {
00058     // {{{ properties
00059 
00064     var $apps = array();
00065 
00070     var $config = array();
00071 
00076     var $appStack = array();
00077 
00082     var $servicePaths = array(
00083         // names of the directories in the fastframe root
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         // names of the services inside an app (%app% is the basename of the app)
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     // {{{ constructor
00102 
00103     function FF_Registry()
00104     {
00105         include_once FASTFRAME_ROOT . 'config/apps.php';
00106         $this->apps =& $apps;
00107         // Add the default app which might not be in the apps file
00108         // since it is sort of a pseudo app
00109         if (!isset($this->apps[FASTFRAME_DEFAULT_APP])) {
00110             $this->apps[FASTFRAME_DEFAULT_APP] = array();
00111         }
00112 
00113         // Don't set the language because it will run into an infinite loop otherwise,
00114         // since the Locale class uses the registry.
00115         $this->pushApp(FASTFRAME_DEFAULT_APP, false);
00116 
00117         // Initial FastFrame-wide settings
00118         // Set the error reporting level in accordance with the config settings.
00119         error_reporting($this->getConfigParam('error/debug_level'));
00120 
00121         // Set the maximum execution time in accordance with the config settings.
00122         @set_time_limit($this->getConfigParam('general/max_exec_time'));
00123 
00124         // Set the umask according to config settings
00125         if (!is_null($umask = $this->getConfigParam('general/umask'))) {
00126             umask($umask);
00127         }
00128 
00129         // All apps to be used must be included in the apps.php file
00130         foreach (array_keys($this->apps) as $s_name) {
00131             // Don't carry around disabled apps
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     // {{{ singleton()
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     // {{{ importConfig()
00165 
00177     function importConfig($in_app = FASTFRAME_DEFAULT_APP)
00178     {
00179         // :NOTE: All fatal errors generated in this method should not be logged
00180         // since the log method calls getConfigParam() which could cause an infinite loop
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         // Now load the configuration for this specific app
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                 // Look up which profile to use for the config files
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     // {{{ setLocale()
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     // {{{ pushApp()
00236 
00252     function pushApp($in_app, $in_locale = true)
00253     {
00254         // If we are changing apps then we need to import config
00255         if ($in_app != $this->getCurrentApp()) {
00256             // Import the config for this application
00257             $this->importConfig($in_app);
00258             if ($in_locale) {
00259                 $this->setLocale($in_app);
00260             }
00261         }
00262 
00263         // Now that we've checked if it's changed, push it on
00264         $this->appStack[] = $in_app;
00265     }
00266 
00267     // }}}
00268     // {{{ popCurrentApp()
00269 
00280     function popCurrentApp()
00281     {
00282         $s_currentApp = array_pop($this->appStack);
00283         $s_app = $this->getCurrentApp();
00284         // Import the new active application's configuration values again
00285         // if there is still at least one application on the stack
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     // {{{ getCurrentApp()
00296 
00304     function getCurrentApp()
00305     {
00306         return @end($this->appStack);
00307     }
00308 
00309     // }}}
00310     // {{{ getFile()
00311 
00323     function getFile($in_file, $in_type = FASTFRAME_FILEPATH)
00324     {
00325         $a_pathParts = array();
00326         // Given the number of times this is called and that it can be
00327         // called from getConfigParam() we access the config array directly
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         // It's important that there be no double slashes in web paths
00351         if ($in_type == FASTFRAME_WEBPATH) {
00352             $s_path = str_replace('//', '/', $s_path);
00353         }
00354         
00355         return $s_path;
00356     }
00357 
00358     // }}}
00359     // {{{ getAppFile()
00360 
00377     function getAppFile($in_filename, $in_app = null, $in_service = '', $in_type = FASTFRAME_FILEPATH)
00378     {
00379         // this is how we handle the root path to the app 
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         // Use the app name as the apps directory unless the user has 
00393         // overriden it in apps.php
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     // {{{ getRootFile()
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     // {{{ getAppParam()
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     // {{{ getConfigParam()
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         // Import the config for this app if it has not yet beeen imported 
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     // {{{ getApps()
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     // {{{ hasApp()
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     // {{{ rootPathToWebPath()
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 ?>

Generated on Fri Jun 23 11:38:17 2006 for FastFrame by  doxygen 1.4.4