Skip to content

Commit

Permalink
Merge pull request #100 from inhere/master
Browse files Browse the repository at this point in the history
add getter method to get aop data info.
  • Loading branch information
inhere authored Apr 10, 2018
2 parents 0114445 + 56e4622 commit 17a7ba0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
45 changes: 32 additions & 13 deletions src/Aop/Aop.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ public function init()
*
* @param object $target Origin object
* @param string $method The execution method
* @param array $params The parameters of execution method
* @param array $params The parameters of execution method
* @return mixed
* @throws \ReflectionException
* @throws Throwable
*/
public function execute($target, string $method, array $params)
{
$class = \get_class($target);

// If doesn't have any advices, then execute the origin method
if (! isset($this->map[$class][$method]) || empty($this->map[$class][$method])) {
if (!isset($this->map[$class][$method]) || empty($this->map[$class][$method])) {
return $target->$method(...$params);
}

Expand All @@ -71,7 +73,7 @@ public function execute($target, string $method, array $params)
public function doAdvice($target, string $method, array $params, array $advices)
{
$result = null;
$advice = array_shift($advices);
$advice = \array_shift($advices);

try {

Expand All @@ -98,9 +100,9 @@ public function doAdvice($target, string $method, array $params, array $advices)
} catch (Throwable $t) {
if (isset($advice['afterThrowing']) && ! empty($advice['afterThrowing'])) {
return $this->doPoint($advice['afterThrowing'], $target, $method, $params, $advice, $advices, null, $t);
} else {
throw $t;
}

throw $t;
}

// afterReturning
Expand Down Expand Up @@ -162,9 +164,9 @@ private function doPoint(
$aspectArgs[] = new ProceedingJoinPoint($target, $method, $args, $advice, $advices);
continue;
}
//Throwable object
if (isset($catch) && $catch instanceof $type) {

// Throwable object
if ($catch && $catch instanceof $type) {
$aspectArgs[] = $catch;
continue;
}
Expand All @@ -187,7 +189,7 @@ private function doPoint(
public function match(string $beanName, string $class, string $method, array $annotations)
{
foreach ($this->aspects as $aspectClass => $aspect) {
if (! isset($aspect['point']) || ! isset($aspect['advice'])) {
if (!isset($aspect['point'], $aspect['advice'])) {
continue;
}

Expand Down Expand Up @@ -218,7 +220,8 @@ public function match(string $beanName, string $class, string $method, array $an
*/
public function register(array $aspects)
{
array_multisort(array_column($aspects, 'order'), SORT_ASC, $aspects);
$temp = \array_column($aspects, 'order');
\array_multisort($temp, SORT_ASC, $aspects);
$this->aspects = $aspects;
}

Expand All @@ -231,7 +234,7 @@ public function register(array $aspects)
*/
private function matchBeanAndAnnotation(array $pointAry, array $classAry): bool
{
$intersectAry = array_intersect($pointAry, $classAry);
$intersectAry = \array_intersect($pointAry, $classAry);
if (empty($intersectAry)) {
return false;
}
Expand All @@ -250,7 +253,7 @@ private function matchBeanAndAnnotation(array $pointAry, array $classAry): bool
private function matchExecution(string $class, string $method, array $executions): bool
{
foreach ($executions as $execution) {
$executionAry = explode('::', $execution);
$executionAry = \explode('::', $execution);
if (\count($executionAry) < 2) {
continue;
}
Expand All @@ -263,11 +266,27 @@ private function matchExecution(string $class, string $method, array $executions

// Method
$reg = '/^(?:' . $executionMethod . ')$/';
if (preg_match($reg, $method)) {
if (\preg_match($reg, $method)) {
return true;
}
}

return false;
}

/**
* @return array
*/
public function getAspects(): array
{
return $this->aspects;
}

/**
* @return array
*/
public function getMap(): array
{
return $this->map;
}
}
4 changes: 3 additions & 1 deletion src/Proxy/Handler/AopHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public function __construct($target)
* @param $parameters
* @return mixed
* @throws \ReflectionException
* @throws \Throwable
*/
public function invoke($method, $parameters)
{
/* @var Aop $aop */
$aop = bean(Aop::class);
$aop = \bean(Aop::class);

return $aop->execute($this->target, $method, $parameters);
}
}
13 changes: 6 additions & 7 deletions src/Proxy/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
class Proxy
{

/**
* Return a proxy instance
*
Expand All @@ -24,8 +23,8 @@ public static function newProxyInstance(string $className, HandlerInterface $han
$reflectionMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED);

// Proxy property
$id = uniqid('', false);
$proxyClassName = basename(str_replace("\\", '/', $className));
$id = \uniqid('', false);
$proxyClassName = \basename(str_replace("\\", '/', $className));
$proxyClassName = $proxyClassName . '_' . $id;
$handlerPropertyName = '__handler_' . $id;

Expand Down Expand Up @@ -61,7 +60,7 @@ private static function getMethodsTemplate(array $reflectionMethods, string $han
foreach ($reflectionMethods as $reflectionMethod) {
$methodName = $reflectionMethod->getName();

// not to overrided method
// not to override method
if ($reflectionMethod->isConstructor() || $reflectionMethod->isStatic()) {
continue;
}
Expand All @@ -75,11 +74,11 @@ private static function getMethodsTemplate(array $reflectionMethods, string $han
$reflectionMethodReturn = $reflectionMethod->getReturnType();
if ($reflectionMethodReturn !== null) {
$returnType = $reflectionMethodReturn->__toString();
$returnType = ($returnType == 'self') ? $reflectionMethod->getDeclaringClass()->getName() : $returnType;
$returnType = $returnType === 'self' ? $reflectionMethod->getDeclaringClass()->getName() : $returnType;
$template .= " : $returnType";
}

// overrided method
// override method
$template .= "{
return \$this->{$handlerPropertyName}->invoke('{$methodName}', func_get_args());
}
Expand Down Expand Up @@ -156,7 +155,7 @@ private static function getParameterDefaultValue(\ReflectionParameter $reflectio
$template = ' = []';
} elseif (\is_float($defaultValue)) {
$template = ' = []';
} elseif (\is_object($defaultValue) || \is_null($defaultValue)) {
} elseif (\is_object($defaultValue) || null === $defaultValue) {
$template = ' = null';
}

Expand Down

0 comments on commit 17a7ba0

Please sign in to comment.