Autoloader
in package
An autoloader that uses both PSR4 autoloading, and traditional classmaps.
Given a foo-bar package of classes in the file system at the following paths:
/path/to/packages/foo-bar/
/src
Baz.php # Foo\Bar\Baz
Qux/
Quux.php # Foo\Bar\Qux\Quux
you can add the path to the configuration array that is passed in the constructor. The Config array consists of 2 primary keys, both of which are associative arrays: 'psr4', and 'classmap'.
$Config = [
'psr4' => [
'Foo\Bar' => '/path/to/packages/foo-bar'
],
'classmap' => [
'MyClass' => '/path/to/class/file.php'
]
];
Example:
<?php
// our configuration array
$Config = [ ... ];
$loader = new \CodeIgniter\Autoloader\Autoloader($Config);
// register the autoloader
$loader->register();
Tags
Table of Contents
Properties
- $classmap : array<class-string, string>
- Stores class name as key, and path as values.
- $files : array<int, string>
- Stores files as a list.
- $helpers : array<int, string>
- Stores helper list.
- $prefixes : array<string, array<int, string>>
- Stores namespaces as key, and path as values.
Methods
- addNamespace() : $this
- Registers namespaces with the autoloader.
- getNamespace() : array<string, array<int, string>>|array<int, string>
- Get namespaces with prefixes as keys and paths as values.
- initialize() : $this
- Reads in the configuration array (described above) and stores the valid parts that we'll need.
- initializeKint() : void
- Initializes Kint
- loadHelpers() : void
- Loads helpers
- register() : void
- Register the loader with the SPL autoloader stack.
- removeNamespace() : $this
- Removes a single namespace from the psr4 settings.
- sanitizeFilename() : string
- Check file path.
- unregister() : void
- Unregister autoloader.
- discoverComposerNamespaces() : void
- Locates autoload information from Composer, if available.
- includeFile() : false|string
- A central way to include a file. Split out primarily for testing purposes.
- loadInNamespace() : false|string
- Loads the class file for a given class name.
- autoloadKint() : void
- configureKint() : void
- loadComposerAutoloader() : void
- loadComposerNamespaces() : void
Properties
$classmap
Stores class name as key, and path as values.
protected
array<class-string, string>
$classmap
= []
$files
Stores files as a list.
protected
array<int, string>
$files
= []
$helpers
Stores helper list.
protected
array<int, string>
$helpers
= ['url']
Always load the URL helper, it should be used in most apps.
$prefixes
Stores namespaces as key, and path as values.
protected
array<string, array<int, string>>
$prefixes
= []
Methods
addNamespace()
Registers namespaces with the autoloader.
public
addNamespace(array<string, array<int, string>|string>|string $namespace[, string|null $path = null ]) : $this
Parameters
- $namespace : array<string, array<int, string>|string>|string
- $path : string|null = null
Return values
$thisgetNamespace()
Get namespaces with prefixes as keys and paths as values.
public
getNamespace([string|null $prefix = null ]) : array<string, array<int, string>>|array<int, string>
If a prefix param is set, returns only paths to the given prefix.
Parameters
- $prefix : string|null = null
Tags
Return values
array<string, array<int, string>>|array<int, string>initialize()
Reads in the configuration array (described above) and stores the valid parts that we'll need.
public
initialize(Autoload $config, Modules $modules) : $this
Parameters
- $config : Autoload
- $modules : Modules
Return values
$thisinitializeKint()
Initializes Kint
public
initializeKint([bool $debug = false ]) : void
Parameters
- $debug : bool = false
loadHelpers()
Loads helpers
public
loadHelpers() : void
register()
Register the loader with the SPL autoloader stack.
public
register() : void
removeNamespace()
Removes a single namespace from the psr4 settings.
public
removeNamespace(string $namespace) : $this
Parameters
- $namespace : string
Return values
$thissanitizeFilename()
Check file path.
public
sanitizeFilename(string $filename) : string
No longer used. See https://github.com/codeigniter4/CodeIgniter4/issues/7055
Checks special characters that are illegal in filenames on certain operating systems and special characters requiring special escaping to manipulate at the command line. Replaces spaces and consecutive dashes with a single dash. Trim period, dash and underscore from beginning and end of filename.
Parameters
- $filename : string
Return values
string —The sanitized filename
unregister()
Unregister autoloader.
public
unregister() : void
This method is for testing.
discoverComposerNamespaces()
Locates autoload information from Composer, if available.
protected
discoverComposerNamespaces() : void
No longer used.
includeFile()
A central way to include a file. Split out primarily for testing purposes.
protected
includeFile(string $file) : false|string
Parameters
- $file : string
Return values
false|string —The filename on success, false if the file is not loaded
loadInNamespace()
Loads the class file for a given class name.
protected
loadInNamespace(string $class) : false|string
Parameters
- $class : string
-
The fully-qualified class name
Return values
false|string —The mapped file name on success, or boolean false on fail
autoloadKint()
private
autoloadKint() : void
configureKint()
private
configureKint() : void
loadComposerAutoloader()
private
loadComposerAutoloader(Modules $modules) : void
Parameters
- $modules : Modules
loadComposerNamespaces()
private
loadComposerNamespaces(ClassLoader $composer, array{only?: list, exclude?: list} $composerPackages) : void
Parameters
- $composer : ClassLoader
-
$composerPackages
: array{only?: list
, exclude?: list }