Skip to content

Commit

Permalink
Add JSON serialization hooking features.
Browse files Browse the repository at this point in the history
  • Loading branch information
hperrin committed Dec 28, 2019
1 parent adde48e commit 26bb6ee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Now you can start setting up objects for method hooking.

```php
class Test {
function testFunction($string) {
echo $string;
}
function testFunction($string) {
echo $string;
}
}
$obj = new Test();
\SciActive\Hook::hookObject($obj, 'Test->');
Expand All @@ -36,7 +36,7 @@ And modifying their method calls.

```php
\SciActive\Hook::addCallback('Test->testFunction', -2, function(&$arguments, $name, &$object, &$function, &$data){
$arguments[0] = 'New argument instead.';
$arguments[0] = 'New argument instead.';
});
```

Expand Down
19 changes: 14 additions & 5 deletions src/HookOverride_extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,20 @@ public function __clone() {
}

public function jsonSerialize() {
if (is_callable($this->_hookObject, 'jsonSerialize')) {
$args = func_get_args();
return call_user_func_array(array($this->_hookObject, 'jsonSerialize'), $args);
} else {
return $this->_hookObject;
$_HOOK_arguments = func_get_args();
$_HOOK_function = array($this->_hookObject, 'jsonSerialize');
$_HOOK_data = array();
\SciActive\Hook::runCallbacks($this->_hookPrefix.'jsonSerialize', $_HOOK_arguments, 'before', $this->_hookObject, $_HOOK_function, $_HOOK_data);
if ($_HOOK_arguments !== false) {
if (is_callable($this->_hookObject, 'jsonSerialize') && !empty($_HOOK_arguments)) {
$_HOOK_return = array(call_user_func_array($_HOOK_function, $_HOOK_arguments));
} else {
$_HOOK_return = array($this->_hookObject);
}
\SciActive\Hook::runCallbacks($this->_hookPrefix.'jsonSerialize', $_HOOK_return, 'after', $this->_hookObject, $_HOOK_function, $_HOOK_data);
if ((array) $_HOOK_return === $_HOOK_return) {
return $_HOOK_return[0];
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions testing/tests/HookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ public function testDataPassing($testModel) {
$this->assertEquals(1, Hook::delCallbackByID('TestModel->testFunction', $ids[0]));
}

/**
* @depends testHooking
*/
public function testJSONSerialization($testModel) {
$this->assertObjectNotHasAttribute('testJson', json_decode(json_encode($testModel)));
$ids = Hook::addCallback('TestModel->jsonSerialize', 1, function (&$return, $name, &$object, &$function, &$data) {
$return[0]->testJson = 'success';
});
$this->assertEquals('success', json_decode(json_encode($testModel))->testJson);
$this->assertEquals(1, Hook::delCallbackByID('TestModel->jsonSerialize', $ids[0]));
$this->assertEquals(0, count(Hook::getCallbacks()['TestModel->jsonSerialize']));
}

/**
* Do this one last, cause it leaves its callbacks.
*
Expand Down

0 comments on commit 26bb6ee

Please sign in to comment.