Chapter 10. Plugins

Table of Contents

Using plugins
Installing a plugin
Activating and configuring a plugin
Developing plugins
The plugin file
Plugin custom configuration
Using a class

Plugins are a simple way to extend Atomik. As said before, the framework doesn't come with a lot of features. However it provides a nice and powerful plugin system.

Using plugins

Installing a plugin

Plugins are stored in the app/plugins directory. Simply copy the plugin file into this directory.

Activating and configuring a plugin

Plugins are not automatically activated. To do so, it's needed to add an entry in the “plugins” configuration key.

Example 10.1. Activating a plugin

					
Atomik::set('plugins', array(
	'db',
	'layout'
));
				

Some plugins need custom configuration which can be specified in the plugins config key.

Example 10.2. Plugin custom configuration

					
Atomik::set('plugins', array(
	'db' => array(
           'dsn'         => 'mysql:host=localhost;dbname=atomik',
           'username'    => 'atomik',
           'password'    => 'atomik'
	),
	'layout'
));
				

Developing plugins

The plugin file

A plugin is made of one file named the same way. For example the db plugin is in the file db.php.

Plugins are loaded at the beginning of a request, just after the configuration has been loaded. The content of the file is free or it can be a class.

Plugin custom configuration

As said in the "Using plugins" section, plugins can have custom configuration. To retrieve this configuration a $config variable is automatically available. It contains the array used in the configuration.

Example 10.3. Retrieving plugin custom configuration

In the configuration file:

					
Atomik::set('plugins', array(
	'myPlugin' => array(
           'name' => 'Peter'
	)
));
				

In the plugin file:

					
echo 'hello ' . $config['name'];
				

Using a class

For better application design it is advise to use a class to define your plugin. When loading a plugin, it will look for a class named like the plugin suffixed with “Plugin”.

If this class has a static start method, it will be called, with the plugin custom configuration as argument, when the plugin is loaded.

Example 10.4. Plugin class

The plugin class for a plugin named db

					
class DbPlugin
{
	public static function start($config)
	{
		// $config['name'] == 'Peter'
	}
}
				

The class can contain static methods that will be automatically registered as callback on events. These methods have to start by "on" followed by the event name without the double ":".

Example 10.5. Plugin class with event callback methods

					
class DbPlugin
{
	public static onAtomikDispatchStart()
	{
		// listener for Atomik::Dispatch::Start
	}
}
				

You can prevent automatic callback registration by returning false in the start method.