52 protected $prefixes = [];
54 protected $fallbacks = [];
69 protected $missing = [];
77 $this->map = new \Elgg\ClassMap();
107 return $this->prefixes;
118 foreach ($namespaces as $namespace => $locations) {
119 $this->namespaces[$namespace] = (array) $locations;
132 $this->namespaces[$namespace] = (array)
$paths;
143 foreach ($classes as $prefix => $locations) {
144 $this->prefixes[$prefix] = (array) $locations;
157 $this->prefixes[$prefix] = (array)
$paths;
176 public function register() {
177 spl_autoload_register([$this,
'loadClass']);
189 if (isset($this->missing[
$class])) {
193 $file = $this->map->getPath($class);
194 if (!empty($file) && (!$this->config->class_loader_verify_file_existence || is_file($file))) {
199 $file = $this->findFile($class);
201 $this->map->setPath($class, $file);
202 $this->map->setAltered(
true);
207 $this->missing[
$class] =
true;
223 if ($pos !==
false) {
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)) {
235 foreach ($dirs as $dir) {
236 $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
237 if (is_file($file)) {
245 foreach ($this->prefixes as $prefix => $dirs) {
246 if (!str_starts_with(
$class, $prefix)) {
250 foreach ($dirs as $dir) {
251 $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
252 if (is_file($file)) {
259 foreach ($this->fallbacks as $dir) {
260 $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
261 if (is_file($file)) {
registerNamespaces(array $namespaces)
Registers an array of namespaces.
$paths
We handle here two possible locations of composer-generated autoload file.
A class/interface/trait autoloader for PHP.
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.
$config
Advanced site settings, debugging section.
registerNamespace($namespace, $paths)
Registers a namespace.
getNamespaces()
Gets the configured namespaces.
registerPrefix($prefix, $paths)
Registers a set of classes using the PEAR naming convention.
__construct(Config $config)
Constructor.
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.