Skip to content

Commit

Permalink
Adds on, off, and dispatchEvent methods
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Aug 30, 2016
1 parent 74b5522 commit 7a7c2ca
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion app/source/classes/Postleaf.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Postleaf {
// Properties
////////////////////////////////////////////////////////////////////////////////////////////////

protected static $database, $language, $settings;
protected static $database, $language, $listeners, $settings;

////////////////////////////////////////////////////////////////////////////////////////////////
// Initialization method
Expand Down Expand Up @@ -70,6 +70,21 @@ public static function run() {
// Public methods
////////////////////////////////////////////////////////////////////////////////////////////////

// Dispatches an event
public static function dispatchEvent($event, $data = null) {
// Loop through all listeners
foreach((array) self::$listeners as $listener) {
// Look for a matching event
if($listener['event'] === $event) {
if(is_callable($listener['callback'])) {
$result = $listener['callback']($data);
// Prevent future events if the callback returns false
if($result === false) break;
}
}
}
}

// Returns the file extension of $filename (lowercase, without a dot)
public static function fileExtension($filename) {
return mb_strtolower(pathinfo($filename)['extension']);
Expand Down Expand Up @@ -201,6 +216,51 @@ public static function numberFormat($number, $dec_places = 0) {
);
}

// Removes one or more event listeners
//
// By event: Postleaf::off('post.save')
// By namespace: Postleaf::off('/namespace')
// By both: Postleaf::off('post.save/namespace')
//
public static function off($event) {
// Separate namespace from event
$options = explode('/', $event, 2);
$event = $options[0] ?: null;
$namespace = $options[1] ?: null;

// Loop through listeners
foreach(self::$listeners as $key => $value) {
if($event && $namespace) {
$off = $event === $value['event'] && $namespace === $value['namespace'];
} elseif($namespace) {
$off = $namespace === $value['namespace'];
} elseif($event) {
$off = $event === $value['event'];
}

if($off) {
unset(self::$listeners[$key]);
}
}
}

// Adds an event listener
//
// No namespace: Postleaf::on('post.save', $callback)
// With namespace: Postleaf::on('post.save/namespace', $callback)
//
public static function on($event, $callback) {
// Separate namespace from event
$options = explode('/', $event, 2);

// Attach the listener
self::$listeners[] = [
'event' => $options[0],
'namespace' => $options[1] ?: null,
'callback' => $callback
];
}

// Generates an array of pagination data
public static function paginate($total_items, $items_per_page = 10, $current_page = 1) {
// Items per page must be at least one
Expand Down

0 comments on commit 7a7c2ca

Please sign in to comment.