Elgg  Version 4.3
ClassLoader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
48 class ClassLoader {
49 
50  protected $namespaces = [];
51  protected $prefixes = [];
52  protected $fallbacks = [];
53 
57  protected $config;
58 
62  protected $map;
63 
67  protected $missing = [];
68 
74  public function __construct(Config $config) {
75  $this->map = new \Elgg\ClassMap();
76  $this->config = $config;
77 
78  $this->register();
79  }
80 
86  public function getClassMap() {
87  return $this->map;
88  }
89 
95  public function getNamespaces() {
96  return $this->namespaces;
97  }
98 
104  public function getPrefixes() {
105  return $this->prefixes;
106  }
107 
114  public function registerNamespaces(array $namespaces) {
115  foreach ($namespaces as $namespace => $locations) {
116  $this->namespaces[$namespace] = (array) $locations;
117  }
118  }
119 
127  public function registerNamespace($namespace, $paths) {
128  $this->namespaces[$namespace] = (array) $paths;
129  }
130 
137  public function registerPrefixes(array $classes) {
138  foreach ($classes as $prefix => $locations) {
139  $this->prefixes[$prefix] = (array) $locations;
140  }
141  }
142 
150  public function registerPrefix($prefix, $paths) {
151  $this->prefixes[$prefix] = (array) $paths;
152  }
153 
160  public function addFallback($path) {
161  $this->fallbacks[] = rtrim($path, '/\\');
162  }
163 
169  public function register() {
170  spl_autoload_register([$this, 'loadClass']);
171  }
172 
179  public function loadClass($class) {
180  // is missing? return
181  if (isset($this->missing[$class])) {
182  return;
183  }
184 
185  $file = $this->map->getPath($class);
186  if (!empty($file) && (!$this->config->class_loader_verify_file_existence || is_file($file))) {
187  require $file;
188  return;
189  }
190 
191  $file = $this->findFile($class);
192  if (!empty($file)) {
193  $this->map->setPath($class, $file);
194  $this->map->setAltered(true);
195  require $file;
196  }
197 
198  // add to missing
199  $this->missing[$class] = true;
200  }
201 
209  public function findFile($class) {
210  if ('\\' == $class[0]) {
211  $class = substr($class, 1);
212  }
213 
214  $pos = strrpos($class, '\\');
215  if (false !== $pos) {
216  // namespaced class name
217  $namespace = substr($class, 0, $pos);
218  $className = substr($class, $pos + 1);
219  $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace)
220  . DIRECTORY_SEPARATOR
221  . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
222  foreach ($this->namespaces as $ns => $dirs) {
223  if (0 !== strpos($namespace, $ns)) {
224  continue;
225  }
226 
227  foreach ($dirs as $dir) {
228  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
229  if (is_file($file)) {
230  return $file;
231  }
232  }
233  }
234  } else {
235  // PEAR-like class name
236  $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
237  foreach ($this->prefixes as $prefix => $dirs) {
238  if (0 !== strpos($class, $prefix)) {
239  continue;
240  }
241 
242  foreach ($dirs as $dir) {
243  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
244  if (is_file($file)) {
245  return $file;
246  }
247  }
248  }
249  }
250 
251  foreach ($this->fallbacks as $dir) {
252  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
253  if (is_file($file)) {
254  return $file;
255  }
256  }
257  }
258 }
registerNamespaces(array $namespaces)
Registers an array of namespaces.
$paths
We handle here two possible locations of composer-generated autoload file.
Definition: autoloader.php:7
$classes
Definition: users.php:21
A class/interface/trait autoloader for PHP.
Definition: ClassLoader.php:48
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.
$class
Definition: summary.php:44
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
$path
Definition: details.php:68
registerNamespace($namespace, $paths)
Registers a namespace.
$namespaces
Definition: default.php:33
getNamespaces()
Gets the configured namespaces.
Definition: ClassLoader.php:95
registerPrefix($prefix, $paths)
Registers a set of classes using the PEAR naming convention.
elgg require
Throw an error if the required package isn&#39;t present.
Definition: deprecated.js:176
__construct(Config $config)
Constructor.
Definition: ClassLoader.php:74
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.
getClassMap()
Get the class map.
Definition: ClassLoader.php:86