-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNodeTemplateCommandController.php
More file actions
164 lines (140 loc) · 5.93 KB
/
NodeTemplateCommandController.php
File metadata and controls
164 lines (140 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace Flowpack\NodeTemplates\Application\Command;
use Flowpack\NodeTemplates\Domain\ErrorHandling\ProcessingErrors;
use Flowpack\NodeTemplates\Domain\NodeCreation\NodeCreationService;
use Flowpack\NodeTemplates\Domain\NodeTemplateDumper\NodeTemplateDumper;
use Flowpack\NodeTemplates\Domain\TemplateConfiguration\TemplateConfigurationProcessor;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\ContentRepository\Domain\Service\NodeTypeManager;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Neos\Domain\Service\ContentContext;
class NodeTemplateCommandController extends CommandController
{
/**
* @var NodeCreationService
* @Flow\Inject
*/
protected $nodeCreationService;
/**
* @Flow\Inject
* @var ContextFactoryInterface
*/
protected $contextFactory;
/**
* @Flow\Inject
* @var NodeTemplateDumper
*/
protected $nodeTemplateDumper;
/**
* @Flow\Inject
* @var TemplateConfigurationProcessor
*/
protected $templateConfigurationProcessor;
/**
* @Flow\Inject
* @var NodeTypeManager
*/
protected $nodeTypeManager;
/**
* Dump the node tree structure into a NodeTemplate YAML structure.
* References to Nodes and non-primitive property values are commented out in the YAML.
*
* @param string $startingNodeId specified root node of the node tree.
* @param string $workspaceName custom workspace to dump from. Defaults to 'live'.
* @return void
*/
public function createFromNodeSubtreeCommand(string $startingNodeId, string $workspaceName = 'live'): void
{
$subgraph = $this->contextFactory->create([
'workspaceName' => $workspaceName
]);
/** @var ?NodeInterface $node */
$node = $subgraph->getNodeByIdentifier($startingNodeId);
if (!$node) {
throw new \InvalidArgumentException("Node $startingNodeId doesnt exist in workspace $workspaceName.");
}
echo $this->nodeTemplateDumper->createNodeTemplateYamlDumpFromSubtree($node);
}
/**
* Checks if all configured NodeTemplates are valid. E.g no syntax errors in EEL expressions,
* that properties exist on the node type and their types match and other checks.
*
* We process and build all configured NodeType templates. No nodes will be created in the Content Repository.
*
*/
public function validateCommand(): void
{
$templatesChecked = 0;
/**
* nodeTypeNames as index
* @var array<string, array{processingErrors: ProcessingErrors, dataWasAccessed: bool}> $faultyNodeTypeTemplates
*/
$faultyNodeTypeTemplates = [];
foreach ($this->nodeTypeManager->getNodeTypes(false) as $nodeType) {
$templateConfiguration = $nodeType->getOptions()['template'] ?? null;
if (!$templateConfiguration) {
continue;
}
$processingErrors = ProcessingErrors::create();
/** @var ContentContext $subgraph */
$subgraph = $this->contextFactory->create();
$observableEmptyData = new class ([]) extends \ArrayObject
{
public bool $dataWasAccessed = false;
public function offsetExists($key): bool
{
$this->dataWasAccessed = true;
return false;
}
};
$siteNode = $subgraph->getCurrentSiteNode();
$template = $this->templateConfigurationProcessor->processTemplateConfiguration(
$templateConfiguration,
[
'data' => $observableEmptyData,
'triggeringNode' => $siteNode, // @deprecated
'site' => $siteNode,
'parentNode' => $siteNode,
],
$processingErrors
);
$this->nodeCreationService->createMutatorsForRootTemplate($template, $nodeType, $this->nodeTypeManager, $subgraph, $processingErrors);
if ($processingErrors->hasError()) {
$faultyNodeTypeTemplates[$nodeType->getName()] = ['processingErrors' => $processingErrors, 'dataWasAccessed' => $observableEmptyData->dataWasAccessed];
}
$templatesChecked++;
}
if ($templatesChecked === 0) {
$this->outputLine('<comment>No NodeType templates found.</comment>');
return;
}
if (empty($faultyNodeTypeTemplates)) {
$this->outputLine(sprintf('<success>%d NodeType templates validated.</success>', $templatesChecked));
return;
}
$possiblyFaultyTemplates = count($faultyNodeTypeTemplates);
$this->outputLine(sprintf('<comment>%d of %d NodeType template validated. %d could not be build standalone.</comment>', $templatesChecked - $possiblyFaultyTemplates, $templatesChecked, $possiblyFaultyTemplates));
$this->outputLine();
// sort so the result is deterministic in ci https://github.com/neos/flow-development-collection/issues/3300
ksort($faultyNodeTypeTemplates);
$hasError = false;
foreach ($faultyNodeTypeTemplates as $nodeTypeName => ['processingErrors' => $processingErrors, 'dataWasAccessed' => $dataWasAccessed]) {
if ($dataWasAccessed) {
$this->outputLine(sprintf('<comment>%s</comment> <b>(depends on "data" context)</b>', $nodeTypeName));
} else {
$hasError = true;
$this->outputLine(sprintf('<error>%s</error>', $nodeTypeName));
}
foreach ($processingErrors as $processingError) {
$this->outputLine(' ' . $processingError->toMessage());
$this->outputLine();
}
}
if ($hasError) {
$this->quit(1);
}
}
}