Skip to content

Commit d449dce

Browse files
committed
TASK: Ensure NodeTypeeNameSpecification does not deal with packages
1 parent 178f14d commit d449dce

File tree

4 files changed

+69
-50
lines changed

4 files changed

+69
-50
lines changed

Classes/Command/NodetypeObjectsCommandController.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function buildCommand(string $packageKeys): void
105105
if (!str_starts_with($nodeType->getName(), $package->getPackageKey() . ':')) {
106106
continue;
107107
}
108-
$nameSpecifications[ $nodeType->getName() ] = NodeTypeObjectNameSpecification::createFromPackageAndNodeType($package, $nodeType);
108+
$nameSpecifications[ $nodeType->getName() ] = NodeTypeObjectNameSpecification::createFromNodeType($nodeType);
109109
}
110110
}
111111
$nameSpecificationsCollection = new NodeTypeObjectNameSpecificationCollection(...$nameSpecifications);
@@ -119,21 +119,22 @@ public function buildCommand(string $packageKeys): void
119119

120120
$specification = NodeTypeObjectSpecification::createFromPackageAndNodeType($package, $nodeType, $nameSpecificationsCollection);
121121

122-
Files::createDirectoryRecursively($specification->names->directory);
122+
Files::createDirectoryRecursively($specification->directory);
123+
123124
$generatedFiles = [];
124-
if ($specification->names->className) {
125+
if ($specification->classFilename) {
125126
file_put_contents(
126-
$specification->names->directory . DIRECTORY_SEPARATOR . $specification->names->className . '.php',
127+
$specification->classFilename,
127128
$specification->toPhpClassString()
128129
);
129-
$generatedFiles[] = $specification->names->phpNamespace . '\\' . $specification->names->className;
130+
$generatedFiles[] = $specification->names->fullyQualifiedClassName;
130131
}
131-
if ($specification->names->interfaceName) {
132+
if ($specification->interfaceFilename) {
132133
file_put_contents(
133-
$specification->names->directory . DIRECTORY_SEPARATOR . $specification->names->interfaceName . '.php',
134+
$specification->interfaceFilename,
134135
$specification->toPhpInterfaceString()
135136
);
136-
$generatedFiles[] = $specification->names->phpNamespace . '\\' . $specification->names->interfaceName;
137+
$generatedFiles[] = $specification->names->fullyQualifiedInterfaceName;
137138
}
138139

139140
$this->outputLine(' - ' . $specification->names->nodeTypeName . ' -> <info>' . implode(', ', $generatedFiles) . '</info>');

Classes/Domain/NodeTypeObjectNameSpecification.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,43 @@
1212
readonly class NodeTypeObjectNameSpecification
1313
{
1414
public function __construct(
15-
public string $directory,
1615
public string $nodeTypeName,
1716
public string $phpNamespace,
1817
public ?string $className,
19-
public string $interfaceName
18+
public ?string $fullyQualifiedClassName,
19+
public ?string $interfaceName,
20+
public ?string $fullyQualifiedInterfaceName,
2021
) {
2122
}
2223

23-
public static function createFromPackageAndNodeType(
24-
FlowPackageInterface $package,
24+
public static function createFromNodeType(
2525
NodeType $nodeType
2626
): self {
2727

28-
if (!str_starts_with($nodeType->getName(), $package->getPackageKey() . ':')) {
29-
throw new \Exception("Only nodetypes from the given package are allowed");
30-
}
28+
list($packageKey, $nodeName) = explode(':', $nodeType->getName(), 2);
3129

32-
$localNameParts = explode('.', str_replace($package->getPackageKey() . ':', '', $nodeType->getName()));
30+
$localNameParts = explode('.', $nodeName);
3331
$localName = array_pop($localNameParts);
34-
$localNamespace = implode('.', $localNameParts);
3532

36-
$namespace = str_replace('.', '\\', $package->getPackageKey())
37-
. '\\NodeTypes'
38-
. ($localNamespace ? '\\' . str_replace('.', '\\', $localNamespace) : '')
39-
. '\\' . $localName;
33+
$phpNamespace = str_replace(['.', ':'], ['\\', '\\NodeTypes\\'], $nodeType->getName());
4034

41-
$directory = $package->getPackagePath()
42-
. 'NodeTypes' . DIRECTORY_SEPARATOR
43-
. ($localNamespace ? str_replace('.', DIRECTORY_SEPARATOR, $localNamespace) . DIRECTORY_SEPARATOR : '')
44-
. $localName;
4535

4636
if ($nodeType->isAbstract()) {
4737
$className = null;
4838
} else {
4939
$className = str_replace('.', '\\', $localName) . 'NodeObject';
5040
}
41+
42+
/** @var string|null $interfaceName */
5143
$interfaceName = str_replace('.', '\\', $localName) . 'NodeInterface';
5244

5345
return new self(
54-
$directory,
5546
$nodeType->getName(),
56-
$namespace,
47+
$phpNamespace,
5748
$className,
49+
$className ? $phpNamespace . '\\' . $className : null,
5850
$interfaceName,
51+
$interfaceName ? $phpNamespace . '\\' . $interfaceName : null
5952
);
6053
}
6154
}

Classes/Domain/NodeTypeObjectSpecification.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public function __construct(
1515
public NodeTypeObjectNameSpecification $names,
1616
public NodePropertySpecificationCollection $properties,
1717
public NodeTypeObjectNameSpecificationCollection $superTypes,
18+
public string $directory,
19+
public ?string $classFilename,
20+
public ?string $interfaceFilename,
1821
) {
1922
}
2023

@@ -23,10 +26,32 @@ public static function createFromPackageAndNodeType(
2326
NodeType $nodeType,
2427
NodeTypeObjectNameSpecificationCollection $nameCollection
2528
): self {
26-
return new NodeTypeObjectSpecification(
27-
NodeTypeObjectNameSpecification::createFromPackageAndNodeType($package, $nodeType),
29+
30+
if (!str_starts_with($nodeType->getName(), $package->getPackageKey() . ':')) {
31+
throw new \Exception("Only nodetypes from the given package are allowed");
32+
}
33+
34+
$nameSpecification = NodeTypeObjectNameSpecification::createFromNodeType($nodeType);
35+
36+
$localNameParts = explode('.', str_replace($package->getPackageKey() . ':', '', $nodeType->getName()));
37+
$localName = array_pop($localNameParts);
38+
$localNamespace = implode('.', $localNameParts);
39+
40+
$directory = $package->getPackagePath()
41+
. 'NodeTypes' . DIRECTORY_SEPARATOR
42+
. ($localNamespace ? str_replace('.', DIRECTORY_SEPARATOR, $localNamespace) . DIRECTORY_SEPARATOR : '')
43+
. $localName;
44+
45+
$classFilename = $nameSpecification->className ? $directory . DIRECTORY_SEPARATOR . $nameSpecification->className . '.php' : null;
46+
$interfaceFileName = $nameSpecification->interfaceName ? $directory . DIRECTORY_SEPARATOR . $nameSpecification->interfaceName . '.php' : null;
47+
48+
return new self(
49+
$nameSpecification,
2850
NodePropertySpecificationCollection::createFromNodeType($nodeType),
29-
NodeTypeObjectNameSpecificationCollection::createFromNodeTypeAndCollection($nodeType, $nameCollection)
51+
NodeTypeObjectNameSpecificationCollection::createFromNodeTypeAndCollection($nodeType, $nameCollection),
52+
$directory,
53+
$classFilename,
54+
$interfaceFileName
3055
);
3156
}
3257

Tests/Unit/NodeTypeObjectNameSpecificationTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,48 @@
1111
class NodeTypeObjectNameSpecificationTest extends TestCase
1212
{
1313

14-
public function testDetectionOfNamesFromPackageAndNodeType(): void
14+
public function testDetectionOfNamesFromNodeType(): void
1515
{
16-
$package = $this->createMock(FlowPackageInterface::class);
17-
$package->expects(self::any())->method('getPackageKey')->willReturn('Vendor.Example');
18-
$package->expects(self::any())->method('getPackagePath')->willReturn('/var/www/html/Packages/Vendor.Example/');
19-
20-
// nodetype has a name and
2116
$nodeType = $this->createMock(NodeType::class);
2217
$nodeType->expects(self::any())->method('getName')->willReturn('Vendor.Example:Foo.Bar');
2318

24-
$specification = NodeTypeObjectNameSpecification::createFromPackageAndNodeType(
25-
$package,
19+
$specification = NodeTypeObjectNameSpecification::createFromNodeType(
2620
$nodeType,
2721
);
2822

2923
$this->assertEquals(
3024
new NodeTypeObjectNameSpecification(
31-
'/var/www/html/Packages/Vendor.Example/NodeTypes/Foo/Bar',
3225
'Vendor.Example:Foo.Bar',
3326
'Vendor\Example\NodeTypes\Foo\Bar',
3427
'BarNodeObject',
28+
'Vendor\Example\NodeTypes\Foo\Bar\BarNodeObject',
3529
'BarNodeInterface',
30+
'Vendor\Example\NodeTypes\Foo\Bar\BarNodeInterface'
3631
),
3732
$specification
3833
);
3934
}
4035

41-
public function testNodesFromOtherPackagesThrowException(): void
36+
public function testNoClassesForAbstractNodeType(): void
4237
{
43-
$package = $this->createMock(FlowPackageInterface::class);
44-
$package->expects(self::any())->method('getPackageKey')->willReturn('Vendor.Example');
45-
$package->expects(self::any())->method('getPackagePath')->willReturn('/var/www/html/Packages/Vendor.Example/');
46-
47-
// nodetype has a name and
4838
$nodeType = $this->createMock(NodeType::class);
49-
$nodeType->expects(self::any())->method('getName')->willReturn('Vendor.Other:Foo.Bar');
50-
51-
$this->expectException(\Exception::class);
39+
$nodeType->expects(self::any())->method('getName')->willReturn('Vendor.Example:Foo.Bar');
40+
$nodeType->expects(self::any())->method('isAbstract')->willReturn(true);
5241

53-
NodeTypeObjectNameSpecification::createFromPackageAndNodeType(
54-
$package,
42+
$specification = NodeTypeObjectNameSpecification::createFromNodeType(
5543
$nodeType,
5644
);
45+
46+
$this->assertEquals(
47+
new NodeTypeObjectNameSpecification(
48+
'Vendor.Example:Foo.Bar',
49+
'Vendor\Example\NodeTypes\Foo\Bar',
50+
null,
51+
null,
52+
'BarNodeInterface',
53+
'Vendor\Example\NodeTypes\Foo\Bar\BarNodeInterface'
54+
),
55+
$specification
56+
);
5757
}
5858
}

0 commit comments

Comments
 (0)