Elgg  Version 5.1
ClassLoader.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
48 class ClassLoader {
49 
50  protected $namespaces = [];
51 
52  protected $prefixes = [];
53 
54  protected $fallbacks = [];
55 
59  protected $config;
60 
64  protected $map;
65 
69  protected $missing = [];
70 
76  public function __construct(Config $config) {
77  $this->map = new \Elgg\ClassMap();
78  $this->config = $config;
79 
80  $this->register();
81  }
82 
88  public function getClassMap() {
89  return $this->map;
90  }
91 
97  public function getNamespaces() {
98  return $this->namespaces;
99  }
100 
106  public function getPrefixes() {
107  return $this->prefixes;
108  }
109 
117  public function registerNamespaces(array $namespaces) {
118  foreach ($namespaces as $namespace => $locations) {
119  $this->namespaces[$namespace] = (array) $locations;
120  }
121  }
122 
131  public function registerNamespace($namespace, $paths) {
132  $this->namespaces[$namespace] = (array) $paths;
133  }
134 
142  public function registerPrefixes(array $classes) {
143  foreach ($classes as $prefix => $locations) {
144  $this->prefixes[$prefix] = (array) $locations;
145  }
146  }
147 
156  public function registerPrefix($prefix, $paths) {
157  $this->prefixes[$prefix] = (array) $paths;
158  }
159 
167  public function addFallback($path) {
168  $this->fallbacks[] = rtrim($path, '/\\');
169  }
170 
176  public function register() {
177  spl_autoload_register([$this, 'loadClass']);
178  }
179 
187  public function loadClass($class) {
188  // is missing? return
189  if (isset($this->missing[$class])) {
190  return;
191  }
192 
193  $file = $this->map->getPath($class);
194  if (!empty($file) && (!$this->config->class_loader_verify_file_existence || is_file($file))) {
195  require $file;
196  return;
197  }
198 
199  $file = $this->findFile($class);
200  if (!empty($file)) {
201  $this->map->setPath($class, $file);
202  $this->map->setAltered(true);
203  require $file;
204  }
205 
206  // add to missing
207  $this->missing[$class] = true;
208  }
209 
217  public function findFile($class) {
218  if ($class[0] === '\\') {
219  $class = substr($class, 1);
220  }
221 
222  $pos = strrpos($class, '\\');
223  if ($pos !== false) {
224  // namespaced class name
225  $namespace = substr($class, 0, $pos);
226  $className = substr($class, $pos + 1);
227  $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace)
228  . DIRECTORY_SEPARATOR
229  . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
230  foreach ($this->namespaces as $ns => $dirs) {
231  if (!str_starts_with($namespace, $ns)) {
232  continue;
233  }
234 
235  foreach ($dirs as $dir) {
236  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
237  if (is_file($file)) {
238  return $file;
239  }
240  }
241  }
242  } else {
243  // PEAR-like class name
244  $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
245  foreach ($this->prefixes as $prefix => $dirs) {
246  if (!str_starts_with($class, $prefix)) {
247  continue;
248  }
249 
250  foreach ($dirs as $dir) {
251  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
252  if (is_file($file)) {
253  return $file;
254  }
255  }
256  }
257  }
258 
259  foreach ($this->fallbacks as $dir) {
260  $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
261  if (is_file($file)) {
262  return $file;
263  }
264  }
265  }
266 }
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:29
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:70
registerNamespace($namespace, $paths)
Registers a namespace.
$namespaces
Definition: default.php:32
getNamespaces()
Gets the configured namespaces.
Definition: ClassLoader.php:97
registerPrefix($prefix, $paths)
Registers a set of classes using the PEAR naming convention.
__construct(Config $config)
Constructor.
Definition: ClassLoader.php:76
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:88