Ferreteria/v0.5/classloader: Difference between revisions

From Woozle Writes Code
Jump to navigation Jump to search
(Created page with "The ClassLoader handles loading of all other classes in Ferreteria. It is (of necessity) written so it can be used without any other part of Ferreteria. ==Class Structure== *...")
 
m (3 revisions imported: moving this project here)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
The ClassLoader handles loading of all other classes in Ferreteria. It is (of necessity) written so it can be used without any other part of Ferreteria.
The ClassLoader ({{l/ferreteria/code|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==
==Class Structure==
* the '''csLoader''' class is just to encapsulate the top-level module/class registry and autoloading
* the '''Loader''' (class <code>caLoader</code>) is just to encapsulate the top-level module/class registry and autoloading.
* a '''caLibrary''' object:
* a '''Library''' object (class <code>caLibrary</code>):
** is a named set of Modules which are only loaded by explicit request from the application
** 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
** has a BasePath, to which all Module files are relative; defaults to csLoader's BasePath
** uses the '''FileLoader''' trait (<code>tFileLoader</code>)
** invokes a specific index file which loads the rest of the Library via its Module object
** 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()
*** ...which may itself invoke other index files via caModule->IncludeIndex()
*** ...the format of which is defined by cModule descendants
*** ...the format of which is defined by cModule descendants
** The index file is only processed when its Library is requested
** The index file (example: {{l/ferreteria/code|dropins/basics/index.dropin.php}}) is only processed when its Library is requested
* a '''caModule''' object:
* a '''Module''' object (class <code>caModule</code>):
** contains zero or more registered classes
** 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.
** maps to a single directly-executable code file
** contains zero or more registered classes associated with that code file
** uses the '''FileLoader''' trait
 
The '''FileLoader''' trait (<code>tFileLoader</code>) has these data access methods:
<syntaxhighlight lang=php>
    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; }
</syntaxhighlight>   
==Process==     
==Process==     
* When an application requests a Library (via <code>csLoader::LoadLibrary()</code>), the master index for that Library is executed (which may load other index files) -- but the code files are not loaded/executed yet.
* When an application requests a Library (via <code>csLoader::LoadLibrary()</code>), 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 <code>csLoader::_LoadClass()</code>, csLoader checks the class list (which it maintains) to see which code-file needs to finally be loaded, and loads/executes it.
* When a class-autoload request comes in to <code>csLoader::_LoadClass()</code>, csLoader checks the class list (which it maintains) to see which code-file needs to finally be loaded, and loads/executes it.

Latest revision as of 16:44, 22 May 2022

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.