Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCelavi committed Aug 15, 2017
1 parent eb3b72f commit de1d6cf
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/Exception/CircularWeavingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\Exception;

class CircularWeavingException extends \RuntimeException implements ExceptionInterface
{

}
16 changes: 16 additions & 0 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\Exception;

interface ExceptionInterface
{

}
4 changes: 4 additions & 0 deletions src/Instrument/ClassLoading/CacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Go\Instrument\ClassLoading;

use Go\Core\AspectKernel;
use Go\Exception\CircularWeavingException;
use Go\Instrument\FileSystem\Enumerator;
use Go\Instrument\Transformer\FilterInjectorTransformer;
use Symfony\Component\Console\Output\NullOutput;
Expand Down Expand Up @@ -83,6 +84,9 @@ public function warmUp()
);

$this->output->writeln(sprintf('<fg=green;options=bold>[OK]</>: <comment>%s</comment>', $path));
} catch (CircularWeavingException $e) {
$this->output->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: File "%s" is not processed correctly due to detected circular weaving.', $path));
throw $e;
} catch (\Throwable $e) {
$displayException($e, $path);
} catch (\Exception $e) {
Expand Down
16 changes: 16 additions & 0 deletions src/Instrument/ClassLoading/SourceTransformingLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Go\Instrument\ClassLoading;

use Go\Exception\CircularWeavingException;
use php_user_filter as PhpStreamFilter;
use Go\Instrument\Transformer\StreamMetaData;
use Go\Instrument\Transformer\SourceTransformer;
Expand Down Expand Up @@ -50,6 +51,13 @@ class SourceTransformingLoader extends PhpStreamFilter
*/
protected static $filterId;

/**
* Transformation log
*
* @var string
*/
protected static $log = [];

/**
* Register current loader as stream filter in PHP
*
Expand Down Expand Up @@ -130,13 +138,21 @@ public static function addTransformer(SourceTransformer $transformer)
public static function transformCode(StreamMetaData $metadata)
{
$result = null;

foreach (self::$transformers as $transformer) {
$result = $transformer->transform($metadata);
if ($result === false) {
break;
}
}

if ($result) {
if (isset(self::$log[$metadata->uri])) {
throw new CircularWeavingException(sprintf('Circular weaving detected at file: "%s".', $metadata->uri));
}
self::$log[$metadata->uri] = true;
}

return $result;
}
}
16 changes: 16 additions & 0 deletions tests/Fixtures/project/src/Application/CircularyWeaved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Go\Tests\TestProject\Application;

use Go\Tests\TestProject\Annotation as Aop;

class CircularyWeaved
{
/**
* @Aop\Loggable()
*/
public function youCanNotWeaveMe()
{

}
}
30 changes: 30 additions & 0 deletions tests/Fixtures/project/src/Aspect/CircularWeavingAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Go\Tests\TestProject\Aspect;

use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Tests\TestProject\Application\CircularyWeaved;
use Go\Lang\Annotation as Pointcut;

class CircularWeavingAspect implements Aspect
{
private $circularyWeaved;

public function __construct(CircularyWeaved $circularyWeaved)
{
$this->circularyWeaved = $circularyWeaved;
}

/**
* Intercepts doSomething()
*
* @param MethodInvocation $invocation
*
* @Pointcut\After("execution(public Go\Tests\TestProject\Application\CircularyWeaved->youCanNotWeaveMe(*))")
*/
public function failToIntercept(MethodInvocation $invocation)
{
$this->circularyWeaved->youCanNotWeaveMe();
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/project/src/Kernel/CircularWeavingAspectKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Go\Tests\TestProject\Kernel;

use Go\Core\AspectContainer;
use Go\Core\AspectKernel;
use Go\Tests\TestProject\Application\CircularyWeaved;
use Go\Tests\TestProject\Aspect\CircularWeavingAspect;
use Go\Tests\TestProject\Aspect\LoggingAspect;
use Psr\Log\NullLogger;

class CircularWeavingAspectKernel extends AspectKernel
{
/**
* {@inheritdoc}
*/
protected function configureAop(AspectContainer $container)
{
$container->registerAspect(new LoggingAspect(new NullLogger()));
$container->registerAspect(new CircularWeavingAspect(new CircularyWeaved()));
}
}
6 changes: 1 addition & 5 deletions tests/Fixtures/project/src/Kernel/DefaultAspectKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
class DefaultAspectKernel extends AspectKernel
{
/**
* Configure an AspectContainer with advisors, aspects and pointcuts
*
* @param AspectContainer $container
*
* @return void
* {@inheritdoc}
*/
protected function configureAop(AspectContainer $container)
{
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/project/web/configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@
__DIR__ . '/../src/'
)
),
'circular_weaving' => array(
'kernel' => \Go\Tests\TestProject\Kernel\CircularWeavingAspectKernel::class,
'appDir' => __DIR__ . '/../',
'debug' => true,
'cacheDir' => __DIR__ . '/../var/cache/aspect',
'includePaths' => array(
__DIR__ . '/../src/'
)
),
);
1 change: 1 addition & 0 deletions tests/Go/Console/Command/DebugAdvisorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DebugAdvisorCommandTest extends BaseFunctionalTest
{
public function setUp()
{
self::clearCache();
self::warmUp();
}

Expand Down
1 change: 1 addition & 0 deletions tests/Go/Console/Command/DebugAspectCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class DebugAspectCommandTest extends BaseFunctionalTest
{
public function setUp()
{
self::clearCache();
self::warmUp();
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Go/Functional/BaseFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected static function warmUp($configuration = null)
return self::exec('cache:warmup:aop', '', $configuration);
}

protected static function exec($command, $args = '', $configuration = null)
protected static function exec($command, $args = '', $configuration = null, $success = true)
{
$configuration = ($configuration) ? sprintf('GO_AOP_CONFIGURATION=%s ', $configuration) : '';

Expand All @@ -43,7 +43,9 @@ protected static function exec($command, $args = '', $configuration = null)

$process->run();

self::assertTrue($process->isSuccessful(), sprintf('Unable to execute "%s" command, got output: "%s".', $command, $process->getOutput()));
$assert = ($success) ? 'assertTrue' : 'assertFalse';

self::{$assert}($process->isSuccessful(), sprintf('Unable to execute "%s" command, got output: "%s".', $command, $process->getOutput()));

return $process->getOutput();
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Go/Functional/CircularWeavingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Go\Functional;

class CircularWeavingTest extends BaseFunctionalTest
{
public function testCircularWeaving()
{
self::clearCache();
$output = self::exec('cache:warmup:aop', '', 'circular_weaving', false);
$this->assertContains('is not processed correctly due to detected circular weaving.', $output);
}
}
1 change: 1 addition & 0 deletions tests/Go/Functional/Issue293Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Issue293Test extends BaseFunctionalTest
{
public function setUp()
{
self::clearCache();
self::warmUp();
}

Expand Down
1 change: 1 addition & 0 deletions tests/Go/Functional/MethodWeavingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class MethodWeavingTest extends BaseFunctionalTest
{
public function setUp()
{
self::clearCache();
self::warmUp();
}

Expand Down

0 comments on commit de1d6cf

Please sign in to comment.