Elgg  Version 1.11
ClassLoader.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg;
3 
49 class ClassLoader {
50 
51  protected $namespaces = array();
52  protected $prefixes = array();
53  protected $fallbacks = array();
54 
58  protected $map;
59 
65  public function __construct(\Elgg\ClassMap $map) {
66  $this->map = $map;
67  }
68 
74  public function getClassMap() {
75  return $this->map;
76  }
77 
83  public function getNamespaces() {
84  return $this->namespaces;
85  }
86 
92  public function getPrefixes() {
93  return $this->prefixes;
94  }
95 
102  public function registerNamespaces(array $namespaces) {
103  foreach ($namespaces as $namespace => $locations) {
104  $this->namespaces[$namespace] = (array)$locations;
105  }
106  }
107 
115  public function registerNamespace($namespace, $paths) {
116  $this->namespaces[$namespace] = (array)$paths;
117  }
118 
125  public function registerPrefixes(array $classes) {
126  foreach ($classes as $prefix => $locations) {
127  $this->prefixes[$prefix] = (array)$locations;
128  }
129  }
130 
138  public function registerPrefix($prefix, $paths) {
139  $this->prefixes[$prefix] = (array)$paths;
140  }
141 
148  public function addFallback($path) {
149  $this->fallbacks[] = rtrim($path, '/\\');
150  }
151 
157  public function register() {
158  spl_autoload_register(array($this, 'loadClass'));
159  }
160 
167  public function loadClass($class) {
168  $file = $this->map->getPath($class);
169  if ($file && is_readable($file)) {
170  require $file;
171  return;
172  }
173 
174  $file = $this->findFile($class);
175  if ($file && is_readable($file)) {
176  $this->map->setPath($class, $file);
177  $this->map->setAltered(true);
178  require $file;
179  }
180  }
181 
189  public function findFile($class) {
190  if ('\\' == $class[0]) {
191  $class = substr($class, 1);
192  }
193 
194  $pos = strrpos($class, '\\');
195  if (false !== $pos) {
196  // namespaced class name
197  $namespace = substr($class, 0, $pos);
198  $className = substr($class, $pos + 1);
199  $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace)
200  . DIRECTORY_SEPARATOR
201  . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
202  foreach ($this->namespaces as $ns => $dirs) {
203  if (0 !== strpos($namespace, $ns)) {
204  continue;
205  }
206 
207  foreach ($dirs as $dir) {
208  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
209  if (is_file($file)) {
210  return $file;
211  }
212  }
213  }
214 
215  } else {
216  // PEAR-like class name
217  $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
218  foreach ($this->prefixes as $prefix => $dirs) {
219  if (0 !== strpos($class, $prefix)) {
220  continue;
221  }
222 
223  foreach ($dirs as $dir) {
224  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
225  if (is_file($file)) {
226  return $file;
227  }
228  }
229  }
230  }
231 
232  foreach ($this->fallbacks as $dir) {
233  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
234  if (is_file($file)) {
235  return $file;
236  }
237  }
238  }
239 }
240 
registerNamespaces(array $namespaces)
Registers an array of namespaces.
__construct(\Elgg\ClassMap $map)
Constructor.
Definition: ClassLoader.php:65
loadClass($class)
Loads the given class or interface, possibly updating the class map.
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
addFallback($path)
Add a directory to search if no registered directory is found.
registerNamespace($namespace, $paths)
Registers a namespace.
$namespaces
Definition: default.php:29
getNamespaces()
Gets the configured namespaces.
Definition: ClassLoader.php:83
Save menu items.
$classes
Definition: full.php:29
elgg require
Throw an error if the required package isn&#39;t present.
Definition: elgglib.js:164
registerPrefix($prefix, $paths)
Registers a set of classes using the PEAR naming convention.
findFile($class)
Finds the path to the file where the class is defined.
registerPrefixes(array $classes)
Registers an array of classes using the PEAR naming convention.
$path
Definition: invalid.php:17
getPrefixes()
Gets the configured class prefixes.
Definition: ClassLoader.php:92
getClassMap()
Get the class map.
Definition: ClassLoader.php:74