Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
138 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "extender" Extension for TYPO3 CMS. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Evoweb\Extender\Composer\Generator; | ||
|
||
use Evoweb\Extender\Parser\FileSegments; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
|
||
class ConstantGenerator implements GeneratorInterface | ||
{ | ||
public function generate(array $statements, array $fileSegments): array | ||
{ | ||
$namespace = $this->getNamespace($statements); | ||
$class = $this->getClass($namespace); | ||
|
||
if ($class) { | ||
$class->stmts = [...$class->stmts, ...$this->getUniqueClassConstants($fileSegments)]; | ||
} | ||
|
||
return $statements; | ||
} | ||
|
||
protected function getUniqueClassConstants(array $fileSegments): array | ||
{ | ||
$classConstant = []; | ||
/** @var FileSegments $fileSegment */ | ||
foreach ($fileSegments as $fileSegment) { | ||
foreach ($fileSegment->getClassConsts() as $currentClassConstant) { | ||
foreach ($currentClassConstant->consts as $currentConst) { | ||
if (isset($classConstant[(string)$currentConst->name])) { | ||
continue; | ||
} | ||
$classConstant[(string)$currentConst->name] = new ClassConst([$currentConst]); | ||
} | ||
} | ||
} | ||
return array_values($classConstant); | ||
} | ||
|
||
protected function getNamespace(array $statements): ?Namespace_ | ||
{ | ||
$namespace = null; | ||
foreach ($statements as $statement) { | ||
if ($statement instanceof Namespace_) { | ||
$namespace = $statement; | ||
break; | ||
} | ||
} | ||
return $namespace; | ||
} | ||
|
||
protected function getClass(Namespace_ $namespace): ?Class_ | ||
{ | ||
/** @var ?Class_ $class */ | ||
$class = null; | ||
foreach ($namespace->stmts as $node) { | ||
if ($node instanceof Class_) { | ||
$class = $node; | ||
break; | ||
} | ||
} | ||
return $class; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "extender" Extension for TYPO3 CMS. | ||
* | ||
* It is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License, either version 2 | ||
* of the License, or any later version. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Evoweb\Extender\Parser\Visitor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
|
||
class ConstantVisitor extends AbstractVisitor | ||
{ | ||
protected array $properties = []; | ||
|
||
public function enterNode(Node $node): void | ||
{ | ||
if ($node instanceof ClassConst) { | ||
$this->fileSegment->addClassConst($node); | ||
} | ||
} | ||
} |
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