remove regular Extension, have SimpleExtension take its place, optimise the event system based on SimpleExtension methods

This commit is contained in:
Shish
2012-02-08 12:07:01 +00:00
parent 2b85e2d2fd
commit 0698b780f9
69 changed files with 79 additions and 116 deletions

View File

@ -13,20 +13,14 @@
* $formatted_text = $tfe->formatted;
* \endcode
*
* An extension is something which is capable of reacting to events. They
* register themselves using the add_event_listener() function, after which
* events will be sent to the object's recieve_event() function.
*
* SimpleExtension subclasses are slightly different -- they are registered
* automatically, and events are sent to a named method, eg PageRequestEvent
* will be sent to onPageRequest()
* An extension is something which is capable of reacting to events.
*
*
* \page hello The Hello World Extension
*
* \code
* // ext/hello/main.php
* public class Hello extends SimpleExtension {
* public class Hello extends Extension {
* public void onPageRequest(PageRequestEvent $event) {
* global $page, $user;
* $this->theme->display_hello($page, $user);
@ -62,33 +56,17 @@
*
*/
/**
* A generic extension class, for subclassing
*/
interface Extension {
public function receive_event(Event $event);
public function get_priority();
}
/**
* send_event(BlahEvent()) -> onBlah($event)
*
* Also loads the theme object into $this->theme if available
*
* index.php will load all SimpleExtension subclasses,
* so no need for register_extension(new Foo())
*
* Automatic registration is done with priority returned by get_priority()
*
* Hopefully this removes as much copy & paste code from the extension
* files as possible~
*
* The original concept came from Artanis's SimpleExtension extension
* The original concept came from Artanis's Extension extension
* --> http://github.com/Artanis/simple-extension/tree/master
* Then re-implemented by Shish after he broke the forum and couldn't
* find the thread where the original was posted >_<
*/
abstract class SimpleExtension implements Extension {
abstract class Extension {
var $theme;
var $_child;
@ -100,15 +78,6 @@ abstract class SimpleExtension implements Extension {
if(is_null($this->theme)) $this->theme = get_theme_object($child, false);
}
public function receive_event(Event $event) {
$name = get_class($event);
// this is rather clever..
$name = "on".str_replace("Event", "", $name);
if(method_exists($this->_child, $name)) {
$this->_child->$name($event);
}
}
public function get_priority() {
return 50;
}
@ -117,7 +86,7 @@ abstract class SimpleExtension implements Extension {
/**
* Several extensions have this in common, make a common API
*/
abstract class FormatterExtension extends SimpleExtension {
abstract class FormatterExtension extends Extension {
public function onTextFormatting(TextFormattingEvent $event) {
$event->formatted = $this->format($event->formatted);
$event->stripped = $this->strip($event->stripped);
@ -131,7 +100,7 @@ abstract class FormatterExtension extends SimpleExtension {
* This too is a common class of extension with many methods in common,
* so we have a base class to extend from
*/
abstract class DataHandlerExtension extends SimpleExtension {
abstract class DataHandlerExtension extends Extension {
public function onDataUpload(DataUploadEvent $event) {
if($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) {
if(!move_upload_to_archive($event)) return;

View File

@ -824,6 +824,7 @@ $_event_count = 0;
function send_event(Event $event) {
global $_event_listeners, $_event_count;
if(!isset($_event_listeners[get_class($event)])) return;
$method_name = "on".str_replace("Event", "", get_class($event));
ctx_log_start(get_class($event));
// SHIT: http://bugs.php.net/bug.php?id=35106
@ -831,7 +832,7 @@ function send_event(Event $event) {
ksort($my_event_listeners);
foreach($my_event_listeners as $listener) {
ctx_log_start(get_class($listener));
$listener->receive_event($event);
$listener->$method_name($event);
ctx_log_endok();
}
$_event_count++;
@ -1001,7 +1002,7 @@ function _load_extensions() {
if($rclass->isAbstract()) {
// don't do anything
}
elseif(is_subclass_of($class, "SimpleExtension")) {
elseif(is_subclass_of($class, "Extension")) {
$c = new $class();
$c->i_am($c);
$my_events = array();
@ -1012,10 +1013,6 @@ function _load_extensions() {
}
add_event_listener($c, $c->get_priority(), $my_events);
}
elseif(is_subclass_of($class, "Extension")) {
$c = new $class();
add_event_listener($c, $c->get_priority(), $all_events);
}
}
if(COMPILE_ELS) {
@ -1024,13 +1021,10 @@ function _load_extensions() {
foreach(get_declared_classes() as $class) {
$rclass = new ReflectionClass($class);
if($rclass->isAbstract()) {}
elseif(is_subclass_of($class, "SimpleExtension")) {
elseif(is_subclass_of($class, "Extension")) {
$p .= "\$$class = new $class(); ";
$p .= "\${$class}->i_am(\$$class);\n";
}
elseif(is_subclass_of($class, "Extension")) {
$p .= "\$$class = new $class();\n";
}
}
$p .= "\$_event_listeners = array(\n";