Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:descriptor_autoloading [2019/06/04 01:20]
developer:descriptor_autoloading [2019/06/04 01:20] (current)
Line 1: Line 1:
 +====== Descriptor Auto-loading ======
  
 +Descriptor Auto-loading is a feature that let's your package end users load your packages with fewer lines or character of code with the simple class ''​method''​ call
 +
 +<code php>
 +$this->​load->​package("​vendor/​package_name"​);​
 +</​code>​
 +
 +All you have to do to make this possible is by using the ''​autoload''​ key in your package descriptor (''​splint.json''​) file. You can use this file to autoload the following resource types.\\
 +
 +  * Libraries (PHP Classes)
 +  * Models
 +  * Helpers &
 +  * Configs
 +
 +===== Auto-loading Libraries (Classes) =====
 +
 +==== No Constructor Parameter ====
 +
 +<code json>
 +{
 +  "​autoload":​ {
 +      "​libraries":​ [
 +        ["​ClassName",​ "​alias"​]
 +      ]
 +    }
 +}
 +</​code>​
 +
 +==== With Constructor Parameters ====
 +
 +<code json>
 +{
 +  "​autoload":​ {
 +    "​libraries:​ [
 +      ["​ClassName",​ {"​arg1"​ => "​val1",​ "​arg2"​ => "​val2"​},​ "​alias"​]
 +    ]
 +  }
 +}
 +</​code>​
 +
 +==== With Config File ====
 +
 +<code json>
 +{
 +  "​autoload":​ {
 +    "​libraries":​ [
 +      ["​ClassName",​ "​@config_file_name",​ "​alias"​]
 +    ]
 +  }
 +}
 +</​code>​
 +
 +The end user will have to create a config file located at ''​application/​config/​config_file_name.php''​ to contain the following.
 +
 +<code php>
 +<?php
 +defined('​BASEPATH'​) OR exit('​No direct script access allowed'​);​
 +
 +$config["​config_file_name"​] = [
 +  "​arg1"​ => "​value1",​
 +  "​arg2"​ => "​value2",​
 +  "​argN"​ => "​valueN"​
 +]
 +?>
 +</​code>​
 +
 +> Notice that the ''​key''​ of the array in the ''​$config''​ variable is the same as the name of the config file.
 +
 +> Splint will not throw an Exception if this file doesn'​t exist. You must check the argument array in your class constructor and verify use the ''​isset()''​ function to check for present values.
 +
 +===== Auto-loading Models =====
 +
 +<code json>
 +{
 +  "​autoload":​ {
 +    "​models":​ [
 +      ["​ModelClassName",​ "​alias"​]
 +    ]
 +  }
 +}
 +</​code>​
 +
 +===== Auto-loading Helpers =====
 +
 +<code json>
 +{
 +  "​autoload":​ {
 +    "​helpers":​ [
 +      ["​helper1",​ "​helper2",​ "​helperN"​]
 +    ]
 +  }
 +}
 +</​code>​
 +
 +===== Auto-loading Configs =====
 +
 +<code json>
 +{
 +  "​autoload":​ {
 +    "​configs":​ [
 +      ["​config1",​ "​config2",​ "​configN"​]
 +    ]
 +  }
 +}
 +</​code>​
 +
 +===== Conclusion =====
 +
 +With this, developers should indicate in their README files what they used as their ''​aliases'' ​ for the resources loaded. (If they used descriptor auto-loading). so end users can go ahead and do something like the one below.
 +
 +<code php>
 +// Quick Load Library.
 +$this->​load->​package("​splint/​platform"​);​
 +
 +// Use loaded resources (Library Class for Instance).
 +$this->​alias->​some_method();​
 +</​code>​
 +===== NEXT =====
 +
 +[[developer:​design_guides|Design Guides]] => \\
 +
 +===== PREVIOUS =====
 +
 +<= [[developer:​splint_json|Splint JSON Package Descriptor]]\\
 +
 +===== Other Topics =====
 +
 +[[library-install|Install a Splint Library]]\\
 +[[app_developer:​start|Application Development]]\\
 +[[load_splint|Load Splint Libraries]]\\