Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter the PointcutInterface::matchesMethod to include the ClassReflectio... #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Aop/InterceptorLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,34 @@ class InterceptorLoader implements InterceptorLoaderInterface
private $interceptors;
private $loadedInterceptors = array();

/**
* @param ContainerInterface $container
* @param array $interceptors
*/
public function __construct(ContainerInterface $container, array $interceptors)
{
$this->container = $container;
$this->interceptors = $interceptors;
}

public function loadInterceptors(\ReflectionMethod $method)
/**
* {@inheritdoc}
*/
public function loadInterceptors(\ReflectionClass $class, \ReflectionMethod $method)
{
if (!isset($this->interceptors[$method->class][$method->name])) {
if (!isset($this->interceptors[$class->name][$method->name])) {
return array();
}

if (isset($this->loadedInterceptors[$method->class][$method->name])) {
return $this->loadedInterceptors[$method->class][$method->name];
if (isset($this->loadedInterceptors[$class->name][$method->name])) {
return $this->loadedInterceptors[$class->name][$method->name];
}

$interceptors = array();
foreach ($this->interceptors[$method->class][$method->name] as $id) {
foreach ($this->interceptors[$class->name][$method->name] as $id) {
$interceptors[] = $this->container->get($id);
}

return $this->loadedInterceptors[$method->class][$method->name] = $interceptors;
return $this->loadedInterceptors[$class->name][$method->name] = $interceptors;
}
}
5 changes: 4 additions & 1 deletion Aop/PointcutInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface PointcutInterface
* annotations.
*
* @param \ReflectionClass $class
*
* @return boolean
*/
function matchesClass(\ReflectionClass $class);
Expand All @@ -47,8 +48,10 @@ function matchesClass(\ReflectionClass $class);
* This method is not limited in the way the matchesClass method is. It may
* use information in the associated class to make its decision.
*
* @param \ReflectionClass $class
* @param \ReflectionMethod $method
*
* @return boolean
*/
function matchesMethod(\ReflectionMethod $method);
function matchesMethod(\ReflectionClass $class, \ReflectionMethod $method);
}
11 changes: 10 additions & 1 deletion Aop/RegexPointcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,26 @@ class RegexPointcut implements PointcutInterface
{
private $pattern;

/**
* @param string $pattern
*/
public function __construct($pattern)
{
$this->pattern = $pattern;
}

/**
* {@inheritdoc}
*/
public function matchesClass(\ReflectionClass $class)
{
return true;
}

public function matchesMethod(\ReflectionMethod $method)
/**
* {@inheritdoc}
*/
public function matchesMethod(\ReflectionClass $class, \ReflectionMethod $method)
{
return 0 < preg_match('#'.$this->pattern.'#', sprintf('%s::%s', $method->class, $method->name));
}
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CHANGELOG
=========

* 2012-08-20

* [BC BREAK] alter the ``PointcutInterface::matchedMethod`` signature to pass the ReflectionClass instance
3 changes: 2 additions & 1 deletion DependencyInjection/Compiler/PointcutMatchingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ private function processDefinition(Definition $definition, $pointcuts, &$interce
}

$advices = array();

foreach ($matchingPointcuts as $interceptor => $pointcut) {
if ($pointcut->matchesMethod($method)) {
if ($pointcut->matchesMethod($class, $method)) {
$advices[] = $interceptor;
}
}
Expand Down
5 changes: 3 additions & 2 deletions Tests/Aop/RegexPointcutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public function testMatchesMethod()
{
$pointcut = new RegexPointcut('foo$');

$class = new \ReflectionClass('JMS\AopBundle\Tests\Aop\RegexPointcutTestClass');
$method = new \ReflectionMethod('JMS\AopBundle\Tests\Aop\RegexPointcutTestClass', 'foo');
$this->assertTrue($pointcut->matchesMethod($method));
$this->assertTrue($pointcut->matchesMethod($class, $method));

$method = new \ReflectionMethod('JMS\AopBundle\Tests\Aop\RegexPointcutTestClass', 'bar');
$this->assertFalse($pointcut->matchesMethod($method));
$this->assertFalse($pointcut->matchesMethod($class, $method));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@

class LoggingPointcut implements PointcutInterface
{

public function matchesClass(\ReflectionClass $class)
{
return true;
}

public function matchesMethod(\ReflectionMethod $method)
public function matchesMethod(\ReflectionClass $class, \ReflectionMethod $method)
{
return false !== strpos($method->name, 'delete');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
use JMS\AopBundle\DependencyInjection\JMSAopExtension;
use JMS\AopBundle\DependencyInjection\Compiler\PointcutMatchingPass;
use Symfony\Component\HttpKernel\Util\Filesystem;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class PointcutMatchingPassTest extends \PHPUnit_Framework_TestCase
Expand Down