52 protected array $prefixes = [];
54 protected array $fallbacks = [];
64 protected array $missing = [];
72 $this->map = new \Elgg\ClassMap();
101 return $this->prefixes;
112 foreach ($namespaces as $namespace => $locations) {
113 $this->namespaces[$namespace] = (array) $locations;
126 $this->namespaces[$namespace] = (array)
$paths;
137 foreach ($classes as $prefix => $locations) {
138 $this->prefixes[$prefix] = (array) $locations;
151 $this->prefixes[$prefix] = (array)
$paths;
170 public function register() {
171 spl_autoload_register([$this,
'loadClass']);
183 if (isset($this->missing[
$class])) {
187 $file = $this->map->getPath($class);
188 if (!empty($file) && (!$this->config->class_loader_verify_file_existence || is_file($file))) {
193 $file = $this->findFile($class);
195 $this->map->setPath($class, $file);
196 $this->map->setAltered(
true);
201 $this->missing[
$class] =
true;
217 if ($pos !==
false) {
221 $normalizedClass =
str_replace(
'\\', DIRECTORY_SEPARATOR, $namespace)
222 . DIRECTORY_SEPARATOR
223 .
str_replace(
'_', DIRECTORY_SEPARATOR, $className) .
'.php';
224 foreach ($this->namespaces as $ns => $dirs) {
225 if (!str_starts_with($namespace, $ns)) {
229 foreach ($dirs as $dir) {
230 $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
231 if (is_file($file)) {
239 foreach ($this->prefixes as $prefix => $dirs) {
240 if (!str_starts_with(
$class, $prefix)) {
244 foreach ($dirs as $dir) {
245 $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
246 if (is_file($file)) {
253 foreach ($this->fallbacks as $dir) {
254 $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
255 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.
findFile($class)
Finds the path to the file where the class is defined.
__construct(protected Config $config)
Constructor.
registerPrefixes(array $classes)
Registers an array of classes using the PEAR naming convention.
getPrefixes()
Gets the configured class prefixes.
getClassMap()
Get the class map.