Skip to content

Commit

Permalink
SimpleExtension, like Extension but with more Magic
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed May 11, 2009
1 parent 6a5b8bd commit 8dd3f8c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/extension.class.php
Expand Up @@ -6,6 +6,26 @@ interface Extension {
public function receive_event(Event $event);
}

/*
* BlahEvent -> onBlah
*/
abstract class SimpleExtension implements Extension {
var $theme;
var $_child;

public function i_am($child) {
$this->_child = $child;
if(is_null($this->theme)) $this->theme = get_theme_object($child, false);
}

public function receive_event(Event $event) {
$name = get_class($event);
$name = "on".str_replace("Event", "", $name);
if(method_exists($this->_child, $name)) {
$this->_child->$name($event);
}
}
}

/*
* Several extensions have this in common, make a common API
Expand Down
10 changes: 10 additions & 0 deletions index.php
Expand Up @@ -52,6 +52,16 @@
}


// initialise the extensions
foreach(get_declared_classes() as $class) {
if(is_subclass_of($class, "SimpleExtension")) {
$c = new $class();
$c->i_am($c);
add_event_listener($c);
}
}


// start the page generation waterfall
$page = new Page();
$user = _get_user($config, $database);
Expand Down

2 comments on commit 8dd3f8c

@Artanis
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize you've done it better--it hadn't occurred to me to call an onEvent function instead of just the event name--but at the very least give me an "inspired by" line. http://github.com/Artanis/simple-extension/tree/master

Cool this is core, now, though, caught me by surprise when I loaded it up after my recent pull and it said I can't re-declare SimpleExtension!

@Artanis
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above fixed in commit 4765e51

Please sign in to comment.