Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated directory structure

  • Loading branch information...
commit 24f1cdf60bfbc0898272eab39e1876f180a86a31 1 parent 02131c0
@chriso authored
View
14 Doxyfile
@@ -0,0 +1,14 @@
+PROJECT_NAME = Request
+OUTPUT_DIRECTORY = docs
+FULL_PATH_NAMES = NO
+TAB_SIZE = 4
+EXTRACT_ALL = YES
+EXTRACT_ANON_NSPACES = YES
+QUIET = YES
+INPUT = lib
+RECURSIVE = YES
+GENERATE_HTML = YES
+HTML_OUTPUT =
+GENERATE_LATEX = NO
+GENERATE_MAN = NO
+GENERATE_XML = NO
View
9 lib/Autoload.php
@@ -0,0 +1,9 @@
+<?php
+/**
+ * Autoload Request classes.
+ */
+spl_autoload_register(function ($class) {
+ $class = str_replace('\\', '/', $class);
+ require "$class.php";
+});
+
View
39 Factory.php → lib/Decorator/Factory.php
@@ -1,8 +1,4 @@
<?php namespace Decorator;
-
-require 'Introspector.php';
-require 'Wrapper.php';
-
/**
* Experimental runtime decoration of functions, classes,
* methods and properties.
@@ -60,6 +56,7 @@ public function addDecorator($name, $decorator = null, $key = null) {
/**
* Add a decorator that invokes $decorator before a function.
*
+ * @param String $name
* @param String|Array|Closure $decorator
* @return Closure $argument_decorator
*/
@@ -74,6 +71,7 @@ public function addPreDecorator($name, $decorator = null) {
/**
* Add a decorator that invokes $decorator after a function.
*
+ * @param String $name
* @param String|Array|Closure $decorator
* @return Closure $return_decorator
*/
@@ -88,6 +86,7 @@ public function addPostDecorator($name, $decorator = null) {
/**
* Add a decorator that modifies a function's arguments.
*
+ * @param String $name
* @param String|Array|Closure $decorator
* @return Closure $argument_decorator
*/
@@ -102,6 +101,7 @@ public function addArgumentDecorator($name, $decorator = null) {
/**
* Add a decorator that modifies a function's return value.
*
+ * @param String $name
* @param String|Array|Closure $decorator
* @return Closure $return_decorator
*/
@@ -226,12 +226,10 @@ public function hasDecorator($name) {
* Call a function with the specified arguments.
*
* @param String|Array|Closure $function
- * @param Mixed $arg1..N
* @return Mixed $result
*/
public function call($function /*, $arg1, $arg2, $argN */) {
- $args = func_get_args();
- $function = array_shift($args);
+ $args = array_slice(func_get_args(), 1);
return $this->apply($function, $args);
}
@@ -257,12 +255,10 @@ public function apply($function, array $args = array()) {
* Decorate and instantiate a class.
*
* @param String|Object $class
- * @param Mixed $arg1..N
* @return Object $instance
*/
public function instantiate($class /*, $arg1, $arg2, $argN */) {
- $args = func_get_args();
- $class = array_shift($args);
+ $args = array_slice(func_get_args(), 1);
$constructor = $this->decorateClass($class);
return $this->apply($constructor, $args);
}
@@ -270,13 +266,11 @@ public function instantiate($class /*, $arg1, $arg2, $argN */) {
/**
* Decorate and invoke a function.
*
- * @param String|Object $class
- * @param Mixed $arg1..N
+ * @param String|Object $function
* @return Object $instance
*/
public function invoke($function /*, $arg1, $arg2, $argN */) {
- $args = func_get_args();
- $class = array_shift($args);
+ $args = array_slice(func_get_args(), 1);
$function = $this->decorateFunction($function);
return $this->apply($function, $args);
}
@@ -600,30 +594,35 @@ public function getFunctionsWithDecorators() {
/**
* Decorate and invoke functions with the specified annotation.
*
- * @param String $annotation
- * @param Mixed $arg1..N
+ * @param String $decorator
+ * @return Integer $invoked
*/
public function invokeFunctionsWithDecorator($decorator /*, $args */) {
- $args = func_get_args();
- $decorator = array_shift($args);
- $functions = $this->getFunctionsWithDecorator($decorator);
+ $args = array_slice(func_get_args(), 1);
+ $functions = $this->getFunctionsWithAnnotation($decorator);
$functions = $this->decorateFunctions($functions);
+ $invoked = 0;
foreach ($functions as $function) {
$this->apply($function, $args);
+ $invoked++;
}
+ return $invoked;
}
/**
* Invoke all decorated functions.
*
- * @param Mixed $arg1..N
+ * @return Integer $invoked
*/
public function invokeDecoratedFunctions(/* arg1, $arg2, $argN */) {
$args = func_get_args();
+ $invoked = 0;
foreach ($this->getFunctionsWithDecorators() as $function) {
$function = $this->decorateFunction($function);
$this->apply($function, $args);
+ $invoked++;
}
+ return $invoked;
}
}
View
1  Introspector.php → lib/Decorator/Introspector.php
@@ -114,7 +114,6 @@ public function reflectionMethod($class, $method) {
/**
* Gets an array of user-defined functions.
*
- * @param String|Array|Closure $function
* @return Array $function_list
*/
public function getFunctions() {
View
0  Wrapper.php → lib/Decorator/Wrapper.php
File renamed without changes
View
6 phpunit.xml
@@ -0,0 +1,6 @@
+<phpunit bootstrap="lib/Autoload.php" colors="true" strict="true" verbose="true">
+ <logging>
+ <log type="coverage-html" target="coverage" />
+ </logging>
+</phpunit>
+
View
73 tests/DecoratorFactoryTest.php
@@ -0,0 +1,73 @@
+<?php
+
+class DecoratorFactoryTest extends PHPUnit_Framework_TestCase {
+
+ protected $decorator = null;
+
+ public function setUp() {
+ $this->decorator = new Decorator\Factory;
+ }
+
+ public function tearDown() {
+ $this->decorator = null;
+ }
+
+ public function testConstructorClosure() {
+ $constructor = $this->decorator->getConstructorClosure('foo');
+ $foo = $constructor();
+ $this->assertTrue($foo instanceof Foo);
+ $foo = $this->decorator->call($constructor);
+ $this->assertTrue($foo instanceof Foo);
+ }
+
+ public function testSetterClosure() {
+ $closure = $this->decorator->getSetterClosure();
+ $this->assertEquals($closure(2), 2);
+ $this->assertEquals($closure(3), 3);
+ $this->assertEquals($closure('bar'), 'bar');
+ $this->assertEquals($closure(null), null);
+ }
+
+ public function testAddingGlobalFunctionDecoratorsAtInstantiation() {
+ $decorators = array('foo', 'bar');
+ $this->decorator = new Decorator\Factory($decorators);
+ $this->assertTrue($this->decorator->hasDecorator('foo'));
+ $this->assertTrue($this->decorator->hasDecorator('bar'));
+ $this->assertFalse($this->decorator->hasDecorator('unknown'));
+
+ $this->assertEquals($this->decorator->getDecorator('foo'), 'foo');
+ $this->assertEquals($this->decorator->getDecorator('bar'), 'bar');
+
+ $decorators = $this->decorator->getDecorators();
+ $this->assertEquals(array_values($decorators), array('foo', 'bar'));
+ $this->assertEquals(array_keys($decorators), array('foo', 'bar'));
+ }
+
+ public function testFunctionDecorators() {
+ $this->decorator->addDecorator('returnformat', function ($callback, $format) {
+ return function () use ($callback, $format) {
+ $return = call_user_func_array($callback, func_get_args());
+ return str_replace('%s', $return, $format);
+ };
+ });
+
+ $result = $this->decorator->invoke('returnvalue2', 'foo');
+ $this->assertEquals($result, '<pre>foo</pre>');
+
+ $this->decorator->addArgumentDecorator('barinsteadoffoo', function ($args) {
+ $args[0] = 'bar';
+ return $args;
+ });
+
+ $result = $this->decorator->invoke('returnvalue2', 'foo');
+ $this->assertEquals($result, '<pre>bar</pre>');
+
+ $this->decorator->addReturnDecorator('reverseresult', function ($return) {
+ return strrev($return);
+ });
+
+ $result = $this->decorator->invoke('returnvalue2', 'foo');
+ $this->assertEquals($result, '<pre>rab</pre>');
+ }
+
+}
View
3  tests/IntrospectorTest.php → tests/DecoratorIntrospectorTest.php
@@ -1,9 +1,8 @@
<?php
-require_once 'Introspector.php';
require_once 'tests/include.php';
-class IntrospectorTest extends PHPUnit_Framework_TestCase {
+class DecoratorIntrospectorTest extends PHPUnit_Framework_TestCase {
protected $introspector = null;
View
17 tests/include.php
@@ -7,6 +7,22 @@
function foobar() {}
/**
+ * @multlplyresultby 2
+ */
+function returnValue($value) {
+ return $value;
+}
+
+/**
+ * @reverseresult
+ * @barinsteadoffoo
+ * @returnformat <pre>%s</pre>
+ */
+function returnValue2($value) {
+ return $value;
+}
+
+/**
* @singleton
*/
class Foo {
@@ -28,3 +44,4 @@ public function foo() {}
protected function bar() {}
}
+
Please sign in to comment.
Something went wrong with that request. Please try again.