Loader
Loader
Global class file loader.
This class contains methods for auto-loading classes from files in the Hazaar library path. Ordinarily there will be no need for developers to use this class directly but it does contain a few methods for working with paths and library files.
This class is not meant to be instantiated directly and instances should be retrieved using the Loader::getInstance()method.
Example
$loader = Hazaar\Loader::getInstance();
!!! notice The loader class is loaded automatically when starting the application. There should be no need to use the Loader instance directly and static methods have been provided for some extra functionality.
!!! warning Instantiating this class directly can have undefined results.
- Full name:
\Hazaar\Loader
Properties
paths
public array<string,string[]> $paths
instance
private static ?\Hazaar\Loader $instance
- This property is static.
Methods
__construct
Initialise a new loader.
public __construct(): mixed
!!! warning Do NOT instantiate this class directly. See Loader::getInstance() on how to get a new Loader instance.
fixDirectorySeparator
public static fixDirectorySeparator(string $path): string
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string |
getInstance
Return the current instance of the Loader object.
public static getInstance(): \Hazaar\Loader
- This method is static.
register
Register this loader instance as a class autoloader.
public register(): void
unregister
Unregister this loader instance as a class autoloader.
public unregister(): void
addIncludePath
public addIncludePath(string $path): void
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string |
addSearchPath
Add a new search path for loading classes from library files.
public addSearchPath(string $type, string $path): bool
The path type can be anything if you are using the loader to load your own library files. There are built in path types for loading Hazaar library files.
- FILE_PATH_ROOT - Path that contains the whole project
- FILE_PATH_MODEL - Path contains model classes
- FILE_PATH_VIEW - Path contains view files.
- FILE_PATH_CONTROLLER - Path contains controller classes.
- FILE_PATH_SUPPORT - Path contains support files. Used by the Application::runDirect()method.
- FILE_PATH_CONFIG - Configuration files
Parameters:
Parameter | Type | Description |
---|---|---|
$type | string | the path type to add |
$path | string | the path to add |
setSearchPath
Sets the search path for a file type.
public setSearchPath(mixed $type, mixed $path): bool
This is the same as addSearchPath except that it overwrites any existing paths.
Parameters:
Parameter | Type | Description |
---|---|---|
$type | mixed | the path type to add |
$path | mixed | the path to add |
addSearchPaths
Add multiple search paths from an array.
public addSearchPaths(array $array): void
Parameters:
Parameter | Type | Description |
---|---|---|
$array | array | Array containing type/path pairs |
getSearchPaths
Return an array of search paths for this loader instance.
public getSearchPaths(?string $type = null): array
Parameters:
Parameter | Type | Description |
---|---|---|
$type | ?string |
Return Value:
Array of search paths
isAbsolutePath
Checks if a given path is an absolute path.
public static isAbsolutePath(string $path): bool
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | the path to check |
Return Value:
returns true if the path is an absolute path, false otherwise
getFilePath
Return the absolute filesystem path to a file.
public static getFilePath(string $type, ?string $search_file = null, string $base_path = null, bool $case_insensitive = false): string
By default this method uses the application path as the base path.
This method also checks that the file exists. If the file does not exist then null will be returned.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$type | string | The path type to search. See Loader::addSearchPath() |
$search_file | ?string | |
$base_path | string | The path to use as a search base if there are no paths of the requested type |
$case_insensitive | bool | By default paths are case sensitive. In some circumstances this might not be desirable so set this TRUE to perform a(slower)case insensitive search. |
Return Value:
The absolute path to the file if it exists. NULL otherwise.
resolve
Resolve a filename within any of the search paths.
public static resolve(string $filename): string
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$filename | string |
Return Value:
Absolute path to the file
loadClassFromFile
Loads a class from a source file.
public static loadClassFromFile(string $className): void
This is the main class loader used by the __autoload()PHP trigger. It is responsible for loading the files that hold class source definitions by determining the correct file to load based on the class name.
First check if the class name is a single word that ends with 'Controller', designating it as a controller class. If that matches then the class is loaded from the controller path.
Otherwise we check if the class starts with Application and load from the application path.
Lastly we do a 2 stage search of the library paths. Stage 1 looks for a correlating path while stage 2 looks for the class in a sub-directory of the module name.
We do 2 stage class path checking.
- Stage 1: Look for the class in a correlating path. eg:[[Hazaar\Application]] in path Hazaar/Application.php
- Stage 2: If stage 1 fails, look in a module sub-directory. eg:[[Hazaar\Application]] in path Hazaar/Application/Application.php
If they both fail, the class is not found and we throw a pretty exception.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$className | string | The name of the class to load |
resolveRealPath
private static resolveRealPath(string $filename, bool $case_insensitive = false): ?string
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$filename | string | |
$case_insensitive | bool |
Automatically generated on 2024-11-14