-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67: Added custom dependency injection handler in …
…Kernel
- Loading branch information
Showing
15 changed files
with
320 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Component; | ||
|
||
enum ComponentType | ||
{ | ||
case TRANSFORMER; | ||
case ASPECT; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Core\Container; | ||
|
||
use Okapi\Aop\Component\ComponentType; | ||
use Okapi\CodeTransformer\Core\Container\TransformerManager as CodeTransformerTransformerManager; | ||
|
||
class TransformerManager extends CodeTransformerTransformerManager | ||
{ | ||
protected function getAdditionalDependencyInjectionParams(): array | ||
{ | ||
return [ComponentType::TRANSFORMER]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/Functional/Kernel/CustomDependencyInjectionHandler/Aspect.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Functional\Kernel\CustomDependencyInjectionHandler; | ||
|
||
use Okapi\Aop\Attributes\After; | ||
use Okapi\Aop\Attributes\Aspect as AspectAttribute; | ||
|
||
#[AspectAttribute] | ||
class Aspect | ||
{ | ||
#[After( | ||
class: Target::class, | ||
method: 'answer', | ||
)] | ||
public function higherAnswer(): int | ||
{ | ||
return 420; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...nctional/Kernel/CustomDependencyInjectionHandler/CustomDependencyInjectionHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Functional\Kernel\CustomDependencyInjectionHandler; | ||
|
||
use Okapi\Aop\Tests\Util; | ||
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[RunTestsInSeparateProcesses] | ||
class CustomDependencyInjectionHandlerTest extends TestCase | ||
{ | ||
public function testCustomDependencyInjectionHandler(): void | ||
{ | ||
Util::clearCache(); | ||
|
||
ob_start(); | ||
|
||
Kernel::init(); | ||
|
||
$output = ob_get_clean(); | ||
|
||
$this->assertStringContainsString( | ||
'Generating aspect/transformer instance: ' . Aspect::class, | ||
$output, | ||
); | ||
|
||
$class = new Target(); | ||
|
||
$this->assertSame( | ||
420, | ||
$class->answer(), | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/Functional/Kernel/CustomDependencyInjectionHandler/Kernel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Functional\Kernel\CustomDependencyInjectionHandler; | ||
|
||
use Closure; | ||
use Okapi\Aop\AopKernel; | ||
use Okapi\Aop\Tests\Util; | ||
|
||
class Kernel extends AopKernel | ||
{ | ||
protected ?string $cacheDir = Util::CACHE_DIR; | ||
|
||
protected function dependencyInjectionHandler(): ?Closure | ||
{ | ||
return function (string $className) { | ||
echo 'Generating aspect/transformer instance: ' . $className . PHP_EOL; | ||
|
||
return new $className(); | ||
}; | ||
} | ||
|
||
protected array $aspects = [ | ||
Aspect::class, | ||
]; | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Functional/Kernel/CustomDependencyInjectionHandler/Target.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Functional\Kernel\CustomDependencyInjectionHandler; | ||
|
||
class Target | ||
{ | ||
public function answer(): int | ||
{ | ||
return 42; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/Integration/TransformerAndAspectDependencyInjectionHandler/Aspect.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** @noinspection PhpUnused */ | ||
namespace Okapi\Aop\Tests\Integration\TransformerAndAspectDependencyInjectionHandler; | ||
|
||
use Okapi\Aop\Attributes\After; | ||
use Okapi\Aop\Attributes\Aspect as AspectAttribute; | ||
use Okapi\Aop\Invocation\AfterMethodInvocation; | ||
|
||
#[AspectAttribute] | ||
class Aspect | ||
{ | ||
#[After( | ||
class: Target::class, | ||
method: 'answer', | ||
)] | ||
public function higherAnswer(AfterMethodInvocation $methodInvocation): int|float | ||
{ | ||
$result = $methodInvocation->proceed(); | ||
|
||
return $result + 378; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/Integration/TransformerAndAspectDependencyInjectionHandler/Kernel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Integration\TransformerAndAspectDependencyInjectionHandler; | ||
|
||
use Closure; | ||
use Okapi\Aop\AopKernel; | ||
use Okapi\Aop\Component\ComponentType; | ||
use Okapi\Aop\Tests\Util; | ||
|
||
class Kernel extends AopKernel | ||
{ | ||
protected ?string $cacheDir = Util::CACHE_DIR; | ||
|
||
protected function dependencyInjectionHandler(): ?Closure | ||
{ | ||
return function (string $className, ComponentType $type) { | ||
/** @noinspection PhpIfWithCommonPartsInspection */ | ||
if ($type === ComponentType::ASPECT) { | ||
echo 'Generating aspect instance: ' . $className . PHP_EOL; | ||
|
||
return new $className(); | ||
} else { | ||
echo 'Generating transformer instance: ' . $className . PHP_EOL; | ||
|
||
return new $className(); | ||
} | ||
}; | ||
} | ||
|
||
protected array $aspects = [ | ||
Aspect::class, | ||
]; | ||
|
||
protected array $transformers = [ | ||
Transformer::class, | ||
]; | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Integration/TransformerAndAspectDependencyInjectionHandler/Target.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Integration\TransformerAndAspectDependencyInjectionHandler; | ||
|
||
class Target | ||
{ | ||
public function answer(): int|float | ||
{ | ||
return 42; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
tests/Integration/TransformerAndAspectDependencyInjectionHandler/Transformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Okapi\Aop\Tests\Integration\TransformerAndAspectDependencyInjectionHandler; | ||
|
||
use Microsoft\PhpParser\Node\DelimitedList\QualifiedNameList; | ||
use Microsoft\PhpParser\Node\MethodDeclaration; | ||
use Microsoft\PhpParser\Node\NumericLiteral; | ||
use Okapi\CodeTransformer\Transformer as TransformerClass; | ||
use Okapi\CodeTransformer\Transformer\Code; | ||
|
||
class Transformer extends TransformerClass | ||
{ | ||
public function getTargetClass(): string|array | ||
{ | ||
return Target::class; | ||
} | ||
|
||
/** @noinspection PhpPossiblePolymorphicInvocationInspection */ | ||
public function transform(Code $code): void | ||
{ | ||
$sourceFileNode = $code->getSourceFileNode(); | ||
|
||
foreach ($sourceFileNode->getDescendantNodes() as $node) { | ||
if ($node instanceof QualifiedNameList | ||
&& $node->getFirstAncestor(MethodDeclaration::class)?->getName() === 'answer' | ||
) { | ||
$code->edit($node, 'int|float'); | ||
} | ||
|
||
if ($node instanceof NumericLiteral | ||
&& $node->getFirstAncestor(MethodDeclaration::class)?->getName() === 'answer' | ||
) { | ||
$text = $node->getText(); | ||
$code->edit($node, "$text.69"); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.