Skip to content

Instantly share code, notes, and snippets.

@croxton
Last active April 8, 2018 02:24
Show Gist options
  • Save croxton/4683017 to your computer and use it in GitHub Desktop.
Save croxton/4683017 to your computer and use it in GitHub Desktop.
ExpressionEngine extension plugins
<?php
// Using __call() in the main extension
/**
* Call magic method
*
* @param string $name The method name being called
* @param array $arguments The method call arguments
*/
public function __call($name, $arguments)
{
if (strpos($name, ':') !== FALSE)
{
$this->EE->load->library('stash_commander_lib');
// parse out the plugin class and method
$plugin = explode(':', $name);
$plugin_class = "stash_" . $plugin[0] . "_pi";
$plugin_method = $plugin[1];
// load and instantiate the plugin
$this->EE->stash_commander_lib->plugin($plugin_class);
// run it
// @TODO: check is callable?
return call_user_func_array(array(&$plugin_class, $plugin_method), $arguments);
}
}
// the plugin method in the Stash_commander_lib
/**
* load a "plugin"
*
* @access public
* @param string
* @return object
*/
public function plugin($plugin)
{
if( ! isset(self::$plugins[$plugin]))
{
// load and instantiate the plugin if it doesn't exist already
require_once PATH_THIRD. $this->mod_name . '/plugins/' . $plugin .EXT;
self::$plugins[$plugin] = new $plugin;
}
// check that we have an object that extends Stash_commander_plugin
if(self::$plugins[$plugin] instanceof Stash_commander_plugin)
{
return self::$plugins[$plugin];
}
else
{
throw new RuntimeException('Plugins must extend Stash_commander_plugin');
}
}
// you can use like this to install hooks for example:
$this->plugin('my_plugin')->install();
// the plugin abstract class - still working on this!
/**
* Stash_commander_plugin Class
*
* Abstract class for plugins
*
* @package Stash Commander
*/
abstract class Stash_commander_plugin {
public $EE, $name, $short_name, $version, $priority;
protected $hooks = array();
protected $ext_class_name;
protected $ext_version = STASH_COMMANDER_VERSION;
public function __construct()
{
$this->EE = get_instance();
$this->ext_class_name = STASH_COMMANDER_CLASS_NAME . '_ext';
$this->short_name = preg_filter('/^Stash_([a-zA-Z0-9_-]+)_pi$/i', '$1', get_class($this));
}
/**
* Activate plugin hooks
*
* @return void
*/
public function install()
{
foreach ($this->hooks AS $hook)
{
$this->_add_hook($hook);
}
}
/**
* Remove plugin hooks
*
* @return void
*/
public function uninstall()
{
foreach ($this->hooks AS $hook)
{
$this->_remove_hook($hook);
}
}
/**
* Add extension hook to the Stash Commander extension class
* Prefix the method with the plugin class short name, so we know how to find it
*
* @access protected
* @param string
* @return void
*/
protected function _add_hook($name)
{
$this->EE->db->insert('extensions',
array(
'class' => $this->ext_class_name,
'method' => $this->short_name . ":" . $name,
'hook' => $name,
'settings' => '',
'priority' => $this->priority,
'version' => $this->version,
'enabled' => 'y'
)
);
}
/**
* remove extension hook from the Stash Commander extension class
*
* @access protected
* @param string
* @return void
*/
protected function _remove_hook($name)
{
$this->EE->db->delete('extensions',
array(
'class' => $this->ext_class_name,
'hook' => $name
)
);
}
public function get_hooks() {
return $this->hooks;
}
abstract public function get_groups();
}
// an example plugin
/**
* Stash Commander cache-breaking plugin
*
* @package Stash Commander
* @author Mark Croxton
*/
class Stash_channel_entries_pi extends Stash_commander_plugin {
/**
* Name
*
* @var string
* @access public
*/
public $name = 'Channel Entries';
/**
* Version
*
* @var string
* @access public
*/
public $version = '1.0.0';
/**
* Extension hook priority
*
* @var integer
* @access public
*/
public $priority = '10';
/**
* Extension hooks
*
* @var array
* @access protected
*/
protected $hooks = array(
'entry_submission_end',
'delete_entries_end',
'update_multi_entries_loop',
'delete_entries_loop'
);
/**
* Get groups for this object
*
* @access public
* @return array
*/
public function get_groups()
{
return array();
}
/**
* Hook: entry_submission_end
*
* @access public
* @param array
* @return void
*/
public function entry_submission_end()
{
}
/**
* Hook: delete_entries_end
*
* @access public
* @param array
* @return void
*/
public function delete_entries_end()
{
}
/**
* Hook: update_multi_entries_loop
*
* @access public
* @param array
* @return void
*/
public function update_multi_entries_loop()
{
}
/**
* Hook: delete_entries_loop
*
* @access public
* @param array
* @return void
*/
public function delete_entries_loop()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment