\CodeIgniter\AutoloaderAutoloader

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

Summary

Methods
Properties
Constants
initialize()
register()
addNamespace()
getNamespace()
removeNamespace()
loadClass()
sanitizeFilename()
No public properties found
No constants found
loadInNamespace()
loadLegacy()
requireFile()
discoverComposerNamespaces()
$prefixes
$classmap
N/A
No private methods found
No private properties found
N/A

Properties

$prefixes

$prefixes : array

Stores namespaces as key, and path as values.

Type

array

$classmap

$classmap : array

Stores class name as key, and path as values.

Type

array

Methods

initialize()

initialize(\Config\Autoload  $config, \Config\Modules  $moduleConfig) : $this

Reads in the configuration array (described above) and stores the valid parts that we'll need.

Parameters

\Config\Autoload $config
\Config\Modules $moduleConfig

Returns

$this

register()

register() 

Register the loader with the SPL autoloader stack.

addNamespace()

addNamespace(array|string  $namespace, string  $path = null) : \CodeIgniter\Autoloader\Autoloader

Registers namespaces with the autoloader.

Parameters

array|string $namespace
string $path

Returns

\CodeIgniter\Autoloader\Autoloader

getNamespace()

getNamespace(\CodeIgniter\Autoloader\string  $prefix = null) : array

Get namespaces with prefixes as keys and paths as values.

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

Parameters

\CodeIgniter\Autoloader\string $prefix

Returns

array

removeNamespace()

removeNamespace(string  $namespace) : \CodeIgniter\Autoloader\Autoloader

Removes a single namespace from the psr4 settings.

Parameters

string $namespace

Returns

\CodeIgniter\Autoloader\Autoloader

loadClass()

loadClass(string  $class) : string|false

Loads the class file for a given class name.

Parameters

string $class

The fully qualified class name.

Returns

string|false —

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

sanitizeFilename()

sanitizeFilename(string  $filename) : string

Sanitizes a filename, replacing spaces with dashes.

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

string $filename

Returns

string —

The sanitized filename

loadInNamespace()

loadInNamespace(string  $class) : string|false

Loads the class file for a given class name.

Parameters

string $class

The fully-qualified class name

Returns

string|false —

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

loadLegacy()

loadLegacy(string  $class) : mixed

Attempts to load the class from common locations in previous version of CodeIgniter, namely 'app/Libraries', and 'app/Models'.

Parameters

string $class

The class name. This typically should NOT have a namespace.

Returns

mixed —

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

requireFile()

requireFile(string  $file) : string|false

A central way to require a file is loaded. Split out primarily for testing purposes.

Parameters

string $file

Returns

string|false —

The filename on success, false if the file is not loaded

discoverComposerNamespaces()

discoverComposerNamespaces() 

Locates all PSR4 compatible namespaces from Composer.