How to return data back from plugins?
Posted: Wed Mar 14, 2007 3:00 pm
Morning,
I'm creating a plugin/pluggable system for my already modular content management system to make it even more flexible, and customizable when released. It's working fine so far, loads the plugins correctly when called based upon events in the 'main' code. However, I'm having trouble wrapping my head around how to return the data from several plugins.
The way my plugin system works is it will call a single method to many different plugins that have mapped an event to a function/method of theres. The problem I have is that I store each plugin return data into an array, this is fine however if a plugin returns an array, it screws the final return up ... here's the code:
As you can see on the final return call I implode() the $plugin_data to 'flattern' all of the returned data from the different plugins that have been called. But, if one of the plugins returns an array, like I said above it messes the implode() function up and call_event() ends up just returning an empty array. So, what is the best way to deal with plugins that return an array? Am I doing this whole plugin bit wrong?
I'm creating a plugin/pluggable system for my already modular content management system to make it even more flexible, and customizable when released. It's working fine so far, loads the plugins correctly when called based upon events in the 'main' code. However, I'm having trouble wrapping my head around how to return the data from several plugins.
The way my plugin system works is it will call a single method to many different plugins that have mapped an event to a function/method of theres. The problem I have is that I store each plugin return data into an array, this is fine however if a plugin returns an array, it screws the final return up ... here's the code:
Code: Select all
//---
// call_event() is the main function used to call an event to each plugin
// that has mapped it's self to an event via it's plugin map.
// @param string $event
// [@param mixed $param]
// @return mixed
//---
public function call_event( $event, $param='' ) {
if ( empty( $this->plugins ) ) {
return '';
} else {
// Find all plugins for this event and store their data
$plugins = $this->find_plugins( $event );
$plugin_data = array();
foreach( $plugins as $key=>$val ) {
$plugin_class = $this->load_plugin( $val['name'] );
if ( is_object( $plugin_class ) ) {
$method_name = $val['map'][ $event ];
if ( method_exists( $plugin_class, $method_name ) ) {
$plugin_data[] = $plugin_class->$method_name();
} else {
Log::message( 'Plugin "'.$val['name'].'" event could not be loaded. Requested event method "'.$method_name.'" does not exist in the class.', Log::L_WARNING );
}
}
}
return implode( '', $plugin_data );
}
}