Source for file jProfiles.class.php

Documentation is available at jProfiles.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  utils
  5. @author      Laurent Jouanneau
  6. @contributor Yannick Le Guédart, Julien Issler
  7. @copyright   2011 Laurent Jouanneau, 2007 Yannick Le Guédart, 2011 Julien Issler
  8. @link        http://jelix.org
  9. @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10. */
  11.  
  12. /**
  13. * class to read profiles from the profiles.ini.php
  14. @package     jelix
  15. @subpackage  utils
  16. */
  17. class jProfiles {
  18.  
  19.     /**
  20.      * loaded profiles
  21.      * @var array 
  22.      */
  23.     protected static $_profiles null;
  24.  
  25.     /**
  26.      * pool of objects loaded for profiles
  27.      * @var array   array of object
  28.      */
  29.     protected static $_objectPool array();
  30.  
  31.  
  32.     protected static function loadProfiles({
  33.         $file jApp::configPath('profiles.ini.php');
  34.         self::$_profiles parse_ini_file($filetrue);
  35.     }
  36.  
  37.     /**
  38.     * load properties of a profile.
  39.     *
  40.     * A profile is a section in the profiles.ini.php file. Profiles are belong
  41.     * to a category. Each section names is composed by "category:profilename".
  42.     *
  43.     * The given name can be a profile name or an alias of a profile. An alias
  44.     * is a parameter name in the category section of the ini file, and the value
  45.     * of this parameter should be a profile name.
  46.     *
  47.     * @param string $category     the profile category
  48.     * @param string   $name  profile name or alias of a profile name. if empty, use the default profile
  49.     * @param boolean  $noDefault  if true and if the profile doesn't exist, throw an error instead of getting the default profile
  50.     * @return array  properties
  51.     */
  52.     public static function get ($category$name=''$noDefault false{
  53.         if (self::$_profiles === null{
  54.             self::loadProfiles();
  55.         }
  56.  
  57.         if ($name == '')
  58.             $name 'default';
  59.         $section $category.':'.$name;
  60.         $targetName $section;
  61.  
  62.         if (isset(self::$_profiles[$category.':__common__'])) {
  63.             $common self::$_profiles[$category.':__common__'];
  64.         }
  65.         else
  66.             $common null;
  67.  
  68.         // the name attribute created in this method will be the name of the connection
  69.         // in the connections pool. So profiles of aliases and real profiles should have
  70.         // the same name attribute.
  71.  
  72.         if (isset(self::$_profiles[$section])) {
  73.             self::$_profiles[$section]['_name'$name;
  74.             if ($common)
  75.                 return array_merge($commonself::$_profiles[$section]);
  76.             return self::$_profiles[$section];
  77.         }
  78.         else if (isset(self::$_profiles[$category][$name])) {
  79.             // it is an alias
  80.             $name self::$_profiles[$category][$name];
  81.             $targetName $category.':'.$name;
  82.         }
  83.         // if the profile doesn't exist, we take the default one
  84.         elseif (!$noDefault{
  85. #ifnot ENABLE_OPTIMIZED_SOURCE
  86.             //trigger_error(jLocale::get('jelix~errors.profile.use.default', $name), E_USER_NOTICE);
  87. #endif
  88.             if (isset(self::$_profiles[$category.':default'])) {
  89.                 self::$_profiles[$category.':default']['_name''default';
  90.                 if ($common)
  91.                     return array_merge($commonself::$_profiles[$category.':default']);
  92.                 return self::$_profiles[$category.':default'];
  93.             }
  94.             elseif (isset(self::$_profiles[$category]['default'])) {
  95.                 $name self::$_profiles[$category]['default'];
  96.                 $targetName $category.':'.$name;
  97.             }
  98.         }
  99.         else {
  100.             if ($name == 'default')
  101.                 throw new jException('jelix~errors.profile.default.unknown'$category);
  102.             else
  103.                 throw new jException('jelix~errors.profile.unknown',array($name$category));
  104.         }
  105.  
  106.         if (isset(self::$_profiles[$targetName]&& is_array(self::$_profiles[$targetName])) {
  107.             self::$_profiles[$targetName]['_name'$name;
  108.             if ($common)
  109.                 return array_merge($commonself::$_profiles[$targetName]);
  110.             return self::$_profiles[$targetName];
  111.         }
  112.         else {
  113.             throw new jException('jelix~errors.profile.unknown'array($name$category));
  114.         }
  115.     }
  116.  
  117.     /**
  118.      * add an object in the objects pool, corresponding to a profile
  119.      * @param string $category the profile category
  120.      * @param string $name the name of the profile  (value of _name in the retrieved profile)
  121.      * @param object $obj the object to store
  122.      */
  123.     public static function storeInPool($category$name$object{
  124.         self::$_objectPool[$category][$name$object;
  125.     }
  126.  
  127.     /**
  128.      * store an object in the objects pool, corresponding to a profile
  129.      * @param string $category the profile category
  130.      * @param string $name the name of the profile (value of _name in the retrieved profile)
  131.      * @return object|nullthe stored object
  132.      */
  133.     public static function getFromPool($category$name{
  134.         if (isset(self::$_objectPool[$category][$name]))
  135.             return self::$_objectPool[$category][$name];
  136.         return null;
  137.     }
  138.  
  139.     /**
  140.      * add an object in the objects pool, corresponding to a profile
  141.      * or store the object retrieved from the function, which accepts a profile
  142.      * as parameter (array)
  143.      * @param string $category the profile category
  144.      * @param string $name the name of the profile (will be given to jProfiles::get)
  145.      * @param string|array $function the function name called to retrieved the object. It uses call_user_func.
  146.      * @param boolean  $noDefault  if true and if the profile doesn't exist, throw an error instead of getting the default profile
  147.      * @return object|nullthe stored object
  148.      */
  149.     public static function getOrStoreInPool($category$name$function$nodefault=false{
  150.         $profile self::get($category$name$nodefault);
  151.         if (isset(self::$_objectPool[$category][$profile['_name']]))
  152.             return self::$_objectPool[$category][$profile['_name']];
  153.         $obj call_user_func($function$profile);
  154.         if ($obj)
  155.             self::$_objectPool[$category][$profile['_name']] $obj;
  156.         return $obj;
  157.     }
  158.  
  159.  
  160.     /**
  161.      * create a temporary new profile
  162.      * @param string $category the profile category
  163.      * @param string $name the name of the profile
  164.      * @param array|string$params parameters of the profile. key=parameter name, value=parameter value.
  165.      *                       we can also indicate a name of an other profile, to create an alias
  166.      */
  167.     public static function createVirtualProfile ($category$name$params{
  168.         global $gJConfig;
  169.         if ($name == ''{
  170.            throw new jException('jelix~errors.profile.virtual.no.name'$category);
  171.         }
  172.  
  173.         if (self::$_profiles === null{
  174.             self::loadProfiles();
  175.         }
  176.         if (is_string($params)) {
  177.             self::$_profiles[$category][$name$params;
  178.         }
  179.         else {
  180.             $params['_name'$name;
  181.             self::$_profiles[$category.':'.$name$params;
  182.         }
  183.         unset (self::$_objectPool[$category][$name])// close existing connection with the same pool name
  184.     }
  185.  
  186.     /**
  187.      * clear the loaded profiles to force to reload the profiles file.
  188.      * WARNING: it destroy all objects stored in the pool!
  189.      */
  190.     public static function clear({
  191.         self::$_profiles null;
  192.         self::$_objectPool array();
  193.     }
  194. }

Documentation generated on Mon, 19 Sep 2011 14:13:18 +0200 by phpDocumentor 1.4.3