Skip to content

Commit

Permalink
Updated to 1.0.4 version
Browse files Browse the repository at this point in the history
  • Loading branch information
josantonius committed Jun 2, 2017
1 parent dd02afb commit 1bccac1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 200 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# CHANGELOG

## 1.0.4 - 2017-06-02

Return was added in the doAction method. Useful for receiving actions that are only executed once.

* Deleted `$_hooks` property.

* Deleted `Josantonius\Hook\Hook::setHook()` method.
* Deleted `Josantonius\Hook\Hook::addHook()` method.
* Deleted `Josantonius\Hook\Hook::resetHook()` method.
* Deleted `Josantonius\Hook\Hook::run()` method.
* Deleted `Josantonius\Hook\Hook::collectHook()` method.

## 1.0.3 - 2017-05-31

These deprecated methods will be removed as of version 1.0.4:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "josantonius/hook",
"version": "1.0.3",
"version": "1.0.4",
"type": "library",
"description": "Library for handling hooks.",
"keywords": [
Expand Down
204 changes: 5 additions & 199 deletions src/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,6 @@
*/
class Hook {

/**
* Available hooks.
*
* @since 1.0.0
*
* @deprecated 1.0.3
*
* @var array
*/
private static $_hooks = [
'meta',
'css',
'after-body',
'footer',
'js',
'launch',
'routes'
];

/**
* Callbacks.
*
Expand Down Expand Up @@ -115,73 +96,6 @@ public static function setSingletonName($method) {
self::$singleton = $method;
}

/**
* Add hook/hooks to hook list.
*
* @since 1.0.0
*
* @deprecated 1.0.3 This method will be removed in the next version
*
* @param string|array $where → hook to add
*
* @return int → number hooks added
*/
public static function setHook($where) {

if (!is_array($where)) {

self::$_hooks[$where] = '';

return 1;
}

foreach ($where as $where) {

self::$_hooks[$where] = '';
}

return count($where);
}

/**
* Attach custom function to hook.
*
* @since 1.0.0
*
* @deprecated 1.0.3 This method will be replaced by addAction
* and removed in the next version
*
* @param array|string $where → hook to use
* @param string $function → function to attach to hook
*
* @throws HookException → hook location not defined
* @return boolean → success with adding
*/
public static function addHook($where, $function = '') {

if (!is_array($where)) {

$where = [$where => $function];
}

foreach ($where as $hook => $function) {

if (!isset(self::$_hooks[$hook])) {

$message = 'Hook location not defined';

throw new HookException($message . ': ' . $hook, 811);
}

$theseHooks = explode('|', self::$_hooks[$hook]);
$theseHooks[] = $function;

self::$_hooks[$hook] = implode('|', $theseHooks);
}

return true;
}

/**
* Attach custom function to action hook.
*
Expand Down Expand Up @@ -224,97 +138,6 @@ public static function addActions($actions) {
return true;
}

/**
* Reset custom function to hook.
*
* @since 1.0.2
*
* @deprecated 1.0.3 This method will be removed in the next version
*
* @param array|string $where → hook to remove
*
* @return boolean
*/
public static function resetHook($where) {

if (isset(self::$_hooks[$where])) {

self::$_hooks[$where] = '';

return true;
}

return false;
}

/**
* Run all hooks attached to the hook.
*
* By default it will look for getInstance method to use singleton
* pattern and create a single instance of the class. If it does not
* exist it will create a new object.
*
* @see setSingletonName() for change the method name.
*
* @since 1.0.0
*
* @deprecated 1.0.3 This method will be replaced by doAction
* and removed in the next version
*
* @param string $where → hook to run
* @param string $args → optional arguments
*
* @throws HookException → the hook is not yet known
* @return object|false → returns the calling function
*/
public static function run($where, $args='') {

if (!isset(self::$_hooks[$where])) {

$message = 'Hook location not defined';

throw new HookException($message . ': ' . $where, 811);
}

$theseHooks = explode('|', self::$_hooks[$where]);

foreach ($theseHooks as $hook) {

if (preg_match("/@/i", $hook)) {

$parts = explode('/', $hook);

$last = end($parts);

$segments = explode('@', $last);

$class = $segments[0];

$method = $segments[1];

if (method_exists($class, self::$singleton)) {

$instance = call_user_func([$class, self::$singleton]);

call_user_func([$instance, $method], $args);

continue;
}

$instance = new $class;

call_user_func([$instance, $method], $args);

} else {

if (function_exists($hook)) {

call_user_func($hook, $result);
}
}
}
}

/**
* Run all hooks attached to the hook.
*
Expand All @@ -329,6 +152,8 @@ public static function run($where, $args='') {
* @param string $tag → action hook name
* @param mixed $args → optional arguments
* @param boolean $remove → delete hook after executing actions
*
* @return returns the output of the last action or false
*/
public static function doAction($tag, $args = [], $remove = true) {

Expand All @@ -351,11 +176,13 @@ public static function doAction($tag, $args = [], $remove = true) {

foreach ($priority as $action) {

self::_runAction($action, $args);
$action = self::_runAction($action, $args);
}
}

self::$current = false;

return (isset($action)) ? $action : false;
}

/**
Expand Down Expand Up @@ -469,25 +296,4 @@ public static function current() {

return self::$current;
}

/**
* Execute hooks attached to run and collect instead of running.
*
* @since 1.0.0
*
* @deprecated 1.0.3 This method will be removed in the next version
*
* @param string $where → hook
* @param string $args → optional arguments
*
* @return object → returns output of hook call
*/
public function collectHook($where, $args = null) {

ob_start();

echo $this->run($where, $args);

return ob_get_clean();
}
}

0 comments on commit 1bccac1

Please sign in to comment.