# Extensions

The extension system of Wolff is simple and useful for some especial or specific tasks. What it does is loading every php file inside the extension folder (only if the page doesn't ends in a 404).

The extension folder should be in app/extensions.

An extension file must have the following format:

namespace Extension;

class {filename} {

    public $desc = array(
        'name' => 'The extension name',
        'description' => 'The extension description',
        'version' => 'The extension version',
        'author' => 'The extension author'
    );


    public function index() {
        //Code
    }

}

Keep in mind that the index method is called when the extension is loaded.

The extension filename must match the class name inside it and the file must be inside the extension folder (it's created automatically if it doesn't exists).

The extension class has access to the load, library, session and upload classes through $this.

Adding Extensions

The extension system works like the route system.

A file named Extensions.php located in the system/Definitions folder is used to define the extensions and the routes where they will work.

The file structure looks like this:

namespace System;

use Core\Extension;

/**
 * Use this file for declaring routes, blocks, redirections and more...
 */

Extension::before('*', 'example');

Adding an existing extension:

Extension::before('*', 'sayHi');

The first parameter is the working route, the second one is the extension name.

This will load the sayHi extension in every page before loading them, you can use the after method for calling that extension after the page is loaded.

Examples:

Extension::before('homepage', 'sayHi');
/* The extension will be executed only when the user access to
 * example.com/homepage.
 */

Extension::before('homepage/*', 'sayHi');
/* The extension will be executed only when the user access to any homepage sub route like
 * example.com/homepage/contact or example.com/homepage/user.
 */

Extension::before('homepage/{}', 'sayHi');
/* The extension will be executed only when the user access to the homepage route
 * followed by a get variable, like example.com/homepage/2, example.com/homepage/3 and others.
 */

If the route parameter is set to empty, the extension will be disabled (won't load in any page).

If the route parameter is set to '*', the extension will work in every page.

Example

A simple extension named SayHello that says Hello two times in every page.

The name of this file is sayHello.php and is inside the extension folder

namespace Extension;

class sayHello {

    public $desc = array(
        'name' => 'Hello',
        'description' => 'Say hello two times to everyone',
        'version' => '1.0.0',
        'author' => 'Usbac'
    );


    public function index() {
        for ($i = 0; $i < 2; $i++) {
            echo "</br> Hello";
        }
    }

}

Utilities

The extension class has some useful methods.

Get the extension folder path:

Extension::getFolder();

Create the extension folder if doesn't exists (it is done automatically anyway):

Extension::mkdir();

Extension details

Get all the extensions details:

Extension::get();

It will return an array with the name, description, version and author of every extension. If you create the sayHello.php extension that was shown above, it will return the following result:

Array
(
    [0] => Array
        (
            [filename] => sayHello
            [name] => Hello
            [description] => Say hello two times to everyone
            [version] => 1.0.0
            [author] => Usbac
        )

)

You can also pass an extension name as parameter and it will return only the information of the specified extension:

Extension::get('sayHello');