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 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 $tempPath = '';
  21.  
  22.     protected static $appPath = '';
  23.  
  24.     protected static $varPath = '';
  25.  
  26.     protected static $logPath = '';
  27.  
  28.     protected static $configPath = '';
  29.  
  30.     protected static $wwwPath = '';
  31.  
  32.     protected static $scriptPath = '';
  33.  
  34.     protected static $_isInit = false;
  35.  
  36.     protected static $env = 'www/';
  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.     /**
  72.      * init path from JELIX_APP_* defines or define JELIX_APP_*,
  73.      * depending of how the bootstrap has been initialized.
  74.      * The goal of this method is to support the transition
  75.      * between the old way of defining path, and the new way
  76.      * in jelix 1.3.
  77.      * @deprecated
  78.      */
  79.     public static function initLegacy() {
  80.         if (self::$_isInit) {
  81.             if (!defined('JELIX_APP_PATH')) {
  82.                 define ('JELIX_APP_PATH',         self::$appPath);
  83.                 define ('JELIX_APP_TEMP_PATH',    self::tempPath());
  84.                 define ('JELIX_APP_VAR_PATH',     self::$varPath);
  85.                 define ('JELIX_APP_LOG_PATH',     self::$logPath);
  86.                 define ('JELIX_APP_CONFIG_PATH',  self::$configPath);
  87.                 define ('JELIX_APP_WWW_PATH',     self::$wwwPath);
  88.                 define ('JELIX_APP_CMD_PATH',     self::$scriptPath);
  89.             }
  90.         }
  91.         else if (defined('JELIX_APP_PATH')) {
  92.             self::initPaths(JELIX_APP_PATH,
  93.                             JELIX_APP_WWW_PATH,
  94.                             JELIX_APP_VAR_PATH,
  95.                             JELIX_APP_LOG_PATH,
  96.                             JELIX_APP_CONFIG_PATH,
  97.                             JELIX_APP_CMD_PATH);
  98.             self::setTempBasePath(JELIX_APP_TEMP_PATH);
  99.         }
  100.     }
  101.  
  102.     public static function appPath($file='') { return self::$appPath.$file; }
  103.     public static function varPath($file='') { return self::$varPath.$file; }
  104.     public static function logPath($file='') { return self::$logPath.$file; }
  105.     public static function configPath($file='') { return self::$configPath.$file; }
  106.     public static function wwwPath($file='') { return self::$wwwPath.$file; }
  107.     public static function scriptsPath($file='') { return self::$scriptPath.$file; }
  108.     public static function tempPath($file='') { return self::$tempBasePath.self::$env.$file; }
  109.     public static function tempBasePath() { return self::$tempBasePath; }
  110.  
  111.     public static function setTempBasePath($path) {
  112.         self::$tempBasePath = $path;
  113.     }
  114.  
  115.     public static function setEnv($env) {
  116.         if (substr($env,-1) != '/')
  117.             $env.='/';
  118.         self::$env = $env;
  119.     }
  120.  
  121.     protected static $contextBackup = array();
  122.  
  123.     /**
  124.      * save all path and others variables relatives to the application, so you can
  125.      * temporary change the context to an other application
  126.      */
  127.     public static function saveContext() {
  128.         self::$contextBackup[] = array(self::$appPath, self::$varPath, self::$logPath, self::$configPath,
  129.                                        self::$wwwPath, self::$scriptPath, self::$tempBasePath);
  130.     }
  131.  
  132.     /**
  133.      * restore the previous context of the application
  134.      */
  135.     public static function restoreContext() {
  136.         if (!count(self::$contextBackup))
  137.             return;
  138.         list(self::$appPath, self::$varPath, self::$logPath, self::$configPath,
  139.              self::$wwwPath, self::$scriptPath, self::$tempBasePath) = array_pop(self::$contextBackup);
  140.     }
  141.  
  142.     /**
  143.      * load a plugin from a plugin directory (any type of plugins)
  144.      * @param string $name the name of the plugin
  145.      * @param string $type the type of the plugin
  146.      * @param string $suffix the suffix of the filename
  147.      * @param string $classname the name of the class to instancy
  148.      * @param mixed $args  the argument for the constructor of the class. null = no argument.
  149.      * @return null|object   null if the plugin doesn't exists
  150.      */
  151.     public static function loadPlugin($name, $type, $suffix, $classname, $args = null) {
  152.  
  153.         if (!class_exists($classname,false)) {
  154.             global $gJConfig;
  155.             $optname = '_pluginsPathList_'.$type;
  156.             if (!isset($gJConfig->$optname))
  157.                 return null;
  158.             $opt = & $gJConfig->$optname;
  159.             if (!isset($opt[$name])
  160.                 || !file_exists($opt[$name]) ){
  161.                 return null;
  162.             }
  163.             require_once($opt[$name].$name.$suffix);
  164.         }
  165.         if (!is_null($args))
  166.             return new $classname($args);
  167.         else
  168.             return new $classname();
  169.     }
  170. }

Documentation generated on Wed, 24 Sep 2014 21:56:12 +0200 by phpDocumentor 1.4.3