Skip to content

Commit

Permalink
Update to get HookPHP working in HHVM.
Browse files Browse the repository at this point in the history
  • Loading branch information
hperrin committed Apr 10, 2015
1 parent 08b0e90 commit ef78d5a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
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.2.1",
"version": "1.2.2",
"type": "library",
"keywords": [
"method hooking",
"hook",
"interception"
],
"homepage": "http://hookphp.org/",
"time": "2015-02-19",
"time": "2015-04-10",
"license": "LGPL",
"authors": [
{
Expand Down
50 changes: 32 additions & 18 deletions 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.2.1
* @version 1.2.2
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand Down Expand Up @@ -171,7 +171,7 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
//$fprefix = $curMethod->isFinal() ? 'final ' : '';
$fprefix = $curMethod->isStatic() ? 'static ' : '';
$params = $curMethod->getParameters();
$paramArray = array(); //$paramNameArray
$paramArray = $paramNameArray = array();
foreach ($params as &$curParam) {
$paramName = $curParam->getName();
$paramPrefix = $curParam->isPassedByReference() ? '&' : '';
Expand All @@ -181,26 +181,40 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
$paramSuffix = '';
}
$paramArray[] = "{$paramPrefix}\${$paramName}{$paramSuffix}";
//$paramNameArray[] = "{$paramPrefix}\${$paramName}";
$paramNameArray[] = "{$paramPrefix}\${$paramName}";
}
unset($curParam);
$code .= $fprefix."function $fname(".implode(', ', $paramArray).") {\n"
// We must use a debug_backtrace, because that's the best way to
// get all the passed arguments, by reference. 5.4 and up lets
// us limit it to 1 frame.
.(version_compare(PHP_VERSION, '5.4.0') >= 0 ?
"\t\$arguments = debug_backtrace(false, 1);\n" :
"\t\$arguments = debug_backtrace(false);\n"
.(defined('HHVM_VERSION') ?
(
// There is bad behavior in HHVM where debug_backtrace
// won't return arguments, but calling func_get_args
// somewhere in the function changes that behavior to be
// consistent with official PHP. However, it also
// returns arguments by value, instead of by reference.
// So, we must use a more direct method.
"\t\$arguments = array();\n"
.(count($paramNameArray) > 0 ?
"\t\$arguments[] = ".implode('; $arguments[] = ', $paramNameArray).";\n" :
''
)
."\t\$real_arg_count = func_num_args();\n"
."\t\$arg_count = count(\$arguments);\n"
."\tif (\$real_arg_count > \$arg_count) {\n"
."\t\tfor (\$i = \$arg_count; \$i < \$real_arg_count; \$i++)\n"
."\t\t\t\$arguments[] = func_get_arg(\$i);\n"
."\t}\n"
) : (
// We must use a debug_backtrace, because that's the
// best way to get all the passed arguments, by
// reference. 5.4 and up lets us limit it to 1 frame.
(version_compare(PHP_VERSION, '5.4.0') >= 0 ?
"\t\$arguments = debug_backtrace(false, 1);\n" :
"\t\$arguments = debug_backtrace(false);\n"
)
."\t\$arguments = \$arguments[0]['args'];\n"
)
)
."\t\$arguments = \$arguments[0]['args'];\n"
// This method works, but isn't faster, and might introduce bugs.
//."\t\$arguments = array(".implode(', ', $paramNameArray).");\n"
//."\t\$real_arg_count = func_num_args();\n"
//."\t\$arg_count = count(\$arguments);\n"
//."\tif (\$real_arg_count > \$arg_count) {\n"
//."\t\tfor (\$i = \$arg_count; \$i < \$real_arg_count; \$i++)\n"
//."\t\t\t\$arguments[] = func_get_arg(\$i);\n"
//."\t}\n"
."\t\$function = array(\$this->_hookObject, '$fname');\n"
."\t\$data = array();\n"
."\t\\SciActive\\Hook::runCallbacks(\$this->_hookPrefix.'$fname', \$arguments, 'before', \$this->_hookObject, \$function, \$data);\n"
Expand Down
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.2.1
* @version 1.2.2
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand Down
2 changes: 1 addition & 1 deletion 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.2.1
* @version 1.2.2
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand Down

0 comments on commit ef78d5a

Please sign in to comment.