-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code improvements. JSON works. Actual PHPUnit tests.
- Loading branch information
Showing
13 changed files
with
322 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testing/logs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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 { | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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" | ||
|
@@ -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; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.