Skip to content

Commit 7e74b4b

Browse files
author
roadiz-ci
committed
refactor: refactor all NodeType and NodesSources class-name resolution and improve nodetypes:validate-files (#262)
1 parent 960fcdb commit 7e74b4b

5 files changed

Lines changed: 27 additions & 8 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Roadiz sub-package which generates Typescript interfaces skeleton based on your schema",
44
"type": "library",
55
"require": {
6-
"roadiz/nodetype-contracts": "^3.0.0",
6+
"roadiz/nodetype-contracts": "^3.1.1",
77
"symfony/http-foundation": "7.3.*"
88
},
99
"require-dev": {

src/DeclarationGeneratorFactory.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace RZ\Roadiz\Typescript\Declaration;
66

7+
use RZ\Roadiz\Contracts\NodeType\NodeTypeClassLocatorInterface;
78
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
89
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
910
use RZ\Roadiz\Typescript\Declaration\Generators\AbstractFieldGenerator;
@@ -17,8 +18,10 @@
1718

1819
final readonly class DeclarationGeneratorFactory
1920
{
20-
public function __construct(private ParameterBag $nodeTypesBag)
21-
{
21+
public function __construct(
22+
private ParameterBag $nodeTypesBag,
23+
private NodeTypeClassLocatorInterface $nodeTypeClassLocator,
24+
) {
2225
}
2326

2427
public function getNodeTypesBag(): ParameterBag
@@ -35,15 +38,16 @@ public function createForNodeType(NodeTypeInterface $nodeType): NodeTypeGenerato
3538
{
3639
return new NodeTypeGenerator(
3740
$nodeType,
38-
$this
41+
$this,
42+
$this->nodeTypeClassLocator
3943
);
4044
}
4145

4246
public function createForNodeTypeField(NodeTypeFieldInterface $field): AbstractFieldGenerator
4347
{
4448
return match (true) {
4549
$field->isDocuments() => new DocumentsFieldGenerator($field, $this->nodeTypesBag),
46-
$field->isNodes() => new NodeReferencesFieldGenerator($field, $this->nodeTypesBag),
50+
$field->isNodes() => new NodeReferencesFieldGenerator($this->nodeTypeClassLocator, $field, $this->nodeTypesBag),
4751
$field->isChildrenNodes() => new ChildrenNodeFieldGenerator($field, $this->nodeTypesBag),
4852
$field->isMultiple(), $field->isEnum() => new EnumFieldGenerator($field, $this->nodeTypesBag),
4953
default => new ScalarFieldGenerator($field, $this->nodeTypesBag),

src/Generators/DeclarationGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace RZ\Roadiz\Typescript\Declaration\Generators;
66

7+
use RZ\Roadiz\Contracts\NodeType\NodeTypeClassLocatorInterface;
78
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
89
use RZ\Roadiz\Typescript\Declaration\DeclarationGeneratorFactory;
910

@@ -19,6 +20,7 @@ final class DeclarationGenerator
1920
*/
2021
public function __construct(
2122
private readonly DeclarationGeneratorFactory $generatorFactory,
23+
private readonly NodeTypeClassLocatorInterface $nodeTypeClassLocator,
2224
array $nodeTypes = [],
2325
) {
2426
if (empty($nodeTypes)) {
@@ -45,7 +47,7 @@ public function getContents(): string
4547

4648
private function getAllTypesInterface(): string
4749
{
48-
$nodeTypeNames = array_map(fn (NodeTypeInterface $nodeType) => $nodeType->getSourceEntityClassName(), $this->nodeTypes);
50+
$nodeTypeNames = array_map(fn (NodeTypeInterface $nodeType) => $this->nodeTypeClassLocator->getSourceEntityClassName($nodeType), $this->nodeTypes);
4951

5052
$nodeTypeNames = implode(' | ', $nodeTypeNames);
5153

src/Generators/NodeReferencesFieldGenerator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44

55
namespace RZ\Roadiz\Typescript\Declaration\Generators;
66

7+
use RZ\Roadiz\Contracts\NodeType\NodeTypeClassLocatorInterface;
8+
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
79
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
10+
use Symfony\Component\HttpFoundation\ParameterBag;
811

912
final class NodeReferencesFieldGenerator extends AbstractFieldGenerator
1013
{
14+
public function __construct(
15+
private readonly NodeTypeClassLocatorInterface $nodeTypeClassLocator,
16+
NodeTypeFieldInterface $field,
17+
ParameterBag $nodeTypesBag,
18+
) {
19+
parent::__construct($field, $nodeTypesBag);
20+
}
21+
1122
#[\Override]
1223
protected function getNullableAssertion(): string
1324
{
@@ -46,7 +57,7 @@ private function getUnionType(): string
4657
return 'RoadizNodesSources';
4758
}
4859

49-
return implode(' | ', array_map(fn (NodeTypeInterface $nodeType) => $nodeType->getSourceEntityClassName(), $nodeTypes));
60+
return implode(' | ', array_map(fn (NodeTypeInterface $nodeType) => $this->nodeTypeClassLocator->getSourceEntityClassName($nodeType), $nodeTypes));
5061
}
5162

5263
#[\Override]

src/Generators/NodeTypeGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace RZ\Roadiz\Typescript\Declaration\Generators;
66

7+
use RZ\Roadiz\Contracts\NodeType\NodeTypeClassLocatorInterface;
78
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
89
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
910
use RZ\Roadiz\Contracts\NodeType\SerializableInterface;
@@ -19,6 +20,7 @@ final class NodeTypeGenerator
1920
public function __construct(
2021
private readonly NodeTypeInterface $nodeType,
2122
private readonly DeclarationGeneratorFactory $generatorFactory,
23+
private readonly NodeTypeClassLocatorInterface $nodeTypeClassLocator,
2224
) {
2325
$this->fieldGenerators = [];
2426

@@ -54,7 +56,7 @@ public function getContents(): string
5456
protected function getInterfaceBody(): string
5557
{
5658
$lines = [
57-
'export interface '.$this->nodeType->getSourceEntityClassName().' extends RoadizNodesSources {',
59+
'export interface '.$this->nodeTypeClassLocator->getSourceEntityClassName($this->nodeType).' extends RoadizNodesSources {',
5860
$this->getFieldsContents(),
5961
'}',
6062
];

0 commit comments

Comments
 (0)