Aspen:Plugins

From Aspen Documentation

Aspen supports a basic plugin architecture that will allow you to create plugins that may be called at certain points within your application.

Plugins look like modules in file structure sense, as they both require a registry.xml file.

To create a plugin, you first need to create a plugins folder in the root installation directory.

Next, create a folder for your new plugin. For this demo, we'll use helloworld.

Inside plugins/helloworld, create a file called registry.xml.

This file begins with the same type of information that a module supports:

<?xml version="1.0"?>
 
<package>
 
	<type>Plugin</type>
	<guid>fd132110-1a87-11de-8c30-0800200c9a66</guid>
	<name>Hello World</name>
	<version>1.0</version>
 
	<add_hook_func hook="before_bootstrap_execute">
		<function>hello_world</function>
	</add_hook_func>
 
</package>

We tell Aspen that this package type is a plugin, give it a unique id, and we give it a name, version, etc.

All elements that are supported in the module registry file are also supported here, except for module prerequisites.

The add_hook_func element is what tells aspen when to execute this plugin.

  • When Aspen is loading, it will encounter the spot "hook" where any plugins associated with that hook will be called.
  • Aspen then load your plugin and calls your function at that precise moment.

To create a plugin hook within your own application, enter:

$this->APP->callPluginHook('before_bootstrap_execute');

When the container of the above is called, and this line executes, all plugin functions that have hook="before_bootstrap_execute" will be executed.

Next, create a file called helloworld.php.

We now need to create the actual function:

<?php
 
function hello_world() {
	print 'Hello World';
}
?>

As the title should explain, the before_bootstrap_execute plugin hook is in Aspen already, and calls any plugins that need to execute before the bootstrap even loads.

If we test all of this out, we'll see:

Hello World

print out at the very top of our app.