Ferreteria/v0.5/classloader

From Woozle Writes Code
Jump to navigation Jump to search

The ClassLoader (loader/ClassLoader.php handles loading of all other classes in Ferreteria. It is (of necessity) written so it can be used without loading any other part of Ferreteria, aside from the cEnv class (of which it loads the appropriate variant in order to make diagnostics easier to read).

Class Structure

  • the Loader (class caLoader) is just to encapsulate the top-level module/class registry and autoloading.
  • a Library object (class caLibrary):
    • has a name
    • has a set of Modules which are only loaded by explicit request from the application
    • has a BasePath, to which all Module files are relative; defaults to csLoader's BasePath
    • uses the FileLoader trait (tFileLoader)
    • invokes a specific index file which loads the rest of the Library via its Module object
      • ...which may itself invoke other index files via caModule->IncludeIndex()
      • ...the format of which is defined by cModule descendants
    • The index file (example: dropins/basics/index.dropin.php) is only processed when its Library is requested
  • a Module object (class caModule):
    • maps to a single code file containing classes which have been registered (if the Module's Library has been requested) and are therefore ready to invoke.
    • contains zero or more registered classes associated with that code file
    • uses the FileLoader trait

The FileLoader trait (tFileLoader) has these data access methods:

    protected function SetSlug(string $sName) { $this->sName = $sName; }
    public function GetSlug() : string { return $this->sName; }
    public function SetBasePath(string $fp) { $this->fpBase = $fp; }
    public function GetBasePath() : string
    protected function SetRelativeSpec(string $fsr) { $this->fsRel = $fsr; }
    protected function GetRelativeSpec() : string
    public function GetFileSpec() : string { return $this->GetBasePath().'/'.$this->GetRelativeSpec(); }

    private $isLoaded = FALSE;
    protected function SetAsLoaded() { $this->isLoaded = TRUE; }
    protected function GetIsLoaded() : bool { return $this->isLoaded; }

Process

  • When an application requests a Library (via csLoader::LoadLibrary()), the master index for that Library is executed (which may load other index files) -- but the code files are not loaded/executed yet.
  • When a class-autoload request comes in to csLoader::_LoadClass(), csLoader checks the class list (which it maintains) to see which code-file needs to finally be loaded, and loads/executes it.