Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file Loader.class.php

Documentation is available at Loader.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: Loader.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @uses class utililty class (class loader)
  11.  * @package Zikula_Core
  12.  */
  13.  
  14. /**
  15.  * Loader
  16.  *
  17.  * @package Zikula_Core
  18.  * @subpackage Loader
  19.  */
  20. class Loader
  21. {
  22.     /**
  23.      * Load a file from the specified location in the pn file tree
  24.      *
  25.      * @param fileName    The name of the file to load
  26.      * @param path        The path prefix to use (optional) (default=null)
  27.      * @param exitOnError whether or not exit upon error (optional) (default=true)
  28.      * @param returnVar   The variable to return from the sourced file (optional) (default=null)
  29.      *
  30.      * @return string The file which was loaded
  31.      */
  32.     function loadFile ($fileName$path=null$exitOnError=true$returnVar=null)
  33.     {
  34.         if (!$fileName{
  35.             return pn_exit ("Invalid file specification [$fileName] ...");
  36.         }
  37.  
  38.         $file null;
  39.         if ($path{
  40.             $file "$path/$fileName";
  41.         else {
  42.             $file $fileName;
  43.         }
  44.  
  45.         $file DataUtil::formatForOS($file);
  46.         if (Loader::_is_included($file)) {
  47.             return $file;
  48.         }
  49.  
  50.         if (is_file($file&& is_readable($file)) {
  51.             if (Loader::includeOnce($file)) {
  52.                 if ($returnVar{
  53.                     return $$returnVar;
  54.                 else {
  55.                     return $file;
  56.                 }
  57.             }
  58.         }
  59.  
  60.         if ($exitOnError{
  61.             return pn_exit ("Unable to load file [$fileName] ...");
  62.         }
  63.  
  64.         return false;
  65.     }
  66.  
  67.  
  68.     /**
  69.      * Load all files from the specified location in the pn file tree
  70.      *
  71.      * @param files        An array of filenames to load
  72.      * @param path         The path prefix to use (optional) (default='null')
  73.      * @param exitOnError  whether or not exit upon error (optional) (default=true)
  74.      *
  75.      * @return boolean true
  76.      */
  77.     function loadAllFiles ($files$path=null$exitOnError=false)
  78.     {
  79.         return Loader::loadFiles ($files$pathtrue$exitOnError);
  80.     }
  81.  
  82.  
  83.     /**
  84.      * Return after the first successful file load. This corresponds to the
  85.      * default behaviour of loadFiles().
  86.      *
  87.      * @param files        An array of filenames to load
  88.      * @param path         The path prefix to use (optional) (default='null')
  89.      * @param exitOnError  whether or not exit upon error (optional) (default=true)
  90.      *
  91.      * @return boolean true
  92.      */
  93.     function loadOneFile ($files$path=null$exitOnError=false)
  94.     {
  95.         return Loader::loadFiles ($files$pathfalse$exitOnError);
  96.     }
  97.  
  98.  
  99.     /**
  100.      * Load multiple files from the specified location in the pn file tree
  101.      * Note that in it's default invokation, this method exits after the
  102.      * first successful file load.
  103.      *
  104.      * @param files       Array of filenames to load
  105.      * @param path        The path prefix to use (optional) (default='null')
  106.      * @param all         whether or not to load all files or exit upon 1st successful load (optional) (default=false)
  107.      * @param exitOnError whether or not exit upon error (optional) (default=true)
  108.      * @param returnVar   The variable to return if $all==false (optional) (default=null)
  109.      *
  110.      * @return boolean true
  111.      */
  112.     function loadFiles ($files$path=null$all=false$exitOnError=false$returnVar='')
  113.     {
  114.         if (!is_array($files|| !$files{
  115.             return pn_exit ("Invalid file array specification ...");
  116.         }
  117.  
  118.         $files array_unique ($files);
  119.  
  120.         $loaded false;
  121.         foreach ($files as $file{
  122.             $rc Loader::loadFile ($file$path$exitOnError$returnVar);
  123.  
  124.             if ($rc{
  125.                 $loaded true;
  126.             }
  127.  
  128.             if ($loaded && !$all{
  129.                 break;
  130.             }
  131.         }
  132.  
  133.         if ($returnVar && !$all{
  134.             return $rc;
  135.         }
  136.  
  137.         return $loaded;
  138.     }
  139.  
  140.  
  141.     /**
  142.      * Load a class file from the specified location in the file tree
  143.      *
  144.      * @param className    The class-basename to load
  145.      * @param classPath    The path prefix to use (optional) (default='includes/pnobjlib')
  146.      * @param exitOnError  whether or not exit upon error (optional) (default=true)
  147.      *
  148.      * @return string The file name which was loaded
  149.      */
  150.     function loadClass ($className$classPath='includes/pnobjlib'$exitOnError=true)
  151.     {
  152.         if (!$className{
  153.             return pn_exit ("Invalid class specification [$className] ...");
  154.         }
  155.  
  156.         if (class_exists($className)) {
  157.             return $className;
  158.         }
  159.  
  160.         $classFile $className '.class.php';
  161.         $rc Loader::loadFile ($classFile"config/classes/$classPath"false);
  162.         if (!$rc{
  163.             $rc Loader::loadFile ($classFile$classPath$exitOnError);
  164.         }
  165.  
  166.         return $rc;
  167.     }
  168.  
  169.  
  170.     /**
  171.      * Load a PNObject extended class from the given module. The given class name is
  172.      * prefixed with 'PN' and underscores are removed to produce a proper class name.
  173.      *
  174.      * @param module        The module to load from
  175.      * @param base_obj_type The base object type for which to load the class
  176.      * @param array         If true, load the array class instead of the single-object class.
  177.      * @param exitOnError   whether or not exit upon error (optional) (default=true)
  178.      * @param prefix        Override parameter for the default PN prefix (default=PN)
  179.      *
  180.      * @return string The ClassName which was loaded from the file
  181.      */
  182.     function loadClassFromModule ($module$base_obj_type$array=false$exitOnError=false$prefix='PN')
  183.     {
  184.         if (!$module{
  185.             return pn_exit ("Invalid module specification [$module] ...");
  186.         }
  187.  
  188.         if (!$base_obj_type{
  189.             return pn_exit ("Invalid base_obj_type specification [$base_obj_type] ...");
  190.         }
  191.  
  192.         $prefix = (string)$prefix;
  193.  
  194.         if (strpos($base_obj_type,'_'!== false{
  195.             $c $base_obj_type;
  196.             $class '';
  197.             while (($p=strpos($c,'_')) !== false{
  198.                 $class .= ucwords(substr ($c0$p));
  199.                 $c substr ($c$p+1);
  200.             }
  201.             $class .= ucwords($c);
  202.         else {
  203.             $class ucwords($base_obj_type);
  204.         }
  205.  
  206.         $class $prefix $class;
  207.         if ($array{
  208.             $class .= 'Array';
  209.         }
  210.  
  211.         // prevent unncessary reloading
  212.         if (class_exists($class)) {
  213.             return $class;
  214.         }
  215.  
  216.         $classFiles array();
  217.         $classFiles["config/classes/$module/{$class}.class.php";
  218.         $classFiles["system/$module/classes/{$class}.class.php";
  219.         $classFiles["modules/$module/classes/{$class}.class.php";
  220.  
  221.         foreach ($classFiles as $classFile{
  222.             $classFile DataUtil::formatForOS($classFile);
  223.             if (file_exists($classFile&& is_readable($classFile)) {
  224.                 if (Loader::includeOnce($classFile)) {
  225.                     return $class;
  226.                 }
  227.  
  228.                 if ($exitOnError{
  229.                     return pn_exit ("Unable to load class [$classFile] ...");
  230.                 }
  231.  
  232.                 return false;
  233.             }
  234.         }
  235.  
  236.         return false;
  237.     }
  238.  
  239.  
  240.     /**
  241.      * Load a PNObjectArray extended class from the given module. The given class name is
  242.      * prefixed with 'PN' and underscores are removed to produce a proper class name.
  243.      *
  244.      * @param module        The module to load from
  245.      * @param base_obj_type The base object type for which to load the class
  246.      * @param exitOnError   whether or not exit upon error (optional) (default=true)
  247.      * @param prefix        Override parameter for the default PN prefix (default=PN)
  248.      *
  249.      * @return string The ClassName which was loaded from the file
  250.      */
  251.     function loadArrayClassFromModule ($module$base_obj_type$exitOnError=false$prefix='PN')
  252.     {
  253.         return Loader::loadClassFromModule ($module$base_obj_typetrue$exitOnError$prefix);
  254.     }
  255.  
  256.  
  257.     /**
  258.      * cache for results of includes
  259.      * done this was because min version is not PHP5
  260.      *
  261.      * @param string $file 
  262.      * @param bool $write 
  263.      * @return bool 
  264.      */
  265.     function _is_included($file$write=false)
  266.     {
  267.         static $cache array();
  268.  
  269.         // read mode
  270.         if (!$write{
  271.             if (isset($cache[$file])) {
  272.                 return true;
  273.             }
  274.         else {
  275.             // write mode
  276.             $cache[$filetrue;
  277.         }
  278.         return false;
  279.     }
  280.  
  281.  
  282.     /**
  283.      * Internal include_once, considerably faster
  284.      *
  285.      * @author Drak
  286.      * @param string $file 
  287.      * @return bool True if file was included - false if not found or included before.
  288.      */
  289.     function includeOnce($file)
  290.     {
  291.         if (defined('_PNINSTALLVER')) {
  292.             return include_once($file);
  293.         }
  294.         // check if file has been included already
  295.         if (!Loader::_is_included($file)) {
  296.             if (include($file)) {
  297.                 // if we manaed to include it, cache the result
  298.                 Loader::_is_included($filetrue);
  299.                 return true;
  300.             }
  301.         }
  302.  
  303.         return false;
  304.     }
  305.  
  306.  
  307.     /**
  308.      * Internal require_once, considerably faster
  309.      *
  310.      * @author Drak
  311.      * @param string $file 
  312.      * @return bool 
  313.      */
  314.     function requireOnce($file)
  315.     {
  316.         if (defined('_PNINSTALLVER')) {
  317.             return require_once($file);
  318.         }
  319.         // check if file has been included already
  320.         if (!Loader::_is_included($file)) {
  321.             if (require($file)) {
  322.                 // if we manaed to include it, cache the result
  323.                 Loader::_is_included($filetrue);
  324.                 return true;
  325.             }
  326.         }
  327.  
  328.         return false;
  329.     }
  330. }

Documentation generated on Fri, 18 Jul 2008 21:47:37 +0200 by phpDocumentor 1.4.1