Skip to content

Commit b28563f

Browse files
author
Bernhard Schmitt
committed
Delegate factory rendering to interface in kickstarter
1 parent 27046ff commit b28563f

8 files changed

Lines changed: 161 additions & 53 deletions

File tree

Classes/Command/ComponentCommandController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentGenerator;
1414
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentName;
1515
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\EnumGenerator;
16+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FactoryRendererInterface;
1617
use PackageFactory\AtomicFusion\PresentationObjects\Domain\PackageKey;
1718
use PackageFactory\AtomicFusion\PresentationObjects\Domain\PackageResolver;
1819
use PackageFactory\AtomicFusion\PresentationObjects\Infrastructure\DefensiveConfirmationFileWriter;
@@ -34,6 +35,9 @@ class ComponentCommandController extends CommandController
3435
*/
3536
protected $colocate;
3637

38+
#[Flow\Inject]
39+
protected FactoryRendererInterface $factoryRenderer;
40+
3741
/**
3842
* Create a new PresentationObject component and factory
3943
*
@@ -66,7 +70,8 @@ class ComponentCommandController extends CommandController
6670
public function kickStartCommand(string $name, bool $listable = false, bool $yes = false): void
6771
{
6872
$componentGenerator = new ComponentGenerator(
69-
new DefensiveConfirmationFileWriter($this->output, $yes)
73+
new DefensiveConfirmationFileWriter($this->output, $yes),
74+
$this->factoryRenderer
7075
);
7176
$package = $this->packageResolver->resolvePackage();
7277
$componentName = ComponentName::fromInput($name, PackageKey::fromPackage($package));

Classes/Domain/Component/Component.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
final class Component
1515
{
1616
public function __construct(
17-
private ComponentName $name,
18-
private Props $props
17+
public readonly ComponentName $name,
18+
public readonly Props $props
1919
) {
2020
}
2121

@@ -40,24 +40,6 @@ final class ' . $this->name->getSimpleClassName() . ' extends AbstractComponentP
4040
';
4141
}
4242

43-
public function getFactoryContent(): string
44-
{
45-
return '<?php
46-
47-
' . $this->name->renderClassComment() . '
48-
49-
declare(strict_types=1);
50-
51-
namespace ' . $this->name->getPhpNamespace() . ';
52-
53-
use PackageFactory\AtomicFusion\PresentationObjects\Fusion\AbstractComponentPresentationObjectFactory;
54-
55-
final class ' . $this->name->getSimpleFactoryName() . ' extends AbstractComponentPresentationObjectFactory
56-
{
57-
}
58-
';
59-
}
60-
6143
public function getComponentArrayContent(): string
6244
{
6345
return '<?php

Classes/Domain/Component/ComponentGenerator.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@
99
namespace PackageFactory\AtomicFusion\PresentationObjects\Domain\Component;
1010

1111
use Neos\Flow\Annotations as Flow;
12+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FactoryRendererInterface;
1213
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FileWriterInterface;
1314
use Symfony\Component\Yaml\Parser as YamlParser;
1415
use Symfony\Component\Yaml\Dumper as YamlWriter;
1516

1617
/**
1718
* The component generator domain service
18-
*
19-
* @Flow\Proxy(false)
2019
*/
20+
#[Flow\Proxy(false)]
2121
final class ComponentGenerator
2222
{
23-
private FileWriterInterface $fileWriter;
24-
25-
public function __construct(FileWriterInterface $fileWriter)
26-
{
27-
$this->fileWriter = $fileWriter;
23+
public function __construct(
24+
private readonly FileWriterInterface $fileWriter,
25+
private readonly FactoryRendererInterface $factoryRenderer
26+
) {
2827
}
2928

3029
/**
@@ -47,7 +46,10 @@ public function generateComponent(
4746
$component = new Component($componentName, $props);
4847

4948
$this->fileWriter->writeFile($componentName->getClassPath($packagePath, $colocate), $component->getClassContent());
50-
$this->fileWriter->writeFile($componentName->getFactoryPath($packagePath, $colocate), $component->getFactoryContent());
49+
$this->fileWriter->writeFile(
50+
$componentName->getFactoryPath($packagePath, $colocate),
51+
$this->factoryRenderer->renderFactoryContent($component)
52+
);
5153
$this->fileWriter->writeFile($componentName->getFusionComponentPath($packagePath), $component->getFusionContent());
5254

5355
if ($listable) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PackageFactory.AtomicFusion.PresentationObjects package
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace PackageFactory\AtomicFusion\PresentationObjects\Domain;
10+
11+
use Neos\Flow\Annotations as Flow;
12+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\Component;
13+
14+
/**
15+
* The dummy presentation object factory renderer
16+
*/
17+
#[Flow\Proxy(false)]
18+
final class DummyFactoryRenderer implements FactoryRendererInterface
19+
{
20+
public function renderFactoryContent(Component $component): string
21+
{
22+
return '<?php
23+
24+
' . $component->name->renderClassComment() . '
25+
26+
declare(strict_types=1);
27+
28+
namespace ' . $component->name->getPhpNamespace() . ';
29+
30+
use PackageFactory\AtomicFusion\PresentationObjects\Fusion\AbstractComponentPresentationObjectFactory;
31+
32+
final class ' . $component->name->getSimpleFactoryName() . ' extends AbstractComponentPresentationObjectFactory
33+
{
34+
}
35+
';
36+
}
37+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PackageFactory.AtomicFusion.PresentationObjects package
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace PackageFactory\AtomicFusion\PresentationObjects\Domain;
10+
11+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\Component;
12+
13+
interface FactoryRendererInterface
14+
{
15+
public function renderFactoryContent(Component $component): string;
16+
}

Tests/Unit/Domain/Component/ComponentGeneratorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use org\bovigo\vfs\vfsStream;
1313
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentGenerator;
1414
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentName;
15+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\DummyFactoryRenderer;
1516
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FusionNamespace;
1617
use PackageFactory\AtomicFusion\PresentationObjects\Domain\PackageKey;
1718
use PackageFactory\AtomicFusion\PresentationObjects\Infrastructure\SimpleFileWriter;
@@ -50,7 +51,8 @@ public function setUpComponentGeneratorTest(): void
5051
]);
5152

5253
$this->componentGenerator = new ComponentGenerator(
53-
new SimpleFileWriter()
54+
new SimpleFileWriter(),
55+
new DummyFactoryRenderer()
5456
);
5557
}
5658

