Skip to content

Commit

Permalink
Merge branch 'feature/refactor' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Aug 14, 2014
2 parents 39c4a5c + 748bd61 commit 4c0469a
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 102 deletions.
6 changes: 3 additions & 3 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WeekendBlocker implements MethodInterceptor
```php
<?php
$bind = new Bind;
$matcher = new Matcher(new Reader);
$matcher = new Matcher;
$interceptors = [new WeekendBlocker];
$pointcut = new Pointcut(
$matcher->any(),
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 4 additions & 5 deletions docs/sample/01-quick-weave/main.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

namespace Ray\Aop\Sample;
namespace Ray\Aop;

require dirname(__DIR__) . '/bootstrap.php';
use Ray\Aop\Sample\WeekendBlocker;

use Ray\Aop\Bind;
require dirname(__DIR__) . '/bootstrap.php';

$bind = (new Bind)->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 {
Expand Down
8 changes: 5 additions & 3 deletions docs/sample/02-multiple-interceptors/main.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Ray\Aop\Sample;
namespace Ray\Aop;

require dirname(__DIR__) . '/bootstrap.php';

use Ray\Aop\Bind;
use Ray\Aop\Sample\Timer;
use Ray\Aop\Sample\interceptorA;
use Ray\Aop\Sample\interceptorB;

$bind = (new Bind)->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);

Expand Down
7 changes: 4 additions & 3 deletions docs/sample/03-benchmark/main.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace Ray\Aop\Sample;
namespace Ray\Aop;

require dirname(__DIR__) . '/bootstrap.php';

use Ray\Aop\Bind;
use Ray\Aop\Sample\EmptyInterceptor;
use Ray\Aop\Sample\RealBillingService;

$bind1 = new Bind;
$bind1->bindInterceptors('chargeOrder', array());
Expand All @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions docs/sample/04-annotation/main.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
<?php
namespace Ray\Aop\Sample;

use Ray\Aop\Pointcut;
use Ray\Aop\Matcher;
use Ray\Aop\Bind;
namespace Ray\Aop;

use Ray\Aop\Sample\WeekendBlocker;

require dirname(__DIR__) . '/bootstrap.php';

use Doctrine\Common\Annotations\AnnotationReader as Reader;

$matcher = new Matcher(new Reader);
$matcher = new Matcher;
$interceptors = [new WeekendBlocker];
$pointcut = new Pointcut(
$matcher->any(),
$matcher->annotatedWith('Ray\Aop\Sample\Annotation\WeekendBlock'),
$interceptors
);
$bind = (new Bind)->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();
Expand Down
2 changes: 1 addition & 1 deletion docs/sample/05-my-matcher/MyMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyMatcher extends AbstractMatcher
*/
public function contains($contain)
{
$this->createMatcher(__FUNCTION__, $contain);
$this->createMatcher(__FUNCTION__, [$contain]);

return clone $this;

Expand Down
13 changes: 5 additions & 8 deletions docs/sample/05-my-matcher/main.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<?php
namespace Ray\Aop\Sample;
namespace Ray\Aop;

use Ray\Aop\Pointcut;
use Ray\Aop\Matcher;
use Ray\Aop\Bind;
use Ray\Aop\Sample\WeekendBlocker;
use Ray\Aop\Sample\MyMatcher;

require dirname(__DIR__) . '/bootstrap.php';

use Doctrine\Common\Annotations\AnnotationReader as Reader;

$matcher = new Matcher(new Reader);
$matcher = new Matcher;
$myMatcher = new MyMatcher;
$interceptors = [new WeekendBlocker];
$pointcut = new Pointcut(
Expand All @@ -18,7 +15,7 @@
$interceptors
);
$bind = (new Bind)->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();
Expand Down
14 changes: 5 additions & 9 deletions src/AbstractMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -61,13 +61,9 @@ 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;
}
$args = array_merge([$class, $target], $this->args);
$method = 'is' . $this->method;
$match = new Match;
$match = method_exists($this, $method) ? $this : new Match;
$matched = call_user_func_array([$match, $method], $args);

return $matched;
Expand Down
8 changes: 4 additions & 4 deletions src/Bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPParser_Lexer;
use Serializable;
use ReflectionParameter;
use PHPParser_PrettyPrinter_Default;

/**
* AOP compiler
Expand Down Expand Up @@ -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;
}

/**
Expand Down
38 changes: 12 additions & 26 deletions src/Match.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -115,22 +103,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;
Expand Down Expand Up @@ -221,7 +209,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
*
Expand All @@ -242,7 +230,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
Expand All @@ -269,7 +257,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
Expand All @@ -294,7 +282,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
Expand All @@ -319,7 +307,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
*
Expand All @@ -331,6 +319,4 @@ public function isLogicalNot($name, $target, Matchable $matcher)

return $isNot;
}


}
}
Loading

0 comments on commit 4c0469a

Please sign in to comment.