edit

Skytells Framework - Magic Loader

Overview

Many developers writing object-oriented applications create one PHP source file per class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).
When loading a Library or a Handler or even Model, the $this->load() method must be used to load the required file.

However, In SF, this is no longer necessary. The magicLoader method registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

How MagicLoader Works?

Lets say you wanna load a Library or a Model on your Controller for example
By then, You'll be forced to use the Regular Loader provided by the SF's Core Loader And Some Apps, Requires you to autoload the whole components using the Autoloader function which negativity effects your app's performance.
However, From now on you will no longer need this anymore.
With the MagicLoader method you can let Skytells Framework analyze your Code and load the required components ONLY WHEN NEEDED, So your performance will not be effected and assuring all used components are being loaded.

MagicLoader Benefits

When comparing the MagicLoader with the Regular Autoloader you'll found a lot of benefits to use MagicLoader rather than the regular loader.

  • Improving Your App's Performance
  • Loading The Required files only when needed
  • Saving Your time from identifying which lib must be loaded.
  • Loads your Libs (In Order) rather than unordered loads

How to use MagicLoader?

In order to use MagicLoader you need to just call $this->autoload() method on your Main Controller's Constructor
For Example, Say you have an App which contains a 3rd-party Libs & Models and you wanna load all the required files in order, So Add the following method like the giving example below:

CONTROLLER CLASS:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct();
      // Initing the MagicLoader
      $this->autoload(); // <---- As simple as that!
    }

  public function index() {
    // This is the default method for the controller.
    echo "Hello World";
  }
}

OR :
You can simply calling it using the Controller Events on Methods only.

Warning

The following example Injects the MagicLoader after the constructor function being executed, So when you use this method make sure to avoid loading your libs inside the constructor method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class MyController extends \Controller implements IController  {

  public function index() {
    // Injecting the autoload into the Base Core itself.
    $this->perform(function() {
      $this->autoload();
    });
  }
}

So by calling the MagicLoader method $this->autoload(); on your controller
The Skytells Framework will start perform magicLoader's functionality at the point you write the callable event in.

Warning

The magicloader's callable function must be written before calling your libs so it will let the Skytells Framework start detecting the above components

By Calling the $this->autoload(); method on your controller, The Skytells Framework will perform the spl_autoload_register() method for each component used by you.

MagicLoader Visibility

The magicLoader method is public method associated for Controllers only and Protected against HTTP direct access.

MagicLoader's Callable Method

The Callable method for MagicLoader is :

1
2
<?php
$this->autoload(); // <---- As simple as that!

MagicLoader's Backend

The Magic Loader's Backend API linked with Skytells\Core\Payload() Class
And it performs serve() method which performs spl_autoload_register() method for each component used by you.