Skip to content

Commit 0d24c7f

Browse files
committed
TASK: Add readme and make generated classes are final and psr12 compliant
1 parent 23d1093 commit 0d24c7f

3 files changed

Lines changed: 83 additions & 12 deletions

File tree

Classes/Command/NodetypeObjectsCommandController.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,51 @@ public function injectPackageManager(PackageManager $packageManager): void
2727
}
2828

2929
/**
30-
* @param string $nodeTypeName Pattern for nodeTypeNames to generate classes for
30+
* Remove all NodeTypeObjects from the selected package
31+
*
3132
* @param string $packageKey PackageKey to store the classes in
3233
* @return void
3334
*/
35+
public function cleanCommand(string $packageKey):void
36+
{
37+
if($this->packageManager->isPackageAvailable($packageKey)) {
38+
$package = $this->packageManager->getPackage($packageKey);
39+
} else {
40+
$this->output->outputLine("Unknown package " . $packageKey);
41+
$this->quit(1);
42+
}
43+
if (!$package instanceof FlowPackageInterface) {
44+
$this->output->outputLine($packageKey . " is not a Flow package");
45+
$this->quit(1);
46+
}
47+
48+
$packagePath = $package->getPackagePath();
49+
$files = Files::readDirectoryRecursively($packagePath, 'NodeObject.php');
50+
if (is_array($files)) {
51+
foreach ($files as $file) {
52+
if (is_file($file)) {
53+
unlink($file);
54+
}
55+
$this->outputLine(' - ' . $file);
56+
}
57+
}
58+
}
59+
60+
/**
61+
* Create new NodeTypeObjects for the selected Package
62+
*
63+
* @param string $packageKey PackageKey
64+
*/
3465
public function buildCommand(string $packageKey):void
3566
{
3667
if($this->packageManager->isPackageAvailable($packageKey)) {
3768
$package = $this->packageManager->getPackage($packageKey);
3869
} else {
39-
$this->output->outputLine("Unknwn package " . $packageKey);
70+
$this->output->outputLine("Unknown package " . $packageKey);
4071
$this->quit(1);
4172
}
4273
if (!$package instanceof FlowPackageInterface) {
43-
$this->output->outputLine($packageKey . " is not a flow package");
74+
$this->output->outputLine($packageKey . " is not a Flow package");
4475
$this->quit(1);
4576
}
4677

@@ -75,6 +106,8 @@ public function buildCommand(string $packageKey):void
75106
$filePath,
76107
$fileName,
77108
);
109+
110+
$this->outputLine(' - ' . $filePath . '/' . $fileName);
78111
}
79112
}
80113

@@ -122,7 +155,7 @@ private function buildOne(FlowPackageInterface $package, NodeType $nodeType, str
122155
/**
123156
* @return $annotationType;
124157
*/
125-
public function $methodName (): ?$phpType
158+
public function $methodName(): ?$phpType
126159
{
127160
return \$this->node->getProperty('$propertyName');
128161
}
@@ -133,6 +166,7 @@ public function $methodName (): ?$phpType
133166

134167
$class = <<<EOL
135168
<?php
169+
136170
declare(strict_types=1);
137171
138172
namespace $classNamespace;
@@ -141,24 +175,27 @@ public function $methodName (): ?$phpType
141175
use Neos\Flow\Annotations as Flow;
142176
143177
/**
144-
* AUTOGENERATED DO NOT MODIFY
178+
* AUTOGENERATED CODE ... DO NOT MODIFY !!!
179+
*
145180
* run `./ nodetypeobjects:build` to regenerate this
146181
*/
147182
#[Flow\Proxy(false)]
148-
readonly class $className {
183+
final readonly class $className
184+
{
149185
private function __construct(
150186
public NodeInterface \$node
151-
) {}
187+
) {
188+
}
152189
153190
public static function fromNode(NodeInterface \$node): self
154191
{
155192
if (\$node->getNodeType()->getName() !== "$nodeTypeName") {
156193
throw new \Exception("unsupported nodetype " . \$node->getNodeType()->getName());
157194
}
158195
return new self(\$node);
159-
}
160-
$propertyAccesssors
196+
}$propertyAccesssors
161197
}
198+
162199
EOL;
163200

164201
Files::createDirectoryRecursively($filePath);
@@ -167,7 +204,5 @@ public static function fromNode(NodeInterface \$node): self
167204
$filePath . '/' . $fileName,
168205
$class
169206
);
170-
171-
$this->outputLine('Generated class %s (%s) from type %s', [$classNamespace . '\\' . $className, $filePath . '/' . $fileName, $nodeType->getName()]);
172207
}
173208
}

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# PackageFactory.NodeTypeObjects
2+
3+
!!! THIS IS EXPERIMENTAL ... EVERYTHING MAY CHANGE, USE AT YOUR OWN RISK !!!
4+
5+
Autogenerate php classes for NodeTypes with type safe property accessor methods that allow full static analysis.
6+
7+
- NodeTypeObject are created for each non abatrsct NodeType in the namespace of the given package.
8+
- NodeTypeObjects are stored in the `NodeTypes` folder using all parts of the NodeTypeName as folders
9+
- The namespace of each NodeTypeObject is derived from the packaghe-key with added ``NodeTypes`
10+
- The className of a NodeTypeObject is defined by the last part of the NodeTypeName with postfix `NodeTypeObject`
11+
12+
## Precondtions
13+
14+
The following preconditions have to be met for a package to use NodeTypeObjects.
15+
16+
- The Package registers a PSR4 Namespace for NodeTypes in the.composer.json that points to the NodeTypes folder
17+
- The pattern `*NodeTypeObject.php` hast to be added to `.gitignore` to avoid committing the generated files.
18+
- The commands `nodetypeobjects:build` and `nodetypeobjects:clean` have to be integrated into build processes and watchers
19+
20+
## Usage
21+
22+
The package defines the following cli commands
23+
24+
- `./flow nodetypeobjects:build Vendor.Site` : regenerate all NodeTypeObject in the given package.
25+
- `./flow nodetypeobjects:clean Vendor.Site` : remove all NodeTypeObject in the given package.
26+
27+
## Installation
28+
29+
PackageFactory.NodeTypeObjects is available via packagist. Just run `composer require packagefactory/nodetypeobjects`.
30+
31+
We use semantic-versioning so every breaking change will increase the major-version number.
32+
33+
## License
34+
35+
see [LICENSE file](LICENSE)

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"name": "packagefactory/nodetypeobjects",
55
"license": "MIT",
66
"require": {
7-
"neos/neos": "~8.3"
7+
"neos/neos": "~8.3",
8+
"php": ">=8.2"
89
},
910
"autoload": {
1011
"psr-4": {

0 commit comments

Comments
 (0)