XOOPS  2.6.0
Psr0ClassLoader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Xoops\Core;
4 
33 class Psr0ClassLoader
34 {
35  private $fileExtension = '.php';
36  private $namespace;
37  private $includePath;
38  private $namespaceSeparator = '\\';
39 
49  public function __construct($ns = null, $includePath = null)
50  {
51  $this->namespace = $ns;
52  $this->includePath = $includePath;
53  }
54 
65  public static function addLoader($namespace, $path, $separator = '\\', $extension = '.php')
66  {
67  $loaderClass = get_called_class();
68  $loader = new $loaderClass($namespace, $path);
69  $loader->setNamespaceSeparator($separator);
70  $loader->setFileExtension($extension);
71  $loader->register();
72  return $loader;
73  }
81  public function setNamespaceSeparator($sep)
82  {
83  $this->namespaceSeparator = $sep;
84  }
85 
91  public function getNamespaceSeparator()
92  {
93  return $this->namespaceSeparator;
94  }
95 
103  public function setIncludePath($includePath)
104  {
105  $this->includePath = $includePath;
106  }
107 
113  public function getIncludePath()
114  {
115  return $this->includePath;
116  }
117 
125  public function setFileExtension($fileExtension)
126  {
127  $this->fileExtension = $fileExtension;
128  }
129 
135  public function getFileExtension()
136  {
137  return $this->fileExtension;
138  }
139 
145  public function register()
146  {
147  spl_autoload_register(array($this, 'loadClass'));
148  }
149 
155  public function unregister()
156  {
157  spl_autoload_unregister(array($this, 'loadClass'));
158  }
159 
167  public function loadClass($className)
168  {
169  if (null === $this->namespace
170  || $this->namespace.$this->namespaceSeparator
171  === substr($className, 0, strlen($this->namespace.$this->namespaceSeparator))
172  ) {
173  $fileName = '';
174  $namespace = '';
175  if (false !== ($lastNsPos = strripos($className, $this->namespaceSeparator))) {
176  $namespace = substr($className, 0, $lastNsPos);
177  $className = substr($className, $lastNsPos + 1);
178  $fileName = str_replace(
179  $this->namespaceSeparator,
181  $namespace
183  }
184  $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
185 
186  $absolute = ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . $fileName;
187  if (file_exists($absolute)) {
188  require $absolute;
189  }
190  //require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') . $fileName;
191  }
192  }
193 }
$path
Definition: execute.php:31
defined('DS') or define('DS' DIRECTORY_SEPARATOR
Definition: common.php:41
__construct()
Definition: Xoops.php:121