Skip to content

Commit c2ffcb1

Browse files
committed
TASK: Simplify API
- run build and clean for single package - Rename Controllers and Classes from NodeTypeObject to NodeObject
1 parent 81ba326 commit c2ffcb1

7 files changed

+214
-232
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PackageFactory\NodeTypeObjects\Command;
6+
7+
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
8+
use Neos\ContentRepositoryRegistry\ContentRepositoryRegistry;
9+
use Neos\Flow\Cli\CommandController;
10+
use Neos\Flow\Package\FlowPackageInterface;
11+
use Neos\Flow\Package\GenericPackage;
12+
use Neos\Flow\Package\PackageManager;
13+
use Neos\Utility\Files;
14+
use PackageFactory\NodeTypeObjects\Domain\NodeObjectNameSpecification;
15+
use PackageFactory\NodeTypeObjects\Domain\NodeObjectNameSpecificationCollection;
16+
use PackageFactory\NodeTypeObjects\Domain\NodeObjectSpecification;
17+
18+
class NodeObjectsCommandController extends CommandController
19+
{
20+
private PackageManager $packageManager;
21+
22+
private ContentRepositoryRegistry $contentRepositoryRegistry;
23+
24+
public function injectPackageManager(PackageManager $packageManager): void
25+
{
26+
$this->packageManager = $packageManager;
27+
}
28+
29+
public function injectContentRepositoryRegistry(ContentRepositoryRegistry $contentRepositoryRegistry): void
30+
{
31+
$this->contentRepositoryRegistry = $contentRepositoryRegistry;
32+
}
33+
34+
/**
35+
* Remove all *NodeObject.php and *NodeInterface.php from the NodeTypes folder of the specified package
36+
*
37+
* @param string $packageKey PackageKey
38+
* @return void
39+
*/
40+
public function cleanCommand(string $packageKey): void
41+
{
42+
$package = $this->findFlowPackageByPackageKey($packageKey);
43+
44+
if ($package === null) {
45+
$this->output->outputLine('No packages found for packageKeys <error>"%s"</error>:', [$packageKey]);
46+
$this->quit(1);
47+
} else {
48+
$this->output->outputLine('Removing NodeObjects and NodeInterfaces from packages <info>"%s"</info>:', [$package->getPackageKey()]);
49+
}
50+
51+
$packagePath = $package->getPackagePath();
52+
if (!file_exists($packagePath . DIRECTORY_SEPARATOR . 'NodeTypes')) {
53+
return;
54+
}
55+
56+
$files = Files::readDirectoryRecursively($packagePath . DIRECTORY_SEPARATOR . 'NodeTypes', 'NodeObject.php');
57+
if (is_array($files)) {
58+
foreach ($files as $file) {
59+
if (is_file($file)) {
60+
unlink($file);
61+
}
62+
$this->outputLine(' - ' . $file);
63+
}
64+
}
65+
$files = Files::readDirectoryRecursively($packagePath . DIRECTORY_SEPARATOR . 'NodeTypes', 'NodeInterface.php');
66+
if (is_array($files)) {
67+
foreach ($files as $file) {
68+
if (is_file($file)) {
69+
unlink($file);
70+
}
71+
$this->outputLine(' - ' . $file);
72+
}
73+
}
74+
}
75+
76+
/**
77+
* Create new NodeObjects and NodeInterfaces for the selected Package
78+
*
79+
* @param string $packageKey PackageKey
80+
*/
81+
public function buildCommand(string $packageKey, string $crId = 'default'): void
82+
{
83+
$package = $this->findFlowPackageByPackageKey($packageKey);
84+
85+
if ($package === null) {
86+
$this->output->outputLine('No packages found for packageKeys <error>"%s"</error>:', [$packageKey]);
87+
$this->quit(1);
88+
} else {
89+
$this->output->outputLine('Building NodeObjects and NodeInterfaces for package <info>"%s"</info>:', [$package->getPackageKey()]);
90+
}
91+
92+
$contentRepository = $this->contentRepositoryRegistry->get(ContentRepositoryId::fromString($crId));
93+
$nodeTypeManager = $contentRepository->getNodeTypeManager();
94+
$nodeTypes = $nodeTypeManager->getNodeTypes(true);
95+
$nameSpecifications = [];
96+
foreach ($nodeTypes as $nodeType) {
97+
if (!str_starts_with($nodeType->name->value, $package->getPackageKey() . ':')) {
98+
continue;
99+
}
100+
$nameSpecifications[$nodeType->name->value] = NodeObjectNameSpecification::createFromNodeType($nodeType);
101+
}
102+
$nameSpecificationsCollection = new NodeObjectNameSpecificationCollection(...$nameSpecifications);
103+
104+
// loop 1 build interfaces
105+
// loop 2 build objects
106+
foreach ($nodeTypes as $nodeType) {
107+
if (!str_starts_with($nodeType->name->value, $package->getPackageKey() . ':')) {
108+
continue;
109+
}
110+
111+
$specification = NodeObjectSpecification::createFromPackageAndNodeType($package, $nodeType, $nameSpecificationsCollection);
112+
113+
Files::createDirectoryRecursively($specification->directory);
114+
115+
$generatedFiles = [];
116+
if ($specification->classFilename) {
117+
file_put_contents(
118+
$specification->classFilename,
119+
$specification->toPhpClassString()
120+
);
121+
$generatedFiles[] = $specification->names->fullyQualifiedClassName;
122+
}
123+
if ($specification->interfaceFilename) {
124+
file_put_contents(
125+
$specification->interfaceFilename,
126+
$specification->toPhpInterfaceString()
127+
);
128+
$generatedFiles[] = $specification->names->fullyQualifiedInterfaceName;
129+
}
130+
131+
$this->outputLine(' - ' . $specification->names->nodeTypeName . ' -> <info>' . implode(', ', $generatedFiles) . '</info>');
132+
}
133+
}
134+
135+
/**
136+
* @param string $packageKey
137+
* @throws \Neos\Flow\Cli\Exception\StopCommandException
138+
* @throws \Neos\Flow\Package\Exception\UnknownPackageException
139+
*/
140+
protected function getPackage(string $packageKey): FlowPackageInterface & GenericPackage
141+
{
142+
if ($this->packageManager->isPackageAvailable($packageKey)) {
143+
$package = $this->packageManager->getPackage($packageKey);
144+
} else {
145+
$this->output->outputLine("Unknown package " . $packageKey);
146+
$this->quit(1);
147+
}
148+
if (!$package instanceof FlowPackageInterface) {
149+
$this->output->outputLine($packageKey . " is not a Flow package");
150+
$this->quit(1);
151+
}
152+
if (!$package instanceof GenericPackage) {
153+
$this->output->outputLine($packageKey . " is not a Generic package");
154+
$this->quit(1);
155+
}
156+
157+
/**
158+
* @var array<int, array{namespace:string, classPath:string, mappingType:string}> $autoloadConfigurations
159+
*/
160+
$autoloadConfigurations = $package->getFlattenedAutoloadConfiguration();
161+
$namespace = null;
162+
foreach ($autoloadConfigurations as $autoloadConfiguration) {
163+
if (
164+
$autoloadConfiguration[ 'mappingType' ] === 'psr-4'
165+
&& str_ends_with($autoloadConfiguration[ 'namespace' ], '\\NodeTypes\\')
166+
&& (
167+
$autoloadConfiguration[ 'classPath' ] === $package->getPackagePath() . 'NodeTypes'
168+
|| $autoloadConfiguration[ 'classPath' ] === $package->getPackagePath() . 'NodeTypes/'
169+
)
170+
) {
171+
$namespace = $autoloadConfiguration[ 'namespace' ];
172+
break;
173+
}
174+
}
175+
176+
if ($namespace === null) {
177+
$this->outputLine('<error>No PSR4-NodeTypes namespace for the NodeTypes folder is registered via composer</error>');
178+
$this->quit(1);
179+
}
180+
return $package;
181+
}
182+
183+
protected function findFlowPackageByPackageKey(string $packageKey): ?FlowPackageInterface
184+
{
185+
if ($this->packageManager->isPackageAvailable($packageKey) === false) {
186+
return null;
187+
}
188+
$package = $this->packageManager->getPackage($packageKey);
189+
if ($package instanceof FlowPackageInterface) {
190+
return $package;
191+
}
192+
return null;
193+
}
194+
}

0 commit comments

Comments
 (0)