Autoloader
Class Autoloader
Description #
Allows for autoloading of LearnDash classes.
Example usage:
// will be `/var/www/site/wp-content/plugins/sfwd-lms'
$this_dir = dirname(__FILE__);
// gets hold of the singleton instance of the class
$autoloader = Autoloader::instance();
// register one by one or use `register_prefixes` method
$autoloader->register_prefix( 'LearnDash__Admin__', $this_dir . '/src/admin' );
$autoloader->register_prefix( 'LearnDash__Admin__', $this_dir . '/src/another-dir' );
$autoloader->register_prefix( 'LearnDash__Utils__', $this_dir . '/src/some-dir' );
// register a direct class to path
$autoloader->register_class( 'LearnDash_Some_Deprecated_Class', $this_dir . '/src/deprecated/LearnDash_Some_Deprecated_Class.php' );
// register a fallback dir to be searched for the class before giving up
$autoloader->add_fallback_dir( $this_dir . '/all-the-classes' );
// calls `spl_autoload_register`
$autoloader->register_autoloader();
// class will be searched in the path
// `/var/www/site/wp-content/plugins/sfwd-lms/src/admin/Some_Class.php'
// and
// `/var/www/site/wp-content/plugins/sfwd-lms/src/another-dir/Some_Class.php'
$i = new LearnDash__Admin__Some_Class();
// class will be searched in the path
// `/var/www/site/wp-content/plugins/sfwd-lms/utils/some-dir/Some_Util.php'
$i = new LearnDash__Utils__Some_Util();
// class will be searched in the path
// `/var/www/site/wp-content/plugins/sfwd-lms/src/deprecated/LearnDash_Some_Deprecated_Class.php'
$i = new LearnDash_Some_Deprecated_Class();
Source #
File: src/Core/Autoloader.php
Methods #
- add_fallback_dir — Adds a folder to search for classes that were not found among the prefixed ones.
- autoload — Includes the file defining a class.
- get_class_path — Gets the absolute path to a class file.
- get_dir_separator — Returns the directory separator used by the class loader.
- get_fallback_path — Gets the absolute path to a class file using the fallback dirs.
- get_prefix_by_slug — Get the registered prefix by slug
- get_prefixed_path — Returns the path to the file defining a class.
- instance — Returns the singleton instance of the class.
- normalize_root_dir — Normalizes the root dir by trimming any trailing `/`.
- register_autoloader — Triggers the registration of the autoload method in the SPL autoload register.
- register_class — Registers a class path.
- register_prefix — Associates a class prefix to an absolute path.
- register_prefixes — Registers prefixes and root dirs using an array.