Autoloader class

Framework\Autoload\Autoloader

Class Autoloader.

The Autoloader class allows to set namespace directories to search for files
(PSR4) and set the absolute path of classes without namespaces (PSR0).

Defined in libraries/autoload/src/Autoloader.php

Properties #

$classes
List of classes to file paths.
$namespaces
List of namespaces to directory paths.
$debugCollector
-

$classes

protected array $classes = array ( )

List of classes to file paths.

$namespaces

protected array $namespaces = array ( )

List of namespaces to directory paths.

$debugCollector

protected \Framework\Autoload\Debug\AutoloadCollector $debugCollector

Methods #

__construct()
Autoloader constructor.
addNamespace()
Adds directory paths to a namespace.
addNamespaces()
Adds directory paths to namespaces.
findClassPath()
Finds the file path of a class searching in the class mapping and resolving namespaces.
getClass()
Gets a class file path.
getClasses()
Gets all mapped classes.
getDebugCollector()
-
getNamespace()
Gets the directory paths for a given namespace.
getNamespaces()
Gets all mapped namespaces.
loadClass()
Loads a class file.
register()
Registers the Autoloader::loadClass() as autoload implementation.
removeClass()
Removes one class from the mapping.
removeClasses()
Removes classes from the mapping.
removeNamespace()
Removes one namespace from the mapping.
removeNamespaces()
Removes namespaces from the mapping.
setClass()
Sets one class mapping for a file path.
setClasses()
Sets classes mapping for file paths.
setDebugCollector()
-
setNamespace()
Sets one namespace mapping for a directory path.
setNamespaces()
Sets namespaces mapping for directory paths.
unregister()
Unregisters the Autoloader::loadClass() as autoload implementation.
loadClassFile()
-
loadDebug()
-
makeRenderedDirectoryPaths()
-
renderDirectoryPath()
Gets the canonicalized absolute pathname for a directory path.
renderFilePath()
Renders the canonicalized absolute pathname for a file path.
renderRealName()
Renders a class or namespace name without lateral slashes.
sortNamespaces()
-

__construct()

public function __construct(bool $register = true, string $extensions = '.php')

Autoloader constructor.

Parameters
  • bool $register - Register the Autoloader::loadClass() as autoload implementation
  • string $extensions - A comma delimited list of file extensions for spl_autoload

addNamespace()

public function addNamespace(string $namespace, array|string $dir): static

Adds directory paths to a namespace.

Parameters
  • string $namespace - Namespace name
  • array|string $dir - Directory path
Returns static

addNamespaces()

public function addNamespaces(array $namespaces): static

Adds directory paths to namespaces.

Parameters
  • array $namespaces - Namespace names as keys and directory paths as values
Returns static

findClassPath()

public function findClassPath(string $class): ?string

Finds the file path of a class searching in the class mapping and resolving namespaces.

Parameters
  • string $class - Fully qualified class name (with namespace)
Returns ?string - The class file path or null if not found

getClass()

public function getClass(string $name): ?string

Gets a class file path.

Parameters
  • string $name - Fully qualified class name (with namespace)
Returns ?string - The file path or null if class is not mapped

getClasses()

public function getClasses(): array

Gets all mapped classes.

Returns array - An array of class names as keys and file paths as values

getDebugCollector()

public function getDebugCollector(): ?\Framework\Autoload\Debug\AutoloadCollector
Returns ?\Framework\Autoload\Debug\AutoloadCollector

getNamespace()

public function getNamespace(string $name): array

Gets the directory paths for a given namespace.

Parameters
  • string $name - Namespace name
Returns array - The namespace directory paths

getNamespaces()

public function getNamespaces(): array

Gets all mapped namespaces.

Returns array

loadClass()

public function loadClass(string $class): bool

Loads a class file.

Parameters
  • string $class - Fully qualified class name (with namespace)
Returns bool - TRUE if the file is loaded, otherwise FALSE

register()

public function register(string $extensions = '.php'): bool

Registers the Autoloader::loadClass() as autoload implementation.

Parameters
  • string $extensions - A comma delimited list of file extensions for spl_autoload
Returns bool - True on success or false on failure

removeClass()

public function removeClass(string $name): static

Removes one class from the mapping.

Parameters
  • string $name - Fully qualified class name (with namespace)
Returns static

removeClasses()

public function removeClasses(array $names): static

Removes classes from the mapping.

Parameters
  • array $names - List of class names
Returns static

removeNamespace()

public function removeNamespace(string $name): static

Removes one namespace from the mapping.

Parameters
  • string $name - Namespace name
Returns static

removeNamespaces()

public function removeNamespaces(array $names): static

Removes namespaces from the mapping.

Parameters
  • array $names - List of namespace names
Returns static

setClass()

public function setClass(string $name, string $filepath): static

Sets one class mapping for a file path.

Parameters
  • string $name - Fully qualified class name (with namespace)
  • string $filepath - Class file path
Returns static

setClasses()

public function setClasses(array $classes): static

Sets classes mapping for file paths.

Parameters
  • array $classes - Associative array with class names as keys and file paths as values
Returns static

setDebugCollector()

public function setDebugCollector(?\Framework\Autoload\Debug\AutoloadCollector $debugCollector = NULL, string $name = 'default'): static
Parameters
  • ?\Framework\Autoload\Debug\AutoloadCollector $debugCollector
  • string $name
Returns static

setNamespace()

public function setNamespace(string $namespace, array|string $dir): static

Sets one namespace mapping for a directory path.

Parameters
  • string $namespace - Namespace name
  • array|string $dir - Directory path
Returns static

setNamespaces()

public function setNamespaces(array $namespaces): static

Sets namespaces mapping for directory paths.

Parameters
  • array $namespaces - Namespace names as keys and directory paths as values
Returns static

unregister()

public function unregister(): bool

Unregisters the Autoloader::loadClass() as autoload implementation.

Returns bool - True on success or false on failure

loadClassFile()

protected function loadClassFile(string $class): bool
Parameters
  • string $class
Returns bool

loadDebug()

protected function loadDebug(string $class): bool
Parameters
  • string $class
Returns bool

makeRenderedDirectoryPaths()

protected function makeRenderedDirectoryPaths(array $directories): array
Parameters
  • array $directories
Returns array

renderDirectoryPath()

protected function renderDirectoryPath(string $path): string

Gets the canonicalized absolute pathname for a directory path.

Adds a trailing slash.

Parameters
  • string $path
Returns string

renderFilePath()

protected function renderFilePath(string $path): string

Renders the canonicalized absolute pathname for a file path.

Parameters
  • string $path - File path
Returns string

renderRealName()

protected function renderRealName(string $name): string

Renders a class or namespace name without lateral slashes.

Parameters
  • string $name - Class or namespace name
Returns string

sortNamespaces()

protected function sortNamespaces(): void
Returns void