Autoloader
in package
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).
Table of Contents
- $classes : array<string|int, mixed>
- List of classes to file paths.
- $debugCollector : AutoloadCollector
- $namespaces : array<string|int, mixed>
- List of namespaces to directory paths.
- __construct() : mixed
- Autoloader constructor.
- addNamespace() : static
- Adds directory paths to a namespace.
- addNamespaces() : static
- Adds directory paths to namespaces.
- findClassPath() : string|null
- Finds the file path of a class searching in the class mapping and resolving namespaces.
- getClass() : string|null
- Gets a class file path.
- getClasses() : array<string, string>
- Gets all mapped classes.
- getDebugCollector() : AutoloadCollector|null
- getNamespace() : array<int, string>
- Gets the directory paths for a given namespace.
- getNamespaces() : array<string, array<int, string>>
- Gets all mapped namespaces.
- loadClass() : bool
- Loads a class file.
- register() : bool
- Registers the Autoloader::loadClass() as autoload implementation.
- removeClass() : static
- Removes one class from the mapping.
- removeClasses() : static
- Removes classes from the mapping.
- removeNamespace() : static
- Removes one namespace from the mapping.
- removeNamespaces() : static
- Removes namespaces from the mapping.
- setClass() : static
- Sets one class mapping for a file path.
- setClasses() : static
- Sets classes mapping for file paths.
- setDebugCollector() : static
- setNamespace() : static
- Sets one namespace mapping for a directory path.
- setNamespaces() : static
- Sets namespaces mapping for directory paths.
- unregister() : bool
- Unregisters the Autoloader::loadClass() as autoload implementation.
- loadClassFile() : bool
- loadDebug() : bool
- makeRenderedDirectoryPaths() : array<int, string>
- renderDirectoryPath() : string
- Gets the canonicalized absolute pathname for a directory path.
- renderFilePath() : string
- Renders the canonicalized absolute pathname for a file path.
- renderRealName() : string
- Renders a class or namespace name without lateral slashes.
- sortNamespaces() : void
Properties
$classes
List of classes to file paths.
protected
array<string|int, mixed>
$classes
= []
$debugCollector
protected
AutoloadCollector
$debugCollector
$namespaces
List of namespaces to directory paths.
protected
array<string|int, mixed>
$namespaces
= []
Methods
__construct()
Autoloader constructor.
public
__construct([bool $register = true ][, string $extensions = '.php' ]) : mixed
Parameters
- $register : bool = true
-
Register the Autoloader::loadClass() as autoload implementation
- $extensions : string = '.php'
-
A comma delimited list of file extensions for spl_autoload
Tags
Return values
mixed —addNamespace()
Adds directory paths to a namespace.
public
addNamespace(string $namespace, array<string|int, string>|string $dir) : static
Parameters
- $namespace : string
-
Namespace name
- $dir : array<string|int, string>|string
-
Directory path
Return values
static —addNamespaces()
Adds directory paths to namespaces.
public
addNamespaces(array<string, string[]|string> $namespaces) : static
Parameters
- $namespaces : array<string, string[]|string>
-
Namespace names as keys and directory paths as values
Return values
static —findClassPath()
Finds the file path of a class searching in the class mapping and resolving namespaces.
public
findClassPath(string $class) : string|null
Parameters
- $class : string
-
Fully qualified class name (with namespace)
Return values
string|null —The class file path or null if not found
getClass()
Gets a class file path.
public
getClass(string $name) : string|null
Parameters
- $name : string
-
Fully qualified class name (with namespace)
Return values
string|null —The file path or null if class is not mapped
getClasses()
Gets all mapped classes.
public
getClasses() : array<string, string>
Return values
array<string, string> —An array of class names as keys and file paths as values
getDebugCollector()
public
getDebugCollector() : AutoloadCollector|null
Return values
AutoloadCollector|null —getNamespace()
Gets the directory paths for a given namespace.
public
getNamespace(string $name) : array<int, string>
Parameters
- $name : string
-
Namespace name
Return values
array<int, string> —The namespace directory paths
getNamespaces()
Gets all mapped namespaces.
public
getNamespaces() : array<string, array<int, string>>
Return values
array<string, array<int, string>> —loadClass()
Loads a class file.
public
loadClass(string $class) : bool
Parameters
- $class : string
-
Fully qualified class name (with namespace)
Return values
bool —TRUE if the file is loaded, otherwise FALSE
register()
Registers the Autoloader::loadClass() as autoload implementation.
public
register([string $extensions = '.php' ]) : bool
Parameters
- $extensions : string = '.php'
-
A comma delimited list of file extensions for spl_autoload
Tags
Return values
bool —True on success or false on failure
removeClass()
Removes one class from the mapping.
public
removeClass(string $name) : static
Parameters
- $name : string
-
Fully qualified class name (with namespace)
Return values
static —removeClasses()
Removes classes from the mapping.
public
removeClasses(array<int, string> $names) : static
Parameters
- $names : array<int, string>
-
List of class names
Return values
static —removeNamespace()
Removes one namespace from the mapping.
public
removeNamespace(string $name) : static
Parameters
- $name : string
-
Namespace name
Return values
static —removeNamespaces()
Removes namespaces from the mapping.
public
removeNamespaces(array<string|int, string> $names) : static
Parameters
- $names : array<string|int, string>
-
List of namespace names
Return values
static —setClass()
Sets one class mapping for a file path.
public
setClass(string $name, string $filepath) : static
Parameters
- $name : string
-
Fully qualified class name (with namespace)
- $filepath : string
-
Class file path
Return values
static —setClasses()
Sets classes mapping for file paths.
public
setClasses(array<string, string> $classes) : static
Parameters
- $classes : array<string, string>
-
Associative array with class names as keys and file paths as values
Return values
static —setDebugCollector()
public
setDebugCollector([AutoloadCollector|null $debugCollector = null ][, string $name = 'default' ]) : static
Parameters
- $debugCollector : AutoloadCollector|null = null
- $name : string = 'default'
Return values
static —setNamespace()
Sets one namespace mapping for a directory path.
public
setNamespace(string $namespace, array<string|int, string>|string $dir) : static
Parameters
- $namespace : string
-
Namespace name
- $dir : array<string|int, string>|string
-
Directory path
Return values
static —setNamespaces()
Sets namespaces mapping for directory paths.
public
setNamespaces(array<string, string[]|string> $namespaces) : static
Parameters
- $namespaces : array<string, string[]|string>
-
Namespace names as keys and directory paths as values
Return values
static —unregister()
Unregisters the Autoloader::loadClass() as autoload implementation.
public
unregister() : bool
Tags
Return values
bool —True on success or false on failure
loadClassFile()
protected
loadClassFile(string $class) : bool
Parameters
- $class : string
Return values
bool —loadDebug()
protected
loadDebug(string $class) : bool
Parameters
- $class : string
Return values
bool —makeRenderedDirectoryPaths()
protected
makeRenderedDirectoryPaths(array<string|int, string> $directories) : array<int, string>
Parameters
- $directories : array<string|int, string>
Return values
array<int, string> —renderDirectoryPath()
Gets the canonicalized absolute pathname for a directory path.
protected
renderDirectoryPath(string $path) : string
Adds a trailing slash.
Parameters
- $path : string
Return values
string —renderFilePath()
Renders the canonicalized absolute pathname for a file path.
protected
renderFilePath(string $path) : string
Parameters
- $path : string
-
File path
Return values
string —renderRealName()
Renders a class or namespace name without lateral slashes.
protected
renderRealName(string $name) : string
Parameters
- $name : string
-
Class or namespace name
Return values
string —sortNamespaces()
protected
sortNamespaces() : void