Mecha CMS

Mecha CMS blog and documentation.

Accessing the Plugin Configuration Data

Updated: Sunday, 07 August 2016

How to get the stored configuration data of your plugin?

Table of Content

Manual

Using the File API, you can obtain the configuration data that would normally be stored in the states folder in your plugins folder this way:

// Extract `.\www\lot\plugins\the-plugin\states\config.txt` as array data
$path = PLUGIN . DS . 'the-plugin' . DS . 'states' . DS . 'config.txt';
$c = File::open($path)->unserialize();

if(isset($c['theme'])) {
    echo $c['theme'];
}

Or use the safe way, with fallback value if file does not exist:

$fallback = array('theme' => 'light');
$c = File::open($path)->unserialize($fallback);

echo $c['theme'];

If you try to retrieve the configuration data from the root of your plugin folder (such as in the launch.php and configurator.php file):

$path = __DIR__ . DS . 'states' . DS . 'config.txt';
$c = File::open($path)->unserialize();

Automatic

Configuration data will also be automatically inserted into the $config->states as an object which you can get by calling a key with pattern plugin_X where X represent the MD5 hash version of the plugin folder name:

$c = $config->states->{'plugin_' . md5('the-plugin')};

if(isset($c->theme)) {
    echo $c->theme;
}

Or use the safe way, with fallback value if key does not exist:

$fallback = (object) array('theme' => 'light');
$c = Config::get('states.plugin_' . md5('the-plugin'), $fallback);

echo $c->theme;

If you try to retrieve the configuration data from the root of your plugin folder (such as in the launch.php and configurator.php file):

$c = $config->states->{'plugin_' . md5(File::B(__DIR__))};
Donation and Email Subscription