Using plugins in OPL
From Invenzzia wiki
OPL core provides a common plugin architecture to the other OPL projects. In this short article we will explain, how to use it.
Contents |
Why plugins?
OPL projects have an autoloader and at first glance it seems that installing an extra stuff requires putting its file in a proper directory. Obviously, this scenario is possible and it would work, but sometimes, before the library starts to see the new stuff, you must register it. Otherwise, we would like to have a single directory for all the extensions. Plugins solve both of the problems - they allow to register new items automatically if necessary and store the extensions in a single place.
Using plugins
Let's take a look at Open Power Template which currently is the only library that uses the plugin architecture. The necessary methods can be found in the main class, Opt_Class. We have three configuration options there:
- pluginDir - the path to the plugins.
- pluginDataDir - the path where to store internal OPL files.
- pluginAutoload - if set to **false**, the plugin autoloading is disabled. Use it in the production environment to improve the performance.
pluginDir may be either a single path or an array of paths. In the second case, OPL scans them all for plugins. In order not to preform a time-consuming directory scanning every HTTP request, the information about the plugins is saved into a single file in pluginDataDir directory. Thus, you must provide a write access to this directory for PHP.
If you are using OPT, you can point pluginDataDir to the same directory, as compileDir in order not to increase the number of the directories with the write access. You do not have to worry about the naming collisions. The OPT compiler and the plugin system use different naming formats.
Once the paths are set properly, you can call Opt_Class::loadPlugins() method. Please note that in this certain case it is called automatically by the setup() method, if we set the pluginDir option. So, the sequence of steps looks like this:
$tpl = new Opt_Class;
// Configuring OPT here
$tpl->pluginDir = './plugins/Opt/';
$tpl->pluginDataDir = './templates_c/';
$tpl->pluginAutoload = true; // we are in the development environment
$tpl->loadPlugins();
// other tasks
$tpl->setup();
Now you must only put some plug-ins into ./plugins/Opt/'.
Writing plugins
The exact plugin structure depends on the library, as different projects may have special needs. The necessary information can be found in the library User Manual and in the following wiki pages:
Please note that not all the extensions require the plugin architecture. For example, the framework ports do not require an environment from OPT or OPL, but rather create such environment for them within the framework.
Conclusion
OPL provides a simple and powerful plugin system that does not require much effort from the programmer. We encourage to use it in your project and to publish your assets on this wiki.

