Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sciactive/hookphp
Browse files Browse the repository at this point in the history
Conflicts:
	.gitignore
	composer.lock
	src/Hook.php
	src/HookOverride.php
	testing/TestModel.php
	testing/tests/HookTest.php
  • Loading branch information
hperrin committed Oct 25, 2017
2 parents e9160ed + f0605a7 commit d281d77
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 222 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
testing/logs
vendor
vendor/
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm
- 7.0
- 7.1
# - hhvm

install: composer install --no-interaction

script: ./testing/run
366 changes: 201 additions & 165 deletions LICENSE

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# <img alt="logo" src="https://raw.githubusercontent.com/sciactive/2be-extras/master/logo/product-icon-40-bw.png" align="top" /> HookPHP
# HookPHP

[![Build Status](https://scrutinizer-ci.com/g/sciactive/hookphp/badges/build.png?b=master)](https://scrutinizer-ci.com/g/sciactive/hookphp/build-status/master) [![Latest Stable Version](https://img.shields.io/packagist/v/sciactive/hookphp.svg?style=flat)](https://packagist.org/packages/sciactive/hookphp) [![License](https://img.shields.io/packagist/l/sciactive/hookphp.svg?style=flat)](https://packagist.org/packages/sciactive/hookphp) [![Open Issues](https://img.shields.io/github/issues/sciactive/hookphp.svg?style=flat)](https://github.com/sciactive/hookphp/issues) [![Code Quality](https://img.shields.io/scrutinizer/g/sciactive/hookphp.svg?style=flat)](https://scrutinizer-ci.com/g/sciactive/hookphp/) [![Coverage](https://img.shields.io/scrutinizer/coverage/g/sciactive/hookphp.svg?style=flat)](https://scrutinizer-ci.com/g/sciactive/hookphp/)
[![Build Status](https://img.shields.io/travis/sciactive/hookphp.svg)](https://travis-ci.org/sciactive/hookphp) [![Latest Stable Version](https://img.shields.io/packagist/v/sciactive/hookphp.svg?style=flat)](https://packagist.org/packages/sciactive/hookphp) [![License](https://img.shields.io/packagist/l/sciactive/hookphp.svg?style=flat)](https://packagist.org/packages/sciactive/hookphp) [![Open Issues](https://img.shields.io/github/issues/sciactive/hookphp.svg?style=flat)](https://github.com/sciactive/hookphp/issues) [![Code Quality](https://img.shields.io/scrutinizer/g/sciactive/hookphp.svg?style=flat)](https://scrutinizer-ci.com/g/sciactive/hookphp/) [![Coverage](https://img.shields.io/scrutinizer/coverage/g/sciactive/hookphp.svg?style=flat)](https://scrutinizer-ci.com/g/sciactive/hookphp/)

Method hooking in PHP.
Method hooking (decorators) in PHP.

## Installation

Expand Down
17 changes: 10 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "sciactive/hookphp",
"description": "Method hooking in PHP.",
"version": "1.2.2",
"version": "2.0.0",
"type": "library",
"keywords": [
"method hooking",
"hook",
"interception"
],
"homepage": "http://hookphp.org/",
"time": "2015-04-10",
"license": "LGPL",
"time": "2017-06-17",
"license": "Apache-2.0",
"authors": [
{
"name": "Hunter Perrin",
Expand All @@ -19,6 +19,12 @@
"role": "Developer"
}
],
"require": {
"php": "~7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.1"
},
"support": {
"issues": "https://github.com/sciactive/hookphp/issues",
"wiki": "https://github.com/sciactive/hookphp/wiki",
Expand All @@ -34,8 +40,5 @@
"psr-4": {
"SciActive\\": "src/"
}
},
"require-dev": {
"phpunit/phpunit": "^6.1"
}
}
}
6 changes: 4 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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.2
* @version 2.0.0
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand Down Expand Up @@ -176,7 +176,8 @@ public static function hookObject(&$object, $prefix = '', $recursive = true) {
$paramArray = $paramNameArray = array();
foreach ($params as &$curParam) {
$paramName = $curParam->getName();
$paramPrefix = $curParam->isPassedByReference() ? '&' : '';
$paramPrefix = $curParam->isVariadic() ? '...' : '';
$paramPrefix .= $curParam->isPassedByReference() ? '&' : '';
if ($curParam->isDefaultValueAvailable()) {
$paramSuffix = ' = '.var_export($curParam->getDefaultValue(), true);
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/HookOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Dynamic HookOverride class.
*
* @version 1.2.2
* @version 2.0.0
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand All @@ -14,4 +14,6 @@
/**
* A base class to check whether an object has been hooked.
*/
class HookOverride { }
class HookOverride {

}
2 changes: 1 addition & 1 deletion src/HookOverride_extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Dynamic HookOverride class.
*
* @version 1.2.2
* @version 2.0.0
* @license https://www.gnu.org/licenses/lgpl.html
* @author Hunter Perrin <[email protected]>
* @copyright SciActive.com
Expand Down
5 changes: 5 additions & 0 deletions testing/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public function testFunctionFake($argument) {
return false;
}

public function testFunctionVariadic($argument, ...$rest) {
return [$argument, $rest];
}

public function testReferenceFunction(&$argument) {
$argument['test'] = 12;
return $argument;
}
}
4 changes: 2 additions & 2 deletions testing/run
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ echo "Use \`run coverage\` to build coverage clover."
echo

if [[ "$1" == "coverage" ]]; then
CVG="--coverage-clover logs/clover.xml"
CVG="--coverage-clover logs/clover.xml"
else
CVG=""
CVG=""
fi

cd $(dirname $0)
Expand Down
79 changes: 46 additions & 33 deletions testing/tests/HookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testHooking() {
*/
public function testObjectAccess($testModel) {
$that = $this;
$ids = Hook::addCallback('TestModel->testFunction', -2, function(&$arguments, $name, &$object, &$function, &$data) use ($that) {
$ids = Hook::addCallback('TestModel->testFunction', -2, function (&$arguments, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals('right', $object->test);
$that->assertEquals('TestModel->testFunction', $name);
});
Expand All @@ -28,21 +28,22 @@ public function testObjectAccess($testModel) {
*/
public function testPassByReference($testModel) {
$that = $this;
$ids = Hook::addCallback('TestModel->testReferenceFunction', -2, function(&$arguments, $name, &$object, &$function, &$data) use ($that) {
$ids = Hook::addCallback('TestModel->testReferenceFunction', -2, function (&$arguments, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals('right', $object->test);
$that->assertEquals('TestModel->testReferenceFunction', $name);
});
$arg = ['data' => true];
$this->assertEquals($testModel->testReferenceFunction($arg), $arg);
$this->assertTrue($arg['data']);
$this->assertEquals($arg['test'], 12);
$this->assertEquals(1, Hook::delCallbackByID('TestModel->testReferenceFunction', $ids[0]));
}

/**
* @depends testHooking
*/
public function testFunctionOverride($testModel) {
$ids = Hook::addCallback('TestModel->testFunction', -1, function(&$arguments, $name, &$object, &$function, &$data) {
$ids = Hook::addCallback('TestModel->testFunction', -1, function (&$arguments, $name, &$object, &$function, &$data) {
$function = array($object, 'testFunctionFake');
});
$this->assertFalse($testModel->testFunction(true));
Expand All @@ -54,18 +55,30 @@ public function testFunctionOverride($testModel) {
*/
public function testReturnValues($testModel) {
$that = $this;
$ids = Hook::addCallback('TestModel->testFunction', 2, function(&$return, $name, &$object, &$function, &$data) use ($that) {
$ids = Hook::addCallback('TestModel->testFunction', 2, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals('success', $return[0]);
});
$testModel->testFunction('success');
$this->assertEquals(1, Hook::delCallbackByID('TestModel->testFunction', $ids[0]));
}

/**
* @depends testHooking
*/
public function testVariadic($testModel) {
$that = $this;
$ids = Hook::addCallback('TestModel->testFunctionVariadic', 2, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(['success', [1, 2, 3]], $return[0]);
});
$testModel->testFunction('success', 1, 2, 3);
$this->assertEquals(1, Hook::delCallbackByID('TestModel->testFunctionVariadic', $ids[0]));
}

/**
* @depends testHooking
*/
public function testArgumentModification($testModel) {
$ids = Hook::addCallback('TestModel->testFunction', -1, function(&$arguments, $name, &$object, &$function, &$data) {
$ids = Hook::addCallback('TestModel->testFunction', -1, function (&$arguments, $name, &$object, &$function, &$data) {
$arguments[0] = 'success';
});
$this->assertEquals('success', $testModel->testFunction(true));
Expand All @@ -77,10 +90,10 @@ public function testArgumentModification($testModel) {
*/
public function testDataPassing($testModel) {
$that = $this;
Hook::addCallback('TestModel->testFunction', -1, function(&$arguments, $name, &$object, &$function, &$data) {
Hook::addCallback('TestModel->testFunction', -1, function (&$arguments, $name, &$object, &$function, &$data) {
$data['test'] = 1;
});
$ids = Hook::addCallback('TestModel->testFunction', 2, function(&$return, $name, &$object, &$function, &$data) use ($that) {
$ids = Hook::addCallback('TestModel->testFunction', 2, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(1, $data['test']);
});
$testModel->testFunction(true);
Expand All @@ -96,106 +109,106 @@ public function testDataPassing($testModel) {
*/
public function testTimeline($testModel) {
$that = $this;
Hook::addCallback('TestModel->testFunction', -1000, function(&$arguments, $name, &$object, &$function, &$data) {
Hook::addCallback('TestModel->testFunction', -1000, function (&$arguments, $name, &$object, &$function, &$data) {
$data['timeline'] = 1;
});
Hook::addCallback('TestModel->testFunction', -100, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -100, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(1, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -90, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -90, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(2, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -70, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -70, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(3, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -30, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -30, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(4, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -10, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -10, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(5, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -9, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -9, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(6, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -8, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -8, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(7, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -7, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -7, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(8, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -6, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -6, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(9, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -5, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -5, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(10, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -4, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -4, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(11, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -3, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -3, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(12, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -2, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -2, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(13, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', -1, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', -1, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(14, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 1, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 1, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(15, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 2, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 2, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(16, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 3, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 3, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(17, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 4, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 4, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(18, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 5, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 5, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(19, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 6, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 6, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(20, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 7, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 7, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(21, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 8, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 8, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(22, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 9, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 9, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(23, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 10, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 10, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(24, $data['timeline']);
$data['timeline']++;
});
Hook::addCallback('TestModel->testFunction', 1000, function(&$return, $name, &$object, &$function, &$data) use ($that) {
Hook::addCallback('TestModel->testFunction', 1000, function (&$return, $name, &$object, &$function, &$data) use ($that) {
$that->assertEquals(25, $data['timeline']);
// Cool, it all worked.
throw new Exception('Everything is good.');
Expand Down

0 comments on commit d281d77

Please sign in to comment.