Documentation

Autoloader
in package

CodeIgniter Autoloader

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();

Table of Contents

$classmap  : array<string, string>
Stores class name as key, and path as values.
$files  : array<int, string>
Stores files as a list.
$prefixes  : array<string, array<string|int, string>>
Stores namespaces as key, and path as values.
addNamespace()  : $this
Registers namespaces with the autoloader.
getNamespace()  : array<string|int, mixed>
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.
loadClass()  : string|false
Loads the class file for a given class name.
loadClassmap()  : string|false
Load a class using available class mapping.
register()  : mixed
Register the loader with the SPL autoloader stack.
removeNamespace()  : $this
Removes a single namespace from the psr4 settings.
sanitizeFilename()  : string
Sanitizes a filename, replacing spaces with dashes.
discoverComposerNamespaces()  : void
Locates autoload information from Composer, if available.
includeFile()  : string|false
A central way to include a file. Split out primarily for testing purposes.
loadInNamespace()  : string|false
Loads the class file for a given class name.

Properties

$classmap

Stores class name as key, and path as values.

protected array<string, string> $classmap = []

$files

Stores files as a list.

protected array<int, string> $files = []

$prefixes

Stores namespaces as key, and path as values.

protected array<string, array<string|int, string>> $prefixes = []

Methods

addNamespace()

Registers namespaces with the autoloader.

public addNamespace(array<string|int, mixed>|string $namespace[, string $path = null ]) : $this
Parameters
$namespace : array<string|int, mixed>|string
$path : string = null
Return values
$this

getNamespace()

Get namespaces with prefixes as keys and paths as values.

public getNamespace([string|null $prefix = null ]) : array<string|int, mixed>

If a prefix param is set, returns only paths to the given prefix.

Parameters
$prefix : string|null = null
Return values
array<string|int, mixed>

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
$this

loadClass()

Loads the class file for a given class name.

public loadClass(string $class) : string|false
Parameters
$class : string

The fully qualified class name.

Return values
string|false

The mapped file on success, or boolean false on failure.

loadClassmap()

Load a class using available class mapping.

public loadClassmap(string $class) : string|false
Parameters
$class : string
Return values
string|false

register()

Register the loader with the SPL autoloader stack.

public register() : mixed
Return values
mixed

removeNamespace()

Removes a single namespace from the psr4 settings.

public removeNamespace(string $namespace) : $this
Parameters
$namespace : string
Return values
$this

sanitizeFilename()

Sanitizes a filename, replacing spaces with dashes.

public sanitizeFilename(string $filename) : string

Removes 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

discoverComposerNamespaces()

Locates autoload information from Composer, if available.

protected discoverComposerNamespaces() : void
Return values
void

includeFile()

A central way to include a file. Split out primarily for testing purposes.

protected includeFile(string $file) : string|false
Parameters
$file : string
Return values
string|false

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) : string|false
Parameters
$class : string

The fully-qualified class name

Return values
string|false

The mapped file name on success, or boolean false on fail

Search results