Skip to content

Commit 18713ab

Browse files
authored
Merge pull request #106 from php-java/change-di-approaching
Change DI behavior.
2 parents 2d9614f + 9ea2db4 commit 18713ab

4 files changed

Lines changed: 64 additions & 16 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Exceptions;
3+
4+
class NotSupportedInjectionTypesException extends \Exception
5+
{
6+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace PHPJava\Kernel\Core;
3+
4+
use PHPJava\Exceptions\NotSupportedInjectionTypesException;
5+
6+
trait DependencyInjector
7+
{
8+
public function getNativeAnnotateInjections($phpDocument): array
9+
{
10+
$documentBlock = \phpDocumentor\Reflection\DocBlockFactory::createInstance()
11+
->create($phpDocument);
12+
13+
// Native annotation will dependency inject.
14+
if (!empty($documentBlock->getTagsByName('native'))) {
15+
$injections = [];
16+
foreach ($documentBlock->getTagsByName('native') as $nativeClass) {
17+
/**
18+
* @var \phpDocumentor\Reflection\DocBlock\Tags\Generic $nativeClass
19+
*/
20+
switch ($type = trim($nativeClass->getDescription())) {
21+
case 'ConstantPool':
22+
$injections[] = $this->getConstantPool();
23+
break;
24+
case 'JavaClass':
25+
$injections[] = $this->javaClass;
26+
break;
27+
case 'JavaClassInvoker':
28+
$injections[] = $this->javaClassInvoker;
29+
break;
30+
case 'Attributes':
31+
$injections[] = $this->getAttributes();
32+
break;
33+
case 'OperandStacks':
34+
$injections[] = $this->getStacks();
35+
break;
36+
case 'LocalStorages':
37+
$injections[] = $this->getLocalStorages();
38+
break;
39+
case 'Options':
40+
$injections[] = $this->options;
41+
break;
42+
default:
43+
throw new NotSupportedInjectionTypesException('Not supported injection type: "' . $type . '"');
44+
}
45+
}
46+
return $injections;
47+
}
48+
return [];
49+
}
50+
}

src/Kernel/Mnemonics/_invokestatic.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class _invokestatic implements OperationInterface
1616
{
1717
use \PHPJava\Kernel\Core\Accumulator;
1818
use \PHPJava\Kernel\Core\ConstantPool;
19+
use \PHPJava\Kernel\Core\DependencyInjector;
1920

2021
public function execute(): void
2122
{
@@ -60,16 +61,11 @@ public function execute(): void
6061
$methodAccessor = $reflectionClass->getMethod("{$prefix}{$methodName}");
6162

6263
if ($document = $methodAccessor->getDocComment()) {
63-
// parse PHPDoc
64-
$documentBlock = \phpDocumentor\Reflection\DocBlockFactory::createInstance()
65-
->create($document);
66-
67-
// Native annotation will dependency inject.
68-
if (!empty($documentBlock->getTagsByName('native'))) {
64+
$prependInjections = $this->getNativeAnnotateInjections($document);
65+
if (!empty($prependInjections)) {
6966
array_unshift(
7067
$arguments,
71-
$this->getConstantPool(),
72-
$this->javaClass
68+
...$prependInjections
7369
);
7470
}
7571
}

src/Kernel/Mnemonics/_invokevirtual.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class _invokevirtual implements OperationInterface
1717
{
1818
use \PHPJava\Kernel\Core\Accumulator;
1919
use \PHPJava\Kernel\Core\ConstantPool;
20+
use \PHPJava\Kernel\Core\DependencyInjector;
2021

2122
public function execute(): void
2223
{
@@ -54,16 +55,11 @@ public function execute(): void
5455
$methodAccessor = $reflectionClass->getMethod($methodName);
5556

5657
if ($document = $methodAccessor->getDocComment()) {
57-
// parse PHPDoc
58-
$documentBlock = \phpDocumentor\Reflection\DocBlockFactory::createInstance()
59-
->create($document);
60-
61-
// Native annotation will dependency inject.
62-
if (!empty($documentBlock->getTagsByName('native'))) {
58+
$prependInjections = $this->getNativeAnnotateInjections($document);
59+
if (!empty($prependInjections)) {
6360
array_unshift(
6461
$arguments,
65-
$this->getConstantPool(),
66-
$this->javaClass
62+
...$prependInjections
6763
);
6864
}
6965
}

0 commit comments

Comments
 (0)