Skip to content

Commit

Permalink
Delegate factory rendering to interface in kickstarter
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schmitt committed Nov 16, 2022
1 parent 27046ff commit b28563f
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 53 deletions.
7 changes: 6 additions & 1 deletion Classes/Command/ComponentCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentGenerator;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentName;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Enum\EnumGenerator;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FactoryRendererInterface;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\PackageKey;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\PackageResolver;
use PackageFactory\AtomicFusion\PresentationObjects\Infrastructure\DefensiveConfirmationFileWriter;
Expand All @@ -34,6 +35,9 @@ class ComponentCommandController extends CommandController
*/
protected $colocate;

#[Flow\Inject]
protected FactoryRendererInterface $factoryRenderer;

/**
* Create a new PresentationObject component and factory
*
Expand Down Expand Up @@ -66,7 +70,8 @@ class ComponentCommandController extends CommandController
public function kickStartCommand(string $name, bool $listable = false, bool $yes = false): void
{
$componentGenerator = new ComponentGenerator(
new DefensiveConfirmationFileWriter($this->output, $yes)
new DefensiveConfirmationFileWriter($this->output, $yes),
$this->factoryRenderer
);
$package = $this->packageResolver->resolvePackage();
$componentName = ComponentName::fromInput($name, PackageKey::fromPackage($package));
Expand Down
22 changes: 2 additions & 20 deletions Classes/Domain/Component/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
final class Component
{
public function __construct(
private ComponentName $name,
private Props $props
public readonly ComponentName $name,
public readonly Props $props
) {
}

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

public function getFactoryContent(): string
{
return '<?php
' . $this->name->renderClassComment() . '
declare(strict_types=1);
namespace ' . $this->name->getPhpNamespace() . ';
use PackageFactory\AtomicFusion\PresentationObjects\Fusion\AbstractComponentPresentationObjectFactory;
final class ' . $this->name->getSimpleFactoryName() . ' extends AbstractComponentPresentationObjectFactory
{
}
';
}

public function getComponentArrayContent(): string
{
return '<?php
Expand Down
18 changes: 10 additions & 8 deletions Classes/Domain/Component/ComponentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@
namespace PackageFactory\AtomicFusion\PresentationObjects\Domain\Component;

use Neos\Flow\Annotations as Flow;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FactoryRendererInterface;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FileWriterInterface;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Yaml\Dumper as YamlWriter;

/**
* The component generator domain service
*
* @Flow\Proxy(false)
*/
#[Flow\Proxy(false)]
final class ComponentGenerator
{
private FileWriterInterface $fileWriter;

public function __construct(FileWriterInterface $fileWriter)
{
$this->fileWriter = $fileWriter;
public function __construct(
private readonly FileWriterInterface $fileWriter,
private readonly FactoryRendererInterface $factoryRenderer
) {
}

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

$this->fileWriter->writeFile($componentName->getClassPath($packagePath, $colocate), $component->getClassContent());
$this->fileWriter->writeFile($componentName->getFactoryPath($packagePath, $colocate), $component->getFactoryContent());
$this->fileWriter->writeFile(
$componentName->getFactoryPath($packagePath, $colocate),
$this->factoryRenderer->renderFactoryContent($component)
);
$this->fileWriter->writeFile($componentName->getFusionComponentPath($packagePath), $component->getFusionContent());

if ($listable) {
Expand Down
37 changes: 37 additions & 0 deletions Classes/Domain/DummyFactoryRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the PackageFactory.AtomicFusion.PresentationObjects package
*/

declare(strict_types=1);

namespace PackageFactory\AtomicFusion\PresentationObjects\Domain;

use Neos\Flow\Annotations as Flow;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\Component;

/**
* The dummy presentation object factory renderer
*/
#[Flow\Proxy(false)]
final class DummyFactoryRenderer implements FactoryRendererInterface
{
public function renderFactoryContent(Component $component): string
{
return '<?php
' . $component->name->renderClassComment() . '
declare(strict_types=1);
namespace ' . $component->name->getPhpNamespace() . ';
use PackageFactory\AtomicFusion\PresentationObjects\Fusion\AbstractComponentPresentationObjectFactory;
final class ' . $component->name->getSimpleFactoryName() . ' extends AbstractComponentPresentationObjectFactory
{
}
';
}
}
16 changes: 16 additions & 0 deletions Classes/Domain/FactoryRendererInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the PackageFactory.AtomicFusion.PresentationObjects package
*/

declare(strict_types=1);

namespace PackageFactory\AtomicFusion\PresentationObjects\Domain;

use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\Component;

interface FactoryRendererInterface
{
public function renderFactoryContent(Component $component): string;
}
4 changes: 3 additions & 1 deletion Tests/Unit/Domain/Component/ComponentGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use org\bovigo\vfs\vfsStream;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentGenerator;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentName;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\DummyFactoryRenderer;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FusionNamespace;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\PackageKey;
use PackageFactory\AtomicFusion\PresentationObjects\Infrastructure\SimpleFileWriter;
Expand Down Expand Up @@ -50,7 +51,8 @@ public function setUpComponentGeneratorTest(): void
]);

$this->componentGenerator = new ComponentGenerator(
new SimpleFileWriter()
new SimpleFileWriter(),
new DummyFactoryRenderer()
);
}

