Elgg  Version 3.0
ClassLoader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
50 class ClassLoader {
51 
52  protected $namespaces = [];
53  protected $prefixes = [];
54  protected $fallbacks = [];
55 
59  protected $map;
60 
64  protected $missing = [];
65 
71  public function __construct(\Elgg\ClassMap $map) {
72  $this->map = $map;
73  }
74 
80  public function getClassMap() {
81  return $this->map;
82  }
83 
89  public function getNamespaces() {
90  return $this->namespaces;
91  }
92 
98  public function getPrefixes() {
99  return $this->prefixes;
100  }
101 
108  public function registerNamespaces(array $namespaces) {
109  foreach ($namespaces as $namespace => $locations) {
110  $this->namespaces[$namespace] = (array) $locations;
111  }
112  }
113 
121  public function registerNamespace($namespace, $paths) {
122  $this->namespaces[$namespace] = (array) $paths;
123  }
124 
131  public function registerPrefixes(array $classes) {
132  foreach ($classes as $prefix => $locations) {
133  $this->prefixes[$prefix] = (array) $locations;
134  }
135  }
136 
144  public function registerPrefix($prefix, $paths) {
145  $this->prefixes[$prefix] = (array) $paths;
146  }
147 
154  public function addFallback($path) {
155  $this->fallbacks[] = rtrim($path, '/\\');
156  }
157 
163  public function register() {
164  spl_autoload_register([$this, 'loadClass']);
165  }
166 
173  public function loadClass($class) {
174  $file = $this->map->getPath($class);
175  if ($file && is_readable($file)) {
176  require $file;
177  return;
178  }
179 
180  // is missing? return
181  if (isset($this->missing[$class])) {
182  return;
183  }
184 
185  $file = $this->findFile($class);
186  if ($file && is_readable($file)) {
187  $this->map->setPath($class, $file);
188  $this->map->setAltered(true);
189  require $file;
190  }
191 
192  // add to missing
193  $this->missing[$class] = true;
194  }
195 
203  public function findFile($class) {
204  if ('\\' == $class[0]) {
205  $class = substr($class, 1);
206  }
207 
208  $pos = strrpos($class, '\\');
209  if (false !== $pos) {
210  // namespaced class name
211  $namespace = substr($class, 0, $pos);
212  $className = substr($class, $pos + 1);
213  $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace)
214  . DIRECTORY_SEPARATOR
215  . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
216  foreach ($this->namespaces as $ns => $dirs) {
217  if (0 !== strpos($namespace, $ns)) {
218  continue;
219  }
220 
221  foreach ($dirs as $dir) {
222  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
223  if (is_file($file)) {
224  return $file;
225  }
226  }
227  }
228  } else {
229  // PEAR-like class name
230  $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
231  foreach ($this->prefixes as $prefix => $dirs) {
232  if (0 !== strpos($class, $prefix)) {
233  continue;
234  }
235 
236  foreach ($dirs as $dir) {
237  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
238  if (is_file($file)) {
239  return $file;
240  }
241  }
242  }
243  }
244 
245  foreach ($this->fallbacks as $dir) {
246  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
247  if (is_file($file)) {
248  return $file;
249  }
250  }
251  }
252 }
registerNamespaces(array $namespaces)
Registers an array of namespaces.
if(!array_key_exists($filename, $text_files)) $file
__construct(\Elgg\ClassMap $map)
Constructor.
Definition: ClassLoader.php:71
$paths
We handle here two possible locations of composer-generated autoload file.
Definition: autoloader.php:7
A class/interface/trait autoloader for PHP.
Definition: ClassLoader.php:50
$path
Definition: details.php:89
loadClass($class)
Loads the given class or interface, possibly updating the class map.
addFallback($path)
Add a directory to search if no registered directory is found.
A map of class names to absolute file paths.
Definition: ClassMap.php:13
registerNamespace($namespace, $paths)
Registers a namespace.
$namespaces
Definition: default.php:34
getNamespaces()
Gets the configured namespaces.
Definition: ClassLoader.php:89
Configuration exception.
$class
Definition: field.php:29
$classes
Definition: full.php:25
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.
getPrefixes()
Gets the configured class prefixes.
Definition: ClassLoader.php:98
getClassMap()
Get the class map.
Definition: ClassLoader.php:80