Tests/Unit/Domain/Component/ComponentTest.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,29 +116,6 @@ public function __construct(
116116
);
117117
}
118118

119-
public function testGetFactoryContent(): void
120-
{
121-
Assert::assertSame(
122-
'<?php
123-
124-
/*
125-
* This file is part of the Vendor.Site package.
126-
*/
127-
128-
declare(strict_types=1);
129-
130-
namespace Vendor\Site\Presentation\Component\MyNewComponent;
131-
132-
use PackageFactory\AtomicFusion\PresentationObjects\Fusion\AbstractComponentPresentationObjectFactory;
133-
134-
final class MyNewComponentFactory extends AbstractComponentPresentationObjectFactory
135-
{
136-
}
137-
',
138-
$this->subject->getFactoryContent()
139-
);
140-
}
141-
142119
public function testGetFusionContent(): void
143120
{
144121
Assert::assertSame(
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PackageFactory.AtomicFusion.PresentationObjects package
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace PackageFactory\AtomicFusion\PresentationObjects\Tests\Unit\Domain;
10+
11+
use Neos\Flow\Tests\UnitTestCase;
12+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\Component;
13+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentName;
14+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\DummyFactoryRenderer;
15+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FusionNamespace;
16+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\PackageKey;
17+
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\Props;
18+
use PHPUnit\Framework\Assert;
19+
20+
/**
21+
* Test cases for DummyFactoryRenderer
22+
*/
23+
class DummyFactoryRendererTest extends UnitTestCase
24+
{
25+
private ?Component $component = null;
26+
27+
public function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$componentName = new ComponentName(
32+
new PackageKey('Vendor.Site'),
33+
FusionNamespace::default(),
34+
'MyNewComponent',
35+
);
36+
$this->component = new Component(
37+
$componentName,
38+
Props::fromInputArray(
39+
$componentName,
40+
[
41+
'bool:bool',
42+
'nullableBool:?bool',
43+
'float:float',
44+
'nullableFloat:?float',
45+
'int:int',
46+
'nullableInt:?int',
47+
'string:string',
48+
'nullableString:?string',
49+
'uri:Uri',
50+
'nullableUri:?Uri',
51+
'image:ImageSource',
52+
'nullableImage:?ImageSource',
53+
'subComponent:MyComponent',
54+
'nullableSubComponent:?MyComponent',
55+
'componentArray:array<MyComponent>',
56+
'enum:MyStringEnum',
57+
'nullableEnum:?MyStringEnum',
58+
'slot:slot',
59+
'nullableSlot:?slot',
60+
]
61+
)
62+
);
63+
}
64+
65+
public function testGetFactoryContent(): void
66+
{
67+
Assert::assertSame(
68+
'<?php
69+
70+
/*
71+
* This file is part of the Vendor.Site package.
72+
*/
73+
74+
declare(strict_types=1);
75+
76+
namespace Vendor\Site\Presentation\Component\MyNewComponent;
77+
78+
use PackageFactory\AtomicFusion\PresentationObjects\Fusion\AbstractComponentPresentationObjectFactory;
79+
80+
final class MyNewComponentFactory extends AbstractComponentPresentationObjectFactory
81+
{
82+
}
83+
',
84+
(new DummyFactoryRenderer())->renderFactoryContent($this->component)
85+
);
86+
}
87+
}

0 commit comments

Comments
 (0)