Source for file jConfigAutoloader.class.php

Documentation is available at jConfigAutoloader.class.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage core
  5. * @author     Laurent Jouanneau
  6. * @copyright  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 jConfigAutoloader {
  17.  
  18.  
  19.     public function __construct($config) {
  20.         $this->config = $config;
  21.     }
  22.  
  23.     /**
  24.      * @var object a configuration object readed from an ini file
  25.      * @see jConfig
  26.      */
  27.     protected $config = null;
  28.  
  29.     /**
  30.      * the method that should be called by the autoload system
  31.      */
  32.     public function loadClass($className) {
  33.         $path = $this->getPath($className);
  34.         if (is_array($path)) {
  35.             foreach($path as $p) {
  36.                 if (file_exists($p)) {
  37.                     require($p);
  38.                     return true;
  39.                 }
  40.             }
  41.         }
  42.         else if ($path) {
  43.             require($path);
  44.             return true;
  45.         }
  46.         return false;
  47.     }
  48.  
  49.     /**
  50.      * @return string the full path of the file declaring the given class
  51.      */
  52.     protected function getPath($className) {
  53.  
  54.         if (!$this->config)
  55.             return '';
  56.  
  57.         $className = ltrim($className, '\\');
  58.  
  59.         /*
  60.         [_autoload_class]
  61.         className = includefile
  62.         */
  63.         if (isset($this->config->_autoload_class[$className])) {
  64.             return $this->config->_autoload_class[$className];
  65.         }
  66.  
  67.         $lastNsPos = strripos($className, '\\');
  68.         if ($lastNsPos !== false) {
  69.             // the class name contains a namespace, let's split ns and class
  70.             $namespace = substr($className, 0, $lastNsPos);
  71.             $class = substr($className, $lastNsPos + 1);
  72.         }
  73.         else {
  74.             $namespace = '';
  75.             $class = &$className;
  76.             // the given class name does not contains namespace
  77.         }
  78.  
  79.         /*
  80.         [_autoload_namespace]
  81.         namespace = "/path|.ext"
  82.         */
  83.         foreach($this->config->_autoload_namespace as $ns=>$info) {
  84.             if (strpos($className, $ns) === 0) {
  85.                 $path = '';
  86.                 if ($lastNsPos !== false) {
  87.                     if ($namespace) {
  88.                         $path = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  89.                     }
  90.                 }
  91.  
  92.                 list($incPath, $ext) = explode('|', $info);
  93.                 $fileName = str_replace('_', DIRECTORY_SEPARATOR, $class) . $ext;
  94.                 return $incPath.DIRECTORY_SEPARATOR.$path.$fileName;
  95.             }
  96.         }
  97.  
  98.         /*
  99.         [_autoload_namespacepathmap]
  100.         namespace = "/path|.ext"
  101.         */
  102.         foreach($this->config->_autoload_namespacepathmap as $ns=>$info) {
  103.             if (strpos($className, $ns) === 0) {
  104.                 $path = '';
  105.                 if ($lastNsPos !== false) {
  106.                     $namespace = substr($namespace, strlen($ns)+1);
  107.                     if ($namespace) {
  108.                         $path = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  109.                     }
  110.                 }
  111.                 list($incPath, $ext) = explode('|', $info);
  112.                 $fileName = str_replace('_', DIRECTORY_SEPARATOR, $class) . $ext;
  113.                 return $incPath.DIRECTORY_SEPARATOR.$path.$fileName;
  114.             }
  115.         }
  116.  
  117.         /*
  118.         [_autoload_classpattern]
  119.         regexp[] = "regexp"
  120.         path[]= "/path|ext"
  121.         */
  122.         if (isset($this->config->_autoload_classpattern['regexp'])) {
  123.             foreach ($this->config->_autoload_classpattern['regexp'] as $k=>$reg) {
  124.                 if (preg_match($reg, $className)) {
  125.                     list($incPath, $ext) = explode('|', $this->config->_autoload_classpattern['path'][$k]);
  126.                     return $incPath. DIRECTORY_SEPARATOR .$className.$ext;
  127.                 }
  128.             }
  129.         }
  130.  
  131.         /*
  132.         [_autoload_includepath]
  133.         path[]="/path|.ext"
  134.         */
  135.         $pathList = array();
  136.         if (isset($this->config->_autoload_includepath['path'])) {
  137.             foreach($this->config->_autoload_includepath['path'] as $info) {
  138.                 list($incPath, $ext) = explode('|',$info);
  139.                 if ($namespace)
  140.                     $path = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  141.                 else $path = '';
  142.                 $pathList[] = $incPath.DIRECTORY_SEPARATOR.$path.str_replace('_', DIRECTORY_SEPARATOR, $class) . $ext;
  143.             }
  144.         }
  145.  
  146.         /*
  147.         [_autoload_includepathmap]
  148.         path[]="/path|.ext"
  149.         */
  150.         if (isset($this->config->_autoload_includepathmap['path'])) {
  151.             foreach($this->config->_autoload_includepathmap['path'] as $info) {
  152.                 list($incPath, $ext) = explode('|',$info);
  153.                 $pathList[] = $incPath.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class) . $ext;
  154.             }
  155.         }
  156.         if (count($pathList)) {
  157.             return $pathList;
  158.         }
  159.         return '';
  160.     }
  161. }

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