Skip to content

Commit

Permalink
Code improvements. JSON works. Actual PHPUnit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hperrin committed Apr 9, 2015
1 parent 6899180 commit 264223e
Show file tree
Hide file tree
Showing 13 changed files with 322 additions and 114 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testing/logs
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ Method hooking in PHP.

## Installation

You can install HookPHP with Composer or Bower.
You can install HookPHP with Composer.

```sh
composer require sciactive/hookphp

bower install https://github.com/sciactive/hookphp.git
```

## Getting Started
Expand All @@ -30,7 +28,7 @@ class Test {
echo $string;
}
}
$obj = new Test;
$obj = new Test();
\SciActive\Hook::hookObject($obj, 'Test->');
```

Expand Down Expand Up @@ -103,6 +101,14 @@ A callback can replace `$function` to cause a different function or method to ru

A callback can add and alter data in the `$data` array to communicate with other callbacks. This is especially useful when communicating with callbacks that run on the other side of the method call.

## Retrieving a Hooked Object

If you need to retrieve an object that has been hooked, you can use the `_hookObject` method:

```php
$originalObject = $hookedObject->_hookObject();
```

## Contacting the Developer

There are several ways to contact HookPHP's developer with your questions, concerns, comments, bug reports, or feature requests.
Expand Down
14 changes: 0 additions & 14 deletions bower.json

This file was deleted.

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sciactive/hookphp",
"description": "Method hooking in PHP.",
"version": "1.1.0",
"version": "1.2.0",
"type": "library",
"keywords": [
"method hooking",
Expand All @@ -25,7 +25,7 @@
"source": "https://github.com/sciactive/hookphp"
},
"archive": {
"exclude": ["bower.json", "test*"]
"exclude": ["testing"]
},
"autoload": {
"psr-0": {
Expand Down
20 changes: 10 additions & 10 deletions src/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
* Hooks are used to call a callback when a method is called and optionally
* manipulate the arguments/function call/return value.
*
* @version 1.1.0
* @version 1.2.0
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
* @link http://requirephp.org
*/

if (!class_exists('\\SciActive\\HookOverride')) {
if (!class_exists('\SciActive\HookOverride')) {
include_once(__DIR__.DIRECTORY_SEPARATOR.'HookOverride.php');
}

Expand Down Expand Up @@ -141,7 +141,7 @@ 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\\Hook', 'depend', 'config', 'info'))) {
if (isset($_) && in_array($className, array('\SciActive\Hook', 'depend', 'config', 'info'))) {
return false;
}

Expand All @@ -153,7 +153,7 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
}
}

if (!class_exists("\\SciActive\\HookOverride_$className")) {
if (!class_exists("\SciActive\HookOverride_$className")) {
if ($isString) {
$reflection = new \ReflectionClass($object);
} else {
Expand All @@ -164,7 +164,7 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
$code = '';
foreach ($methods as &$curMethod) {
$fname = $curMethod->getName();
if (in_array($fname, array('__construct', '__destruct', '__get', '__set', '__isset', '__unset', '__toString', '__invoke', '__set_state', '__clone', '__sleep'))) {
if (in_array($fname, array('__construct', '__destruct', '__get', '__set', '__isset', '__unset', '__toString', '__invoke', '__set_state', '__clone', '__sleep', 'jsonSerialize'))) {
continue;
}

Expand Down Expand Up @@ -201,15 +201,15 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
//."\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->_hook_object, '$fname');\n"
."\t\$function = array(\$this->_hookObject, '$fname');\n"
."\t\$data = array();\n"
."\t\\SciActive\\Hook::runCallbacks(\$this->_hook_prefix.'$fname', \$arguments, 'before', \$this->_hook_object, \$function, \$data);\n"
."\t\\SciActive\\Hook::runCallbacks(\$this->_hookPrefix.'$fname', \$arguments, 'before', \$this->_hookObject, \$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\\Hook::hookObject(\$return, '$prefix', false);\n"
."\t\t\$return = array(\$return);\n"
."\t\t\\SciActive\\Hook::runCallbacks(\$this->_hook_prefix.'$fname', \$return, 'after', \$this->_hook_object, \$function, \$data);\n"
."\t\t\\SciActive\\Hook::runCallbacks(\$this->_hookPrefix.'$fname', \$return, 'after', \$this->_hookObject, \$function, \$data);\n"
."\t\tif ((array) \$return === \$return)\n"
."\t\t\treturn \$return[0];\n"
."\t}\n"
Expand All @@ -218,10 +218,10 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
unset($curMethod);
// Build a HookOverride class.
$include = str_replace(array('_NAMEHERE_', '//#CODEHERE#', '<?php', '?>'), array($className, $code, '', ''), Hook::$hookFile);
eval ($include);
eval($include);
}

eval ('$object = new \\SciActive\\HookOverride_'.$className.' ($object, $prefix);');
eval('$object = new \SciActive\HookOverride_'.$className.' ($object, $prefix);');
return true;
}

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.1.0
* @version 1.2.0
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand Down
46 changes: 23 additions & 23 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.1.0
* @version 1.2.0
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand All @@ -15,74 +15,74 @@
* This class is dynamically edited during the takeover of an object for
* hooking.
*/
class HookOverride__NAMEHERE_ extends HookOverride {
class HookOverride__NAMEHERE_ extends HookOverride implements \JsonSerializable {
/**
* Used to store the overridden class.
* @var mixed $_hook_object
* @var mixed $_hookObject
*/
protected $_hook_object = null;
protected $_hookObject = null;
/**
* Used to store the prefix (the object's variable name).
* @var string $_hook_prefix
* @var string $_hookPrefix
*/
protected $_hook_prefix = '';
protected $_hookPrefix = '';

public function _hook_object() {
return $this->_hook_object;
public function _hookObject() {
return $this->_hookObject;
}

public function __construct(&$object, $prefix = '') {
$this->_hook_object = $object;
$this->_hook_prefix = $prefix;
$this->_hookObject = $object;
$this->_hookPrefix = $prefix;
}

public function &__get($name) {
return $val =& $this->_hook_object->$name;
return $val =& $this->_hookObject->$name;
}

public function __set($name, $value) {
return $this->_hook_object->$name = $value;
return $this->_hookObject->$name = $value;
}

public function __isset($name) {
return isset($this->_hook_object->$name);
return isset($this->_hookObject->$name);
}

public function __unset($name) {
unset($this->_hook_object->$name);
unset($this->_hookObject->$name);
}

public function __toString() {
return (string) $this->_hook_object;
return (string) $this->_hookObject;
}

public function __invoke() {
if (method_exists($this->_hook_object, '__invoke')) {
if (method_exists($this->_hookObject, '__invoke')) {
$args = func_get_args();
return call_user_func_array(array($this->_hook_object, '__invoke'), $args);
return call_user_func_array(array($this->_hookObject, '__invoke'), $args);
}
}

public function __set_state() {
if (method_exists($this->_hook_object, '__set_state')) {
if (method_exists($this->_hookObject, '__set_state')) {
$args = func_get_args();
return call_user_func_array(array($this->_hook_object, '__set_state'), $args);
return call_user_func_array(array($this->_hookObject, '__set_state'), $args);
}
}

public function __clone() {
// TODO: Test this. Make sure cloning works properly.
$newObject = clone $this->_hook_object;
$newObject = clone $this->_hookObject;
Hook::hookObject($newObject, get_class($newObject).'->', false);
return $newObject;
}

public function jsonSerialize() {
if (method_exists($this->_hook_object, 'jsonSerialize')) {
if (is_callable($this->_hookObject, 'jsonSerialize')) {
$args = func_get_args();
return call_user_func_array(array($this->_hook_object, 'jsonSerialize'), $args);
return call_user_func_array(array($this->_hookObject, 'jsonSerialize'), $args);
} else {
return json_decode(json_encode($this->_hook_object), true);
return $this->_hookObject;
}
}

Expand Down
60 changes: 0 additions & 60 deletions test.php

This file was deleted.

13 changes: 13 additions & 0 deletions testing/TestModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

class TestModel {
public $test = 'right';

public function testFunction($argument) {
return $argument;
}

public function testFunctionFake($argument) {
return false;
}
}
19 changes: 19 additions & 0 deletions testing/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

error_reporting(E_ALL);

spl_autoload_register(function ($class) {
$prefix = 'SciActive\\';
$base_dir = __DIR__ . '/../src/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});

require_once __DIR__.'/TestModel.php';
30 changes: 30 additions & 0 deletions testing/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.3/phpunit.xsd"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
timeoutForSmallTests="1"
timeoutForMediumTests="10"
timeoutForLargeTests="60"
strict="false"
verbose="false">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">../src</directory>
<exclude>
<file>../src/HookOverride_extend.php</file>
</exclude>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit 264223e

Please sign in to comment.