From 869b3714d2b42e8d9560749f70468e4a1eee246a Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 12:13:22 +0900 Subject: [PATCH 01/10] make array args for match --- src/AbstractMatcher.php | 10 ++-------- src/Matcher.php | 10 +++++----- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/AbstractMatcher.php b/src/AbstractMatcher.php index 2d26deae..684fef0b 100644 --- a/src/AbstractMatcher.php +++ b/src/AbstractMatcher.php @@ -61,14 +61,8 @@ protected function createMatcher($method, $args) */ public function __invoke($class, $target) { - $args = [$class, $target]; - $thisArgs = is_array($this->args) ? $this->args : [$this->args]; - foreach ($thisArgs as $arg) { - $args[] = $arg; - } - $method = 'is' . $this->method; - $match = new Match; - $matched = call_user_func_array([$match, $method], $args); + $args = array_merge([$class, $target], $this->args); + $matched = call_user_func_array([new Match, 'is' . $this->method], $args); return $matched; } diff --git a/src/Matcher.php b/src/Matcher.php index e6dd23ac..bdec4e15 100644 --- a/src/Matcher.php +++ b/src/Matcher.php @@ -28,7 +28,7 @@ public function __construct(Reader $reader = null) */ public function any() { - return $this->createMatcher(__FUNCTION__, null); + return $this->createMatcher(__FUNCTION__, [null]); } /** @@ -40,7 +40,7 @@ public function annotatedWith($annotationName) throw new InvalidAnnotation($annotationName); } - return $this->createMatcher(__FUNCTION__, $annotationName); + return $this->createMatcher(__FUNCTION__, [$annotationName]); } /** @@ -48,7 +48,7 @@ public function annotatedWith($annotationName) */ public function subclassesOf($superClass) { - return $this->createMatcher(__FUNCTION__, $superClass); + return $this->createMatcher(__FUNCTION__, [$superClass]); } /** @@ -64,7 +64,7 @@ public function startWith($prefix) */ public function startsWith($prefix) { - return $this->createMatcher(__FUNCTION__, $prefix); + return $this->createMatcher(__FUNCTION__, [$prefix]); } /** @@ -106,7 +106,7 @@ public function logicalXor(Matchable $matcherA, Matchable $matcherB) public function logicalNot(Matchable $matcher) { $this->method = __FUNCTION__; - $this->args = $matcher; + $this->args = [$matcher]; return clone $this; } From 35251916f85d8415c567f229e651e2f11e1f794e Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 12:19:47 +0900 Subject: [PATCH 02/10] remove reader from matcher constructor --- README.ja.md | 2 +- docs/sample/04-annotation/main.php | 2 +- docs/sample/05-my-matcher/main.php | 2 +- tests/BindTest.php | 24 ++++++++++++------------ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.ja.md b/README.ja.md index 8de525e3..22d7a2ad 100644 --- a/README.ja.md +++ b/README.ja.md @@ -68,7 +68,7 @@ class WeekendBlocker implements MethodInterceptor ```php any(), diff --git a/docs/sample/04-annotation/main.php b/docs/sample/04-annotation/main.php index 43b11a2c..2472e550 100644 --- a/docs/sample/04-annotation/main.php +++ b/docs/sample/04-annotation/main.php @@ -9,7 +9,7 @@ use Doctrine\Common\Annotations\AnnotationReader as Reader; -$matcher = new Matcher(new Reader); +$matcher = new Matcher; $interceptors = [new WeekendBlocker]; $pointcut = new Pointcut( $matcher->any(), diff --git a/docs/sample/05-my-matcher/main.php b/docs/sample/05-my-matcher/main.php index a23ded12..bc75697b 100644 --- a/docs/sample/05-my-matcher/main.php +++ b/docs/sample/05-my-matcher/main.php @@ -9,7 +9,7 @@ use Doctrine\Common\Annotations\AnnotationReader as Reader; -$matcher = new Matcher(new Reader); +$matcher = new Matcher; $myMatcher = new MyMatcher; $interceptors = [new WeekendBlocker]; $pointcut = new Pointcut( diff --git a/tests/BindTest.php b/tests/BindTest.php index 32250f29..cb021bf7 100644 --- a/tests/BindTest.php +++ b/tests/BindTest.php @@ -44,7 +44,7 @@ public function testBindInterceptors() public function testBindAnyAny() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $pointcut = new Pointcut($matcher->any(), $matcher->any(), $this->interceptors); $class = 'Ray\Aop\Mock\AnnotateClass'; $result = $this->bind->bind($class, [$pointcut]); @@ -56,7 +56,7 @@ public function testBindAnyAny() public function testBindAnySubClassOf() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $pointcut = new Pointcut($matcher->subclassesOf('Ray\Aop\parentClass'), $matcher->any(), $this->interceptors); $class = 'Ray\Aop\childClass'; $result = $this->bind->bind($class, [$pointcut]); @@ -68,7 +68,7 @@ public function testBindAnySubClassOf() public function testBindAnyAnnotatedWith() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $class = 'Ray\Aop\Mock\AnnotateClass'; $annotationName = 'Ray\Aop\Annotation\Marker'; $pointcut = new Pointcut($matcher->any(), $matcher->annotatedWith($annotationName), $this->interceptors); @@ -81,7 +81,7 @@ public function testBindAnyAnnotatedWith() public function testBindAnyAnnotatedWithAnnotation() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $class = 'Ray\Aop\Mock\AnnotateClass'; $annotationName = 'Ray\Aop\Annotation\Marker'; $pointcut = new Pointcut($matcher->any(), $matcher->annotatedWith($annotationName), $this->interceptors); @@ -95,7 +95,7 @@ public function testBindAnyAnnotatedWithAnnotation() public function testBindAnyAnnotatedWithDoubleBind() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $class = 'Ray\Aop\Mock\AnnotateClass'; $annotationName = 'Ray\Aop\Annotation\Marker'; $interceptors1 = [new VoidInterceptor, new DoubleInterceptor]; @@ -119,7 +119,7 @@ public function testBindAnyAnnotatedWithDoubleBind() */ public function testBindAnyAnnotatedWithInvalidAnnotationName() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $class = 'Ray\Aop\Mock\AnnotateClass'; $annotationName = 'Ray\Aop\Annotation\AnnotationNotExistXXX'; $pointcut = new Pointcut($matcher->any(), $matcher->annotatedWith($annotationName), $this->interceptors); @@ -138,7 +138,7 @@ public function testToString() public function testHasBindingReturnTrue() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $pointcut = new Pointcut($matcher->subclassesOf('Ray\Aop\parentClass'), $matcher->any(), $this->interceptors); $class = 'Ray\Aop\childClass'; $this->bind->bind($class, [$pointcut]); @@ -164,7 +164,7 @@ public function testInvoke() */ public function testBindByAnnotateBinding() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $class = 'Ray\Aop\Mock\AnnotateClass'; $annotationName = 'Ray\Aop\Annotation\Resource'; $pointcut = new Pointcut($matcher->any(), $matcher->annotatedWith($annotationName), $this->interceptors); @@ -191,7 +191,7 @@ public function testUnserialize($data) public function testNotClassMatch() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $class = 'Ray\Aop\Mock\AnnotateClass'; $pointcut = new Pointcut($matcher->startsWith('XXX'), $matcher->startsWith('XXX'), $this->interceptors); $this->bind->bind($class, [$pointcut]); @@ -203,7 +203,7 @@ public function testNotClassMatch() */ public function logicalMethodMatchers() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; return [ [$matcher->logicalOr($matcher->annotatedWith('Ray\Aop\Annotation\Resource'), $matcher->annotatedWith('Ray\Aop\Annotation\Marker'))], @@ -219,7 +219,7 @@ public function logicalMethodMatchers() */ public function testBindByLogicalBindingAsMethodMatcher(Matchable $logicalMethodMatcher) { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $class = 'Ray\Aop\Mock\AnnotateClass'; $pointcut = new Pointcut($matcher->any(), $logicalMethodMatcher, $this->interceptors); @@ -233,7 +233,7 @@ public function testBindByLogicalBindingAsMethodMatcher(Matchable $logicalMethod public function testBindByMethodLogicalBinding() { - $matcher = new Matcher(new Reader); + $matcher = new Matcher; $class = 'Ray\Aop\Mock\AnnotateClass'; $pointcut = new Pointcut( $matcher->any(), From 07afed71785d3daf28199a52501187d619280095 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 12:43:00 +0900 Subject: [PATCH 03/10] compiler take default PHPParser_PrettyPrinter_Default --- src/Compiler.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Compiler.php b/src/Compiler.php index edd28e13..e7442400 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -18,6 +18,7 @@ use PHPParser_Lexer; use Serializable; use ReflectionParameter; +use PHPParser_PrettyPrinter_Default; /** * AOP compiler @@ -50,10 +51,10 @@ final class Compiler implements CompilerInterface, Serializable */ public function __construct( $classDir, - PHPParser_PrettyPrinterAbstract $printer + PHPParser_PrettyPrinterAbstract $printer = null ) { $this->classDir = $classDir; - $this->printer = $printer; + $this->printer = $printer ?: new PHPParser_PrettyPrinter_Default; } /** From 5e59b34eb9761f53c9bc0c5b78bb69a09d70850c Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 12:44:56 +0900 Subject: [PATCH 04/10] remove instance.php from sample script --- README.ja.md | 4 ++-- docs/sample/01-quick-weave/main.php | 9 ++++----- docs/sample/02-multiple-interceptors/main.php | 8 +++++--- docs/sample/03-benchmark/main.php | 7 ++++--- docs/sample/04-annotation/main.php | 9 ++++----- docs/sample/05-my-matcher/main.php | 11 ++++------- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/README.ja.md b/README.ja.md index 22d7a2ad..223bab00 100644 --- a/README.ja.md +++ b/README.ja.md @@ -77,7 +77,7 @@ $pointcut = new Pointcut( ); $bind->bind('Ray\Aop\Sample\AnnotationRealBillingService', [$pointcut]); -$compiler = require dirname(__DIR__) . '/scripts/instance.php'; +$compiler = new Compiler(sys_get_temp_dir()); $billing = $compiler->newInstance('RealBillingService', [], $bind); try { echo $billing->chargeOrder(); @@ -109,7 +109,7 @@ Explicit method name match $bind = new Bind; $bind->bindInterceptors('chargeOrder', [new WeekendBlocker]); - $compiler = require dirname(__DIR__) . '/scripts/instance.php'; + $compiler = new Compiler(sys_get_temp_dir()); $billing = $compiler->newInstance('RealBillingService', [], $bind); try { echo $billing->chargeOrder(); diff --git a/docs/sample/01-quick-weave/main.php b/docs/sample/01-quick-weave/main.php index cd9961e6..087a96d6 100644 --- a/docs/sample/01-quick-weave/main.php +++ b/docs/sample/01-quick-weave/main.php @@ -1,14 +1,13 @@ bindInterceptors('chargeOrder', [new WeekendBlocker]); - -$compiler = require dirname(dirname(dirname(__DIR__))) . '/scripts/instance.php'; +$compiler = new Compiler(sys_get_temp_dir()); $billingService = $compiler->newInstance('Ray\Aop\Sample\RealBillingService', [], $bind); try { diff --git a/docs/sample/02-multiple-interceptors/main.php b/docs/sample/02-multiple-interceptors/main.php index 996e5618..737e4d23 100644 --- a/docs/sample/02-multiple-interceptors/main.php +++ b/docs/sample/02-multiple-interceptors/main.php @@ -1,13 +1,15 @@ bindInterceptors('chargeOrder', array(new Timer, new interceptorA, new interceptorB)); -$compiler = require dirname(dirname(dirname(__DIR__))) . '/scripts/instance.php'; +$compiler = new Compiler(sys_get_temp_dir()); $billingService = $compiler->newInstance('Ray\Aop\Sample\RealBillingService', [], $bind); diff --git a/docs/sample/03-benchmark/main.php b/docs/sample/03-benchmark/main.php index ea76d528..e90bc86d 100644 --- a/docs/sample/03-benchmark/main.php +++ b/docs/sample/03-benchmark/main.php @@ -1,10 +1,11 @@ bindInterceptors('chargeOrder', array()); @@ -17,7 +18,7 @@ $bind4 = new Bind; $bind4->bindInterceptors('chargeOrder', array(new EmptyInterceptor, new EmptyInterceptor, new EmptyInterceptor, new EmptyInterceptor)); -$compiler = require dirname(dirname(dirname(__DIR__))) . '/scripts/instance.php'; +$compiler = new Compiler(sys_get_temp_dir()); $billing0 = $compiler->newInstance('Ray\Aop\Sample\RealBillingService', [], new Bind); $billing1 = $compiler->newInstance('Ray\Aop\Sample\RealBillingService', [], $bind1); diff --git a/docs/sample/04-annotation/main.php b/docs/sample/04-annotation/main.php index 2472e550..25e167a4 100644 --- a/docs/sample/04-annotation/main.php +++ b/docs/sample/04-annotation/main.php @@ -1,9 +1,8 @@ bind('Ray\Aop\Sample\AnnotationRealBillingService', [$pointcut]); -$compiler = require dirname(dirname(dirname(__DIR__))) . '/scripts/instance.php'; +$compiler = new Compiler(sys_get_temp_dir()); $billingService = $compiler->newInstance('Ray\Aop\Sample\RealBillingService', [], $bind); try { echo $billingService->chargeOrder(); diff --git a/docs/sample/05-my-matcher/main.php b/docs/sample/05-my-matcher/main.php index bc75697b..a00635ee 100644 --- a/docs/sample/05-my-matcher/main.php +++ b/docs/sample/05-my-matcher/main.php @@ -1,14 +1,11 @@ bind('Ray\Aop\Sample\AnnotationRealBillingService', [$pointcut]); -$compiler = require dirname(dirname(dirname(__DIR__))) . '/scripts/instance.php'; +$compiler = new Compiler(sys_get_temp_dir()); $billingService = $compiler->newInstance('Ray\Aop\Sample\RealBillingService', [], $bind); try { echo $billingService->chargeOrder(); From 95ab9d68beda794b3e86f2e5cd8b941944de20c0 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 12:45:35 +0900 Subject: [PATCH 05/10] createMatcher take args only array --- docs/sample/05-my-matcher/MyMatcher.php | 2 +- src/AbstractMatcher.php | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/sample/05-my-matcher/MyMatcher.php b/docs/sample/05-my-matcher/MyMatcher.php index bf8a6221..7166e56a 100644 --- a/docs/sample/05-my-matcher/MyMatcher.php +++ b/docs/sample/05-my-matcher/MyMatcher.php @@ -13,7 +13,7 @@ class MyMatcher extends AbstractMatcher */ public function contains($contain) { - $this->createMatcher(__FUNCTION__, $contain); + $this->createMatcher(__FUNCTION__, [$contain]); return clone $this; diff --git a/src/AbstractMatcher.php b/src/AbstractMatcher.php index 684fef0b..263dfd74 100644 --- a/src/AbstractMatcher.php +++ b/src/AbstractMatcher.php @@ -38,12 +38,12 @@ abstract class AbstractMatcher /** - * @param string $method - * @param null|string $args + * @param string $method + * @param array $args * * @return AbstractMatcher */ - protected function createMatcher($method, $args) + protected function createMatcher($method, array $args) { $this->method = $method; $this->args = $args; @@ -62,7 +62,9 @@ protected function createMatcher($method, $args) public function __invoke($class, $target) { $args = array_merge([$class, $target], $this->args); - $matched = call_user_func_array([new Match, 'is' . $this->method], $args); + $method = 'is' . $this->method; + $match = method_exists($this, $method) ? $this : new Match; + $matched = call_user_func_array([$match, $method], $args); return $matched; } From 958d9030e9c477e316760113b4769a77c2edd9e0 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 12:52:24 +0900 Subject: [PATCH 06/10] fix phpdoc --- src/Match.php | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Match.php b/src/Match.php index 7789bebd..8ae6c782 100644 --- a/src/Match.php +++ b/src/Match.php @@ -115,22 +115,22 @@ public function isAny($name, $target) * Return Match object if annotate bindings, which containing multiple results. * Otherwise return bool. * - * @param mixed $class string(class name) | ReflectionMethod + * @param mixed $name string(class name) | ReflectionMethod * @param bool $target AbstractMatcher::TARGET_CLASS | AbstractMatcher::TARGET_METHOD * @param string $annotationName * * @return bool | Matched[] * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ - public function isAnnotatedWith($class, $target, $annotationName) + public function isAnnotatedWith($name, $target, $annotationName) { - if ($class instanceof \ReflectionMethod) { - return $this->isAnnotatedMethod($target, $class, $annotationName); + if ($name instanceof \ReflectionMethod) { + return $this->isAnnotatedMethod($target, $name, $annotationName); } if ($target !== AbstractMatcher::TARGET_CLASS) { - return $this->setAnnotations($class, $annotationName); + return $this->setAnnotations($name, $annotationName); } - $annotation = $this->reader->getClassAnnotation(new ReflectionClass($class), $annotationName); + $annotation = $this->reader->getClassAnnotation(new ReflectionClass($name), $annotationName); $hasAnnotation = $annotation ? true : false; return $hasAnnotation; @@ -221,7 +221,7 @@ public function isSubclassesOf($class, $target, $superClass) /** * Return prefix match * - * @param string $name + * @param mixed $name string (class name) or ReflectionMethod * @param string $target * @param string $startsWith * @@ -242,7 +242,7 @@ public function isStartsWith($name, $target, $startsWith) /** * Return logical or matching result * - * @param string $name + * @param mixed $name * @param bool $target * @param Matchable $matcherA * @param Matchable $matcherB @@ -269,7 +269,7 @@ public function isLogicalOr($name, $target, Matchable $matcherA, Matchable $matc /** * Return logical and matching result * - * @param string $name + * @param mixed $name * @param bool $target * @param Matchable $matcherA * @param Matchable $matcherB @@ -294,7 +294,7 @@ public function isLogicalAnd($name, $target, Matchable $matcherA, Matchable $mat /** * Return logical xor matching result * - * @param string $name + * @param mixed $name * @param bool $target * @param Matchable $matcherA * @param Matchable $matcherB @@ -319,7 +319,7 @@ public function isLogicalXor($name, $target, Matchable $matcherA, Matchable $mat /** * Return logical not matching result * - * @param string $name + * @param mixed $name * @param bool $target * @param Matchable $matcher * @@ -331,6 +331,4 @@ public function isLogicalNot($name, $target, Matchable $matcher) return $isNot; } - - -} \ No newline at end of file +} From debfdebe820449a59394bdd2bbd67a27778a8a34 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 13:07:26 +0900 Subject: [PATCH 07/10] remove Matcher constructor --- src/Matcher.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/Matcher.php b/src/Matcher.php index bdec4e15..85362a59 100644 --- a/src/Matcher.php +++ b/src/Matcher.php @@ -16,13 +16,6 @@ class Matcher extends AbstractMatcher implements Matchable { - /** - * @param Reader $reader - */ - public function __construct(Reader $reader = null) - { - } - /** * {@inheritdoc} */ @@ -122,6 +115,4 @@ public function isAnnotateBinding() return $isAnnotateBinding; } - - } From 0d9f6b2a165f61c1b78eea5669ff492be611013d Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 13:09:27 +0900 Subject: [PATCH 08/10] remove unused method --- src/Match.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Match.php b/src/Match.php index 8ae6c782..02ac6a3e 100644 --- a/src/Match.php +++ b/src/Match.php @@ -72,18 +72,6 @@ public function __construct(Reader $reader = null) $this->reader = self::$annotationReader; } - /** - * Return isAnnotateBinding - * - * @return bool - */ - public function isAnnotateBinding() - { - $isAnnotateBinding = $this->method === 'annotatedWith'; - - return $isAnnotateBinding; - } - /** * Return isAny * From c282fd521b91682e54f7589aadb0b224f1af57f1 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 13:19:14 +0900 Subject: [PATCH 09/10] fix phpdocs --- src/Bind.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Bind.php b/src/Bind.php index 5338b4d1..fc3039ae 100644 --- a/src/Bind.php +++ b/src/Bind.php @@ -111,7 +111,7 @@ public function __toString() * * @param string $class * @param AbstractMatcher $methodMatcher - * @param array $interceptors + * @param Interceptor[] $interceptors */ private function methodMatchBind($class, AbstractMatcher $methodMatcher, array $interceptors) { @@ -127,9 +127,9 @@ private function methodMatchBind($class, AbstractMatcher $methodMatcher, array $ /** * Bind interceptor by annotation binding * - * @param string $class - * @param Matcher $methodMatcher - * @param array $interceptors + * @param string $class + * @param Matcher $methodMatcher + * @param Interceptor[] $interceptors */ private function bindByAnnotateBinding($class, AbstractMatcher $methodMatcher, array $interceptors) { From 748bd61efc246592135f7fabae8ef90fe6ba8826 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Thu, 14 Aug 2014 13:19:24 +0900 Subject: [PATCH 10/10] remove unused use statements --- src/Matcher.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Matcher.php b/src/Matcher.php index 85362a59..9416c469 100644 --- a/src/Matcher.php +++ b/src/Matcher.php @@ -6,13 +6,7 @@ */ namespace Ray\Aop; -use Doctrine\Common\Annotations\AnnotationReader; -use Doctrine\Common\Annotations\CachedReader; -use Doctrine\Common\Annotations\FileCacheReader; -use Doctrine\Common\Annotations\Reader; use Ray\Aop\Exception\InvalidAnnotation; -use Ray\Aop\Exception\InvalidArgument as InvalidArgumentException; -use ReflectionClass; class Matcher extends AbstractMatcher implements Matchable {