Mecha CMS

Learn.

Extension

Table of Contents
  1. Structure 
  2. State 
  3. Check if Extension Exists 

Add extra functionality to the core engine.

An extension don’t have to change the behavior of your site directly. It can also be used as an API enhancer that does nothing until you use it on a particular layout, or in your personal function file.

Structure 

An extension is a folder with a unique name, stored in .\lot\x folder:

.\
└── lot\
    └── x\
        ├── art\
        ├── asset\
        ├── extension-1\
        ├── extension-2\
        ├── …
        └── …

A standard extension should at least contains files that structured like this:

.\lot\x\extension-1\
├── engine\
│   ├── kernel\
│   │   └── extension-1.php
│   ├── plug\
│   │   └── extension-1.php
│   └── fire.php
├── lot\
│   └── asset\
│       ├── .htaccess
│       ├── css\
│       └── js\
├── about.page
├── index.php
└── state.php

But the simplest one can also be achieved as:

.\lot\x\extension-1\
└── index.php

Note: The .htaccess file is required to enable access of the asset files publicly. It only contains the following command:

allow from all

The most important part is the index.php file. The automatic extension loader will seek for index.php file and once it found, then the engine will include it before everything else is being loaded.

State 

The state.php file contains custom settings for your users. It will be loaded automatically along with the index.php file loading. The state values will be loaded to the x property in your global $state variable:

// Get `page` extension’s state as object
$page_state = $state->x->page;

// Get `page` extension’s state as array
$page_state = state('x.page');

If you are affraid that your extension state data will overload the memory usage of your site environment due to the automatic loading process, you can try to store the state data to another file and then load it manually:

.\lot\x\extension-1\
├── state\
│   └── foo-bar.php
└── index.php
if ($should_load_the_state) {
    $the_state = require __DIR__ . DS . 'state' . DS . 'foo-bar.php';
} else {
    $the_state = [];
}

Check if Extension Exists 

There is no special mechanism to detect if an extension exists. The easiest way would be to detect the state value of certain extension that is stored in the global $state variable. As of the automatic loading proccess that I have explained before, then you can confirm the existence of an extension by checking the state value of the desired extension. If it at least return an empty array, then it means that the extension does exist even if you don’t have any state.php file in your extension folder.

if (null !== state('x.page')) {
    // Has `page` extension
}

Or even simpler:

if (is_file(LOT . DS . 'x' . DS . 'page' . DS . 'index.php')) {
    // Has `page` extension
}

0 Comments

No comments yet.