Skip to content

Commit c7a5d63

Browse files
committed
Change DI approaching
1 parent 2d9614f commit c7a5d63

4 files changed

Lines changed: 66 additions & 24 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: 5 additions & 12 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,18 +61,10 @@ 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'))) {
69-
array_unshift(
70-
$arguments,
71-
$this->getConstantPool(),
72-
$this->javaClass
73-
);
74-
}
64+
array_unshift(
65+
$arguments,
66+
...$this->getNativeAnnotateInjections($document)
67+
);
7568
}
7669

7770
$return = forward_static_call_array(

src/Kernel/Mnemonics/_invokevirtual.php

Lines changed: 5 additions & 12 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,18 +55,10 @@ 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'))) {
63-
array_unshift(
64-
$arguments,
65-
$this->getConstantPool(),
66-
$this->javaClass
67-
);
68-
}
58+
array_unshift(
59+
$arguments,
60+
...$this->getNativeAnnotateInjections($document)
61+
);
6962
}
7063

7164
$result = call_user_func_array(

0 commit comments

Comments
 (0)