Hazaar
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();
$loader->loadController('index');

p(notice notice-info) . 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.

p(notice notice-warning) . Instantiating this class directly can have undefined results.

Summary
MethodsPropertiesConstants
No constants
Properties
$application
$application
Static
$instance
$instance
$paths
$paths
Methods
__construct()
__construct($application)
Initialise a new loader

p(notice notice-warning) . Do NOT instantiate this class directly. See Loader::getInstance() on how to get a new Loader instance.

addIncludePath()
addIncludePath($path)
addSearchPath()
addSearchPath($type, $path)

Add a new search path for loading classes from library files

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
addSearchPaths()
addSearchPaths($array)

Add multiple search paths from an array

Static
fixDirectorySeparator()
fixDirectorySeparator($path)
Static
getClassSource()
getClassSource($path)

Check the library paths to make sure the file exists somewhere

Static
getFilePath()
getFilePath($type, $search_file = null, $base_path = 'application_path', $case_insensitive = false) : string

Return the absolute filesystem path to a file. 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.

Static
getInstance()
getInstance($application = null)

Return the current instance of the Loader object.

Static
getModuleFilePath()
getModuleFilePath($search_file = null, $module = null, $case_insensitive = false)
getSearchPaths()
getSearchPaths($type = null) : Array

Return an array of search paths for this loader instance

Static
isAbsolutePath()
isAbsolutePath($path)
Static
loadClassFromFile()
loadClassFromFile($class_name)

Loads a class from a source file. 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.

loadController()
loadController($controller) : mixed

This method is used to load a new instance of a controller class. There are some built-in 'magic controllers' that this method will automatically load upon request.

These controllers are:

If no controller can be found the default site controller will be loaded.

register()
register()

Register this loader instance as a class autoloader

Static
resolve()
resolve($filename) : string

Resolve a filename within any of the search paths

Static
resolveRealPath()
resolveRealPath($filename, $case_insensitive = false)
setApplication()
setApplication($application)
setSearchPath()
setSearchPath($type, $path) : boolean
Sets the search path for a file type

This is the same as addSearchPath except that it overwrites any existing paths.

unregister()
unregister()

Unregister this loader instance as a class autoloader