Expand Down
23 changes: 0 additions & 23 deletions Tests/Unit/Domain/Component/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,6 @@ public function __construct(
);
}

public function testGetFactoryContent(): void
{
Assert::assertSame(
'<?php
/*
* This file is part of the Vendor.Site package.
*/
declare(strict_types=1);
namespace Vendor\Site\Presentation\Component\MyNewComponent;
use PackageFactory\AtomicFusion\PresentationObjects\Fusion\AbstractComponentPresentationObjectFactory;
final class MyNewComponentFactory extends AbstractComponentPresentationObjectFactory
{
}
',
$this->subject->getFactoryContent()
);
}

public function testGetFusionContent(): void
{
Assert::assertSame(
Expand Down
87 changes: 87 additions & 0 deletions Tests/Unit/Domain/DummyFactoryRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/*
* This file is part of the PackageFactory.AtomicFusion.PresentationObjects package
*/

declare(strict_types=1);

namespace PackageFactory\AtomicFusion\PresentationObjects\Tests\Unit\Domain;

use Neos\Flow\Tests\UnitTestCase;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\Component;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\ComponentName;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\DummyFactoryRenderer;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\FusionNamespace;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\PackageKey;
use PackageFactory\AtomicFusion\PresentationObjects\Domain\Component\Props;
use PHPUnit\Framework\Assert;

/**
* Test cases for DummyFactoryRenderer
*/
class DummyFactoryRendererTest extends UnitTestCase
{
private ?Component $component = null;

public function setUp(): void
{
parent::setUp();

$componentName = new ComponentName(
new PackageKey('Vendor.Site'),
FusionNamespace::default(),
'MyNewComponent',
);
$this->component = new Component(
$componentName,
Props::fromInputArray(
$componentName,
[
'bool:bool',
'nullableBool:?bool',
'float:float',
'nullableFloat:?float',
'int:int',
'nullableInt:?int',
'string:string',
'nullableString:?string',
'uri:Uri',
'nullableUri:?Uri',
'image:ImageSource',
'nullableImage:?ImageSource',
'subComponent:MyComponent',
'nullableSubComponent:?MyComponent',
'componentArray:array<MyComponent>',
'enum:MyStringEnum',
'nullableEnum:?MyStringEnum',
'slot:slot',
'nullableSlot:?slot',
]
)
);
}

public function testGetFactoryContent(): void
{
Assert::assertSame(
'<?php
/*
* This file is part of the Vendor.Site package.
*/
declare(strict_types=1);
namespace Vendor\Site\Presentation\Component\MyNewComponent;
use PackageFactory\AtomicFusion\PresentationObjects\Fusion\AbstractComponentPresentationObjectFactory;
final class MyNewComponentFactory extends AbstractComponentPresentationObjectFactory
{
}
',
(new DummyFactoryRenderer())->renderFactoryContent($this->component)
);
}
}

0 comments on commit b28563f

Please sign in to comment.