Source for file jApp.class.php

Documentation is available at jApp.class.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage core
  5. * @author     Laurent Jouanneau
  6. * @copyright  2011-2012 Laurent Jouanneau
  7. * @link       http://jelix.org
  8. * @licence    http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  9. */
  10.  
  11. /**
  12. *
  13. * @package    jelix
  14. * @subpackage core
  15. */
  16. class jApp {
  17.  
  18.     protected static $tempBasePath = '';
  19.  
  20.     protected static $appPath = '';
  21.  
  22.     protected static $varPath = '';
  23.  
  24.     protected static $logPath = '';
  25.  
  26.     protected static $configPath = '';
  27.  
  28.     protected static $wwwPath = '';
  29.  
  30.     protected static $scriptPath = '';
  31.  
  32.     protected static $_isInit = false;
  33.  
  34.     protected static $env = 'www/';
  35.  
  36.     protected static $configAutoloader = null;
  37.  
  38.     /**
  39.      * initialize the application paths
  40.      *
  41.      * Warning: given paths should be ended by a directory separator.
  42.      * @param string $appPath  application directory
  43.      * @param string $wwwPath  www directory
  44.      * @param string $varPath  var directory
  45.      * @param string $logPath log directory
  46.      * @param string $configPath config directory
  47.      * @param string $scriptPath scripts directory
  48.      */
  49.     public static function initPaths ($appPath,
  50.                                  $wwwPath = null,
  51.                                  $varPath = null,
  52.                                  $logPath = null,
  53.                                  $configPath = null,
  54.                                  $scriptPath = null
  55.                                  ) {
  56.         self::$appPath = $appPath;
  57.         self::$wwwPath = (is_null($wwwPath)?$appPath.'www/':$wwwPath);
  58.         self::$varPath = (is_null($varPath)?$appPath.'var/':$varPath);
  59.         self::$logPath = (is_null($logPath)?self::$varPath.'log/':$logPath);
  60.         self::$configPath = (is_null($configPath)?self::$varPath.'config/':$configPath);
  61.         self::$scriptPath = (is_null($scriptPath)?$appPath.'scripts/':$scriptPath);
  62.         self::$_isInit = true;
  63.     }
  64.  
  65.     /**
  66.      * indicate if path have been set
  67.      * @return boolean  true if it is ok
  68.      */
  69.     public static function isInit() { return self::$_isInit; }
  70.  
  71.     public static function appPath($file='') { return self::$appPath.$file; }
  72.     public static function varPath($file='') { return self::$varPath.$file; }
  73.     public static function logPath($file='') { return self::$logPath.$file; }
  74.     public static function configPath($file='') { return self::$configPath.$file; }
  75.     public static function wwwPath($file='') { return self::$wwwPath.$file; }
  76.     public static function scriptsPath($file='') { return self::$scriptPath.$file; }
  77.     public static function tempPath($file='') { return self::$tempBasePath.self::$env.$file; }
  78.     public static function tempBasePath() { return self::$tempBasePath; }
  79.  
  80.     public static function setTempBasePath($path) {
  81.         self::$tempBasePath = $path;
  82.     }
  83.  
  84.     public static function setEnv($env) {
  85.         if (substr($env,-1) != '/')
  86.             $env.='/';
  87.         self::$env = $env;
  88.     }
  89.  
  90.     /**
  91.      * @var object  object containing all configuration options of the application
  92.      */
  93.     protected static $_config = null;
  94.  
  95.     /**
  96.      * @return object object containing all configuration options of the application
  97.      */
  98.     public static function config() {
  99.         return self::$_config;
  100.     }
  101.  
  102.     public static function setConfig($config) {
  103.         if (self::$configAutoloader) {
  104.             spl_autoload_unregister(array(self::$configAutoloader, 'loadClass'));
  105.             self::$configAutoloader = null;
  106.         }
  107.  
  108.         self::$_config = $config;
  109.         if ($config) {
  110.             date_default_timezone_set(self::$_config->timeZone);
  111.             self::$configAutoloader = new jConfigAutoloader($config);
  112.             spl_autoload_register(array(self::$configAutoloader, 'loadClass'));
  113.             foreach(self::$_config->_autoload_autoloader as $autoloader)
  114.                 require_once($autoloader);
  115.         }
  116.     }
  117.  
  118.     /**
  119.      * Load the configuration from the given file.
  120.      *
  121.      * Call it after initPaths
  122.      * @param  string|object $configFile name of the ini file to configure the framework or a configuration object
  123.      * @param  boolean $enableErrorHandler enable the error handler of jelix.
  124.      *                  keep it to true, unless you have something to debug
  125.      *                  and really have to use the default handler or an other handler
  126.      */
  127.     public static function loadConfig ($configFile, $enableErrorHandler=true) {
  128.         if ($enableErrorHandler) {
  129.             jBasicErrorHandler::register();
  130.         }
  131.         if (is_object($configFile))
  132.             self::setConfig($configFile);
  133.         else
  134.             self::setConfig(jConfig::load($configFile));
  135.         self::$_config->enableErrorHandler = $enableErrorHandler;
  136.     }
  137.  
  138.     protected static $_coord = null;
  139.     
  140.     public static function coord() {
  141.         return self::$_coord;
  142.     }
  143.  
  144.     public static function setCoord($coord) {
  145.         self::$_coord = $coord;
  146.     }
  147.  
  148.     protected static $contextBackup = array();
  149.  
  150.     /**
  151.      * save all path and others variables relatives to the application, so you can
  152.      * temporary change the context to an other application
  153.      */
  154.     public static function saveContext() {
  155.         if (self::$_config)
  156.             $conf = clone self::$_config;
  157.         else
  158.             $conf = null;
  159.         if (self::$_coord)
  160.             $coord = clone self::$_coord;
  161.         else
  162.             $coord = null;
  163.         self::$contextBackup[] = array(self::$appPath, self::$varPath, self::$logPath,
  164.                                        self::$configPath, self::$wwwPath, self::$scriptPath,
  165.                                        self::$tempBasePath, self::$env, $conf, $coord,
  166.                                        self::$modulesContext);
  167.     }
  168.  
  169.     /**
  170.      * restore the previous context of the application
  171.      */
  172.     public static function restoreContext() {
  173.         if (!count(self::$contextBackup))
  174.             return;
  175.         list(self::$appPath, self::$varPath, self::$logPath, self::$configPath,
  176.              self::$wwwPath, self::$scriptPath, self::$tempBasePath, self::$env,
  177.              $conf, self::$_coord, self::$modulesContext) = array_pop(self::$contextBackup);
  178.         self::setConfig($conf);
  179.     }
  180.  
  181.     /**
  182.      * load a plugin from a plugin directory (any type of plugins)
  183.      * @param string $name the name of the plugin
  184.      * @param string $type the type of the plugin
  185.      * @param string $suffix the suffix of the filename
  186.      * @param string $classname the name of the class to instancy
  187.      * @param mixed $args  the argument for the constructor of the class. null = no argument.
  188.      * @return null|object   null if the plugin doesn't exists
  189.      */
  190.     public static function loadPlugin($name, $type, $suffix, $classname, $args = null) {
  191.  
  192.         if (!class_exists($classname,false)) {
  193.             $optname = '_pluginsPathList_'.$type;
  194.             if (!isset(jApp::config()->$optname))
  195.                 return null;
  196.             $opt = & jApp::config()->$optname;
  197.             if (!isset($opt[$name])
  198.                 || !file_exists($opt[$name].$name.$suffix) ){
  199.                 return null;
  200.             }
  201.             require_once($opt[$name].$name.$suffix);
  202.         }
  203.         if (!is_null($args))
  204.             return new $classname($args);
  205.         else
  206.             return new $classname();
  207.     }
  208.  
  209.     /**
  210.     * Says if the given module $name is enabled
  211.     * @param string $moduleName 
  212.     * @param boolean $includingExternal  true if we want to know if the module
  213.     *                is also an external module, e.g. in an other entry point
  214.     * @return boolean true : module is ok
  215.     */
  216.     public static function isModuleEnabled ($moduleName, $includingExternal = false) {
  217.         if (!self::$_config)
  218.             throw new Exception ('Configuration is not loaded');
  219.         if ($includingExternal && isset(self::$_config->_externalModulesPathList[$moduleName])) {
  220.             return true;
  221.         }
  222.         return isset(self::$_config->_modulesPathList[$moduleName]);
  223.     }
  224.  
  225.     /**
  226.      * return the real path of a module
  227.      * @param string $module a module name
  228.      * @param boolean $includingExternal  true if we want to know if the module
  229.      *                is also an external module, e.g. in an other entry point
  230.      * @return string the corresponding path
  231.      */
  232.     public static function getModulePath($module, $includingExternal = false){
  233.         if (!self::$_config)
  234.             throw new Exception ('Configuration is not loaded');
  235.  
  236.         if (!isset(self::$_config->_modulesPathList[$module])) {
  237.             if ($includingExternal && isset(self::$_config->_externalModulesPathList[$module])) {
  238.                 return self::$_config->_externalModulesPathList[$module];
  239.             }
  240.             throw new Exception('getModulePath : invalid module name');
  241.         }
  242.         return self::$_config->_modulesPathList[$module];
  243.     }
  244.  
  245.     static protected $modulesContext = array();
  246.  
  247.     /**
  248.     * set the context to the given module
  249.     * @param string $module  the module name
  250.     */
  251.     static function pushCurrentModule ($module){
  252.         array_push (self::$modulesContext, $module);
  253.     }
  254.  
  255.     /**
  256.     * cancel the current context and set the context to the previous module
  257.     * @return string the obsolet module name
  258.     */
  259.     static function popCurrentModule (){
  260.         return array_pop (self::$modulesContext);
  261.     }
  262.  
  263.     /**
  264.     * get the module name of the current context
  265.     * @return string name of the current module
  266.     */
  267.     static function getCurrentModule (){
  268.         return end(self::$modulesContext);
  269.     }
  270. }

Documentation generated on Wed, 04 Jan 2017 22:52:02 +0100 by phpDocumentor 1.4.3