Skip to content

Commit

Permalink
Release 1.1.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
hperrin committed Feb 19, 2015
1 parent 9b2fbd7 commit 6899180
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 40 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ bower install https://github.com/sciactive/hookphp.git

## Getting Started

If you don't use an autoloader, all you need to do is include the H.php file.
If you don't use an autoloader, all you need to do is include the Hook.php file.

```php
require("H.php");
require("Hook.php");
```

Now you can start setting up objects for method hooking.
Expand All @@ -31,13 +31,13 @@ class Test {
}
}
$obj = new Test;
\SciActive\H::hookObject($obj, 'Test->');
\SciActive\Hook::hookObject($obj, 'Test->');
```

And modifying their method calls.

```php
\SciActive\H::addCallback('Test->testFunction', -2, function(&$arguments, $name, &$object, &$function, &$data){
\SciActive\Hook::addCallback('Test->testFunction', -2, function(&$arguments, $name, &$object, &$function, &$data){
$arguments[0] = 'New argument instead.';
});
```
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hookphp",
"version": "1.0.0",
"main": "src/H.php",
"version": "1.1.0",
"main": "src/Hook.php",
"ignore": [
"test*",
"composer.json"
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "sciactive/hookphp",
"description": "Method hooking in PHP.",
"version": "1.0.0",
"version": "1.1.0",
"type": "library",
"keywords": [
"method hooking",
"hook",
"interception"
],
"homepage": "http://hookphp.org/",
"time": "2014-12-15",
"time": "2015-02-19",
"license": "LGPL",
"authors": [
{
Expand Down
46 changes: 23 additions & 23 deletions src/H.php → src/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Hooks are used to call a callback when a method is called and optionally
* manipulate the arguments/function call/return value.
*
* @version 1.0.0
* @version 1.1.0
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand All @@ -18,7 +18,7 @@
include_once(__DIR__.DIRECTORY_SEPARATOR.'HookOverride.php');
}

class H {
class Hook {
/**
* An array of the callbacks for each hook.
* @var array
Expand Down Expand Up @@ -80,16 +80,16 @@ class H {
* @param int $order The order can be negative, which will run before the method, or positive, which will run after the method. It cannot be zero.
* @param callback The callback.
* @return array An array containing the IDs of the new callback and all matching callbacks.
* @uses \SciActive\H::sortCallbacks() To resort the callback array in the correct order.
* @uses \SciActive\Hook::sortCallbacks() To resort the callback array in the correct order.
*/
public static function addCallback($hook, $order, $function) {
$callback = array($order, $function);
if (!isset(H::$hooks[$hook])) {
H::$hooks[$hook] = array();
if (!isset(Hook::$hooks[$hook])) {
Hook::$hooks[$hook] = array();
}
H::$hooks[$hook][] = $callback;
uasort(H::$hooks[$hook], '\\SciActive\\H::sortCallbacks');
return array_keys(H::$hooks[$hook], $callback);
Hook::$hooks[$hook][] = $callback;
uasort(Hook::$hooks[$hook], '\\SciActive\\Hook::sortCallbacks');
return array_keys(Hook::$hooks[$hook], $callback);
}

/**
Expand All @@ -100,10 +100,10 @@ public static function addCallback($hook, $order, $function) {
* @return int 1 if the callback was deleted, 2 if it didn't exist.
*/
public static function delCallbackByID($hook, $id) {
if (!isset(H::$hooks[$hook][$id])) {
if (!isset(Hook::$hooks[$hook][$id])) {
return 2;
}
unset(H::$hooks[$hook][$id]);
unset(Hook::$hooks[$hook][$id]);
return 1;
}

Expand All @@ -117,7 +117,7 @@ public static function delCallbackByID($hook, $id) {
* @return array An array of callbacks.
*/
public static function getCallbacks() {
return H::$hooks;
return Hook::$hooks;
}

/**
Expand All @@ -141,14 +141,14 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
// recursively calling ourself. Some system classes shouldn't be hooked.
$className = str_replace('\\', '_', $isString ? $object : get_class($object));
global $_;
if (isset($_) && in_array($className, array('\\SciActive\\H', 'depend', 'config', 'info'))) {
if (isset($_) && in_array($className, array('\\SciActive\\Hook', 'depend', 'config', 'info'))) {
return false;
}

if ($recursive && !$isString) {
foreach ($object as $curName => &$curProperty) {
if ((object) $curProperty === $curProperty) {
H::hookObject($curProperty, $prefix.$curName.'->');
Hook::hookObject($curProperty, $prefix.$curName.'->');
}
}
}
Expand Down Expand Up @@ -203,21 +203,21 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
//."\t}\n"
."\t\$function = array(\$this->_hook_object, '$fname');\n"
."\t\$data = array();\n"
."\t\\SciActive\\H::runCallbacks(\$this->_hook_prefix.'$fname', \$arguments, 'before', \$this->_hook_object, \$function, \$data);\n"
."\t\\SciActive\\Hook::runCallbacks(\$this->_hook_prefix.'$fname', \$arguments, 'before', \$this->_hook_object, \$function, \$data);\n"
."\tif (\$arguments !== false) {\n"
."\t\t\$return = call_user_func_array(\$function, \$arguments);\n"
."\t\tif ((object) \$return === \$return && get_class(\$return) === '$className')\n"
."\t\t\t\\SciActive\\H::hookObject(\$return, '$prefix', false);\n"
."\t\t\t\\SciActive\\Hook::hookObject(\$return, '$prefix', false);\n"
."\t\t\$return = array(\$return);\n"
."\t\t\\SciActive\\H::runCallbacks(\$this->_hook_prefix.'$fname', \$return, 'after', \$this->_hook_object, \$function, \$data);\n"
."\t\t\\SciActive\\Hook::runCallbacks(\$this->_hook_prefix.'$fname', \$return, 'after', \$this->_hook_object, \$function, \$data);\n"
."\t\tif ((array) \$return === \$return)\n"
."\t\t\treturn \$return[0];\n"
."\t}\n"
."}\n\n";
}
unset($curMethod);
// Build a HookOverride class.
$include = str_replace(array('_NAMEHERE_', '//#CODEHERE#', '<?php', '?>'), array($className, $code, '', ''), H::$hookFile);
$include = str_replace(array('_NAMEHERE_', '//#CODEHERE#', '<?php', '?>'), array($className, $code, '', ''), Hook::$hookFile);
eval ($include);
}

Expand All @@ -240,8 +240,8 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
* @param array &$data A data array for callback communication.
*/
public static function runCallbacks($name, &$arguments = array(), $type = 'all', &$object = null, &$function = null, &$data = array()) {
if (isset(H::$hooks['all'])) {
foreach (H::$hooks['all'] as $curCallback) {
if (isset(Hook::$hooks['all'])) {
foreach (Hook::$hooks['all'] as $curCallback) {
if (($type == 'all' && $curCallback[0] != 0) || ($type == 'before' && $curCallback[0] < 0) || ($type == 'after' && $curCallback[0] > 0)) {
call_user_func_array($curCallback[1], array(&$arguments, $name, &$object, &$function, &$data));
if ($arguments === false) {
Expand All @@ -250,8 +250,8 @@ public static function runCallbacks($name, &$arguments = array(), $type = 'all',
}
}
}
if (isset(H::$hooks[$name])) {
foreach (H::$hooks[$name] as $curCallback) {
if (isset(Hook::$hooks[$name])) {
foreach (Hook::$hooks[$name] as $curCallback) {
if (($type == 'all' && $curCallback[0] != 0) || ($type == 'before' && $curCallback[0] < 0) || ($type == 'after' && $curCallback[0] > 0)) {
call_user_func_array($curCallback[1], array(&$arguments, $name, &$object, &$function, &$data));
if ($arguments === false) {
Expand Down Expand Up @@ -281,8 +281,8 @@ private static function sortCallbacks($a, $b) {
}

public static function getHookFile() {
H::$hookFile = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'HookOverride_extend.php');
Hook::$hookFile = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'HookOverride_extend.php');
}
}

H::getHookFile();
Hook::getHookFile();
2 changes: 1 addition & 1 deletion src/HookOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Dynamic HookOverride class.
*
* @version 1.0.0
* @version 1.1.0
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand Down
4 changes: 2 additions & 2 deletions src/HookOverride_extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Dynamic HookOverride class.
*
* @version 1.0.0
* @version 1.1.0
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand Down Expand Up @@ -73,7 +73,7 @@ public function __set_state() {
public function __clone() {
// TODO: Test this. Make sure cloning works properly.
$newObject = clone $this->_hook_object;
H::hookObject($newObject, get_class($newObject).'->', false);
Hook::hookObject($newObject, get_class($newObject).'->', false);
return $newObject;
}

Expand Down
12 changes: 6 additions & 6 deletions test.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

error_reporting(E_ALL);
require 'src/H.php';
use SciActive\H as H;
require 'src/Hook.php';
use SciActive\Hook as Hook;

echo "Testing...<br>\n";

Expand All @@ -20,8 +20,8 @@ public function testFunctionReal($argument) {
}

$obj = new Test;
H::hookObject($obj, 'Test->');
H::addCallback('Test->testFunction', -2, function(&$arguments, $name, &$object, &$function, &$data){
Hook::hookObject($obj, 'Test->');
Hook::addCallback('Test->testFunction', -2, function(&$arguments, $name, &$object, &$function, &$data){
$arguments[0] = 'Still Failure!';
if ($object->test !== 'right') {
echo "Object check failed.<br>\n";
Expand All @@ -34,12 +34,12 @@ public function testFunctionReal($argument) {
echo "Name check passed.<br>\n";
}
});
H::addCallback('Test->testFunction', -1, function(&$arguments, $name, &$object, &$function, &$data){
Hook::addCallback('Test->testFunction', -1, function(&$arguments, $name, &$object, &$function, &$data){
$arguments[0] = 'Success!';
$data['test'] = 1;
$function = array($object, 'testFunctionReal');
});
H::addCallback('Test->testFunction', 1, function(&$return, $name, &$object, &$function, &$data){
Hook::addCallback('Test->testFunction', 1, function(&$return, $name, &$object, &$function, &$data){
if ($data['test'] !== 1) {
echo "Data check failed.<br>\n";
} else {
Expand Down

0 comments on commit 6899180

Please sign in to comment.