Skip to content

Commit 8d7254b

Browse files
committed
Allows to use use statement
1 parent 4de80c2 commit 8d7254b

18 files changed

+227
-13
lines changed

src/Compiler/Lang/Assembler/AbstractAssembler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPJava\Compiler\Lang\Assembler\Traits\ConstantPoolManageable;
88
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\NamespaceManageable;
99
use PHPJava\Compiler\Lang\Assembler\Traits\EntryPointClassAssemblerManageable;
10+
use PHPJava\Compiler\Lang\Assembler\Traits\ImportManageable;
1011
use PHPJava\Compiler\Lang\Assembler\Traits\MethodAssemblerManageable;
1112
use PHPJava\Compiler\Lang\Assembler\Traits\StoreManageable;
1213
use PHPJava\Compiler\Lang\Assembler\Traits\StreamManageable;
@@ -24,6 +25,7 @@ abstract class AbstractAssembler implements AssemblerInterface, ParameterService
2425
use ClassAssemblerManageable;
2526
use EntryPointClassAssemblerManageable;
2627
use StructureAccessorsLocatorManageable;
28+
use ImportManageable;
2729

2830
/**
2931
* @var Node

src/Compiler/Lang/Assembler/ClassAssembler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPJava\Compiler\Lang\Assembler\Store\Store;
1616
use PHPJava\Compiler\Lang\Assembler\Traits\Bindable;
1717
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
18+
use PHPJava\Compiler\Lang\Assembler\Traits\ImportManageable;
1819
use PHPJava\Compiler\Lang\Assembler\Traits\OperationManageable;
1920
use PHPJava\Compiler\Lang\Assembler\Traits\ParameterParseable;
2021
use PHPJava\Compiler\Lang\Assembler\Traits\StaticInitializerAssignable;

src/Compiler/Lang/Assembler/EntryPointClassAssembler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPJava\Compiler\Lang\Assembler\Traits\ConstantPoolManageable;
2424
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
2525
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\NamespaceManageable;
26+
use PHPJava\Compiler\Lang\Assembler\Traits\ImportManageable;
2627
use PHPJava\Compiler\Lang\Assembler\Traits\MethodAssemblerManageable;
2728
use PHPJava\Compiler\Lang\Assembler\Traits\OperationManageable;
2829
use PHPJava\Compiler\Lang\Assembler\Traits\StaticInitializerAssignable;
@@ -50,6 +51,7 @@ class EntryPointClassAssembler implements AssemblerInterface, ParameterServiceIn
5051
use Bindable;
5152
use StaticInitializerAssignable;
5253
use StructureAccessorsLocatorManageable;
54+
use ImportManageable;
5355

5456
/**
5557
* @var Methods

src/Compiler/Lang/Assembler/MethodAssembler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
1414
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\LocalVariableAssignable;
1515
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\LocalVariableLoadable;
16+
use PHPJava\Compiler\Lang\Assembler\Traits\ImportManageable;
1617
use PHPJava\Compiler\Lang\Assembler\Traits\OperationManageable;
1718
use PHPJava\Compiler\Lang\Assembler\Traits\ParameterParseable;
1819
use PHPJava\Kernel\Maps\OpCode;

src/Compiler/Lang/Assembler/ParameterServiceInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPJava\Compiler\Builder\Finder\ConstantPoolFinder;
77
use PHPJava\Compiler\Lang\Assembler\Processors\AbstractProcessor;
88
use PHPJava\Compiler\Lang\Assembler\Store\Store;
9+
use PHPJava\Compiler\Lang\Assembler\Structure\Accessor\Imports;
910
use PHPJava\Compiler\Lang\Assembler\Structure\Accessor\StructureAccessorsLocator;
1011

1112
/**
@@ -21,9 +22,11 @@
2122
* @method ConstantPoolFinder getConstantPoolFinder()
2223
* @method AbstractAssembler|AbstractProcessor setMethodAssembler(MethodAssembler $methodAssembler)
2324
* @method MethodAssembler getMethodAssembler()
24-
* @method AbstractAssembler|AbstractProcessor setClassAssembler(ClassAssembler $methodAssembler)
25+
* @method AbstractAssembler|AbstractProcessor setClassAssembler(ClassAssembler $classAssembler)
2526
* @method ClassAssembler getClassAssembler()
2627
* @method StructureAccessorsLocator getStructureAccessorsLocator()
28+
* @method Imports getImportsAccessor()
29+
* @method AbstractAssembler|AbstractProcessor setImportsAccessor(Imports $importsAccessor)
2730
* @emthod EntryPointClassAssembler getEntryPointClassAssembler()
2831
* @emthod AbstractAssembler|AbstractProcessor setEntryPointClassAssembler(EntryPointClassAssembler $entryPointClassAssembler)
2932
*/

src/Compiler/Lang/Assembler/Processors/AbstractProcessor.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
1212
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\NamespaceManageable;
1313
use PHPJava\Compiler\Lang\Assembler\Traits\EntryPointClassAssemblerManageable;
14+
use PHPJava\Compiler\Lang\Assembler\Traits\ImportManageable;
1415
use PHPJava\Compiler\Lang\Assembler\Traits\MethodAssemblerManageable;
1516
use PHPJava\Compiler\Lang\Assembler\Traits\StoreManageable;
1617
use PHPJava\Compiler\Lang\Assembler\Traits\StreamManageable;
@@ -35,6 +36,7 @@ abstract class AbstractProcessor implements ProcessorInterface, ParameterService
3536
use ClassAssemblerManageable;
3637
use EntryPointClassAssemblerManageable;
3738
use StructureAccessorsLocatorManageable;
39+
use ImportManageable;
3840

3941
public static function factory(): self
4042
{

src/Compiler/Lang/Assembler/Processors/StatementProcessor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use PHPJava\Compiler\Lang\Assembler\Statements\ExpressionStatementAssembler;
1212
use PHPJava\Compiler\Lang\Assembler\Statements\ForStatementAssembler;
1313
use PHPJava\Compiler\Lang\Assembler\Statements\IfStatementAssembler;
14+
use PHPJava\Compiler\Lang\Assembler\Structure\Accessor\Imports;
1415
use PHPJava\Compiler\Lang\Assembler\Traits\Bindable;
16+
use PHPJava\Compiler\Lang\Assembler\Traits\ImportManageable;
1517
use PHPJava\Compiler\Lang\Assembler\Traits\NodeExtractable;
1618
use PHPJava\Compiler\Lang\Assembler\Traits\OperationManageable;
1719
use PHPJava\Exceptions\AssembleStructureException;
@@ -24,6 +26,8 @@ class StatementProcessor extends AbstractProcessor
2426
use Bindable;
2527
use NodeExtractable;
2628

29+
protected $imports = [];
30+
2731
/**
2832
* @param Node[] $nodes
2933
*/
@@ -66,6 +70,12 @@ public function execute(array $nodes, ?callable $callback = null): array
6670
$entryPointClassAssembler
6771
->assemble();
6872
break;
73+
case \PhpParser\Node\Stmt\Use_::class:
74+
/**
75+
* @var \PhpParser\Node\Stmt\Use_ $statement
76+
*/
77+
$this->imports[] = $statement;
78+
break;
6979
case \PhpParser\Node\Stmt\Class_::class:
7080
/**
7181
* @var \PhpParser\Node\Stmt\Class_ $statement
@@ -74,6 +84,7 @@ public function execute(array $nodes, ?callable $callback = null): array
7484
->setStreamReader($this->getStreamReader())
7585
->setNamespace($this->getNamespace())
7686
->setStructureAccessorsLocator($this->getStructureAccessorsLocator())
87+
->setImportsAccessor($this->getImportsAccessor() ?? new Imports($this->imports))
7788
->assemble();
7889
break;
7990
case \PhpParser\Node\Stmt\If_::class:

src/Compiler/Lang/Assembler/Structure/Accessor/Classes.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function find(string $className): Methods
2525
protected function findOnNode(Node $node, string $path, array $tracedPath = []): ?Node\Stmt\Class_
2626
{
2727
switch (get_class($node)) {
28+
case \PhpParser\Node\Stmt\Use_::class:
29+
// Nothing to do
30+
break;
2831
case \PhpParser\Node\Stmt\Namespace_::class:
2932
/**
3033
* @var \PhpParser\Node\Stmt\Namespace_ $node
@@ -53,7 +56,7 @@ protected function findOnNode(Node $node, string $path, array $tracedPath = []):
5356
break;
5457
default:
5558
throw new AssembleStructureException(
56-
'Cannot find structure on class.'
59+
'Cannot find structure on class or ' . get_class($node) . ' is not supported statement.'
5760
);
5861
}
5962
return null;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
namespace PHPJava\Compiler\Lang\Assembler\Structure\Accessor;
3+
4+
use PHPJava\Exceptions\NotFoundImportException;
5+
6+
class Imports extends AbstractAccessor implements AccessorInterface
7+
{
8+
public function resolvePath(string $path): string
9+
{
10+
$isTopLevelNamespace = ($path[0] ?? null) === '\\';
11+
foreach ($this->nodes as $node) {
12+
/**
13+
* @var \PhpParser\Node\Stmt\Use_ $node
14+
*/
15+
foreach ($node->uses as $useNode) {
16+
/**
17+
* @var \PhpParser\Node\Stmt\UseUse $useNode
18+
*/
19+
$merged = ltrim(implode('\\', $useNode->name->parts), '\\');
20+
$comparedResult = $isTopLevelNamespace
21+
? $this->compareAtFirst(
22+
$path,
23+
'\\' . $merged
24+
)
25+
: $this->compareAtLast(
26+
$path,
27+
$merged
28+
);
29+
30+
if ($comparedResult) {
31+
return $merged;
32+
}
33+
}
34+
}
35+
throw new NotFoundImportException(
36+
'Not found import path for ' . $path
37+
);
38+
}
39+
40+
protected function compareAtFirst(string $path, string $target): bool
41+
{
42+
return !!preg_match('/\A' . preg_quote($path, '/') . '/', $target);
43+
}
44+
45+
protected function compareAtLast(string $path, string $target): bool
46+
{
47+
return !!preg_match('/' . preg_quote($path, '/') . '\z/', $target);
48+
}
49+
}

src/Compiler/Lang/Assembler/Traits/Bindable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ protected function bindParameters(ParameterServiceInterface $from): ParameterSer
4646
->setStructureAccessorsLocator(
4747
$this->getStructureAccessorsLocator()
4848
)
49+
->setImportsAccessor(
50+
$this->getImportsAccessor()
51+
)
4952
->setClassAssembler(
5053
$this instanceof ClassAssembler
5154
? $this

0 commit comments

Comments
 (0)