|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Flowpack\NodeTemplates\NodeTemplateDumper; |
| 6 | + |
| 7 | +use Neos\ContentRepository\Domain\Model\ArrayPropertyCollection; |
| 8 | +use Neos\ContentRepository\Domain\Model\NodeInterface; |
| 9 | +use Neos\Flow\Annotations as Flow; |
| 10 | +use Neos\Flow\I18n\EelHelper\TranslationHelper; |
| 11 | +use Symfony\Component\Yaml\Yaml; |
| 12 | + |
| 13 | +/** @Flow\Scope("singleton") */ |
| 14 | +class NodeTemplateDumper |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var TranslationHelper |
| 18 | + * @Flow\Inject |
| 19 | + */ |
| 20 | + protected $translationHelper; |
| 21 | + |
| 22 | + /** |
| 23 | + * Dump the node tree structure into a NodeTemplate YAML structure. |
| 24 | + * References to Nodes and non-primitive property values are commented out in the YAML. |
| 25 | + * |
| 26 | + * @param NodeInterface $startingNode specified root node of the node tree to dump |
| 27 | + * @return string YAML representation of the node template |
| 28 | + */ |
| 29 | + public function createNodeTemplateYamlDumpFromSubtree(NodeInterface $startingNode): string |
| 30 | + { |
| 31 | + $comments = Comments::empty(); |
| 32 | + |
| 33 | + $nodeType = $startingNode->getNodeType(); |
| 34 | + |
| 35 | + if ( |
| 36 | + !$nodeType->isOfType('Neos.Neos:Document') |
| 37 | + && !$nodeType->isOfType('Neos.Neos:Content') |
| 38 | + && !$nodeType->isOfType('Neos.Neos:ContentCollection') |
| 39 | + ) { |
| 40 | + throw new \InvalidArgumentException("Node {$startingNode->getIdentifier()} must be one of Neos.Neos:Document,Neos.Neos:Content,Neos.Neos:ContentCollection."); |
| 41 | + } |
| 42 | + |
| 43 | + $template = $this->nodeTemplateFromNodes([$startingNode], $comments); |
| 44 | + |
| 45 | + foreach ($template as $firstEntry) { |
| 46 | + break; |
| 47 | + } |
| 48 | + assert(isset($firstEntry)); |
| 49 | + |
| 50 | + $templateInNodeTypeOptions = [ |
| 51 | + $nodeType->getName() => [ |
| 52 | + 'options' => [ |
| 53 | + 'template' => array_filter([ |
| 54 | + 'properties' => $firstEntry['properties'] ?? null, |
| 55 | + 'childNodes' => $firstEntry['childNodes'] ?? null, |
| 56 | + ]) |
| 57 | + ] |
| 58 | + ] |
| 59 | + ]; |
| 60 | + |
| 61 | + $yamlWithSerializedComments = Yaml::dump($templateInNodeTypeOptions, 99, 2, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_NULL_AS_TILDE); |
| 62 | + |
| 63 | + return $comments->renderCommentsInYamlDump($yamlWithSerializedComments); |
| 64 | + } |
| 65 | + |
| 66 | + /** @param array<NodeInterface> $nodes */ |
| 67 | + private function nodeTemplateFromNodes(array $nodes, Comments $comments): array |
| 68 | + { |
| 69 | + $documentNodeTemplates = []; |
| 70 | + $contentNodeTemplates = []; |
| 71 | + foreach ($nodes as $index => $node) { |
| 72 | + assert($node instanceof NodeInterface); |
| 73 | + $nodeType = $node->getNodeType(); |
| 74 | + $isDocumentNode = $nodeType->isOfType('Neos.Neos:Document'); |
| 75 | + |
| 76 | + $templatePart = array_filter([ |
| 77 | + 'properties' => $this->nonDefaultConfiguredNodeProperties($node, $comments), |
| 78 | + 'childNodes' => $this->nodeTemplateFromNodes( |
| 79 | + $isDocumentNode |
| 80 | + ? $node->getChildNodes('Neos.Neos:Content,Neos.Neos:ContentCollection,Neos.Neos:Document') |
| 81 | + : $node->getChildNodes('Neos.Neos:Content,Neos.Neos:ContentCollection'), |
| 82 | + $comments |
| 83 | + ) |
| 84 | + ]); |
| 85 | + |
| 86 | + if ($templatePart === []) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if ($isDocumentNode) { |
| 91 | + if ($node->isTethered()) { |
| 92 | + $documentNodeTemplates[$node->getLabel() ?: $node->getName()] = array_merge([ |
| 93 | + 'name' => $node->getName() |
| 94 | + ], $templatePart); |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + $documentNodeTemplates["page$index"] = array_merge([ |
| 99 | + 'type' => $node->getNodeType()->getName() |
| 100 | + ], $templatePart); |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + if ($node->isTethered()) { |
| 105 | + $contentNodeTemplates[$node->getLabel() ?: $node->getName()] = array_merge([ |
| 106 | + 'name' => $node->getName() |
| 107 | + ], $templatePart); |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + $contentNodeTemplates["content$index"] = array_merge([ |
| 112 | + 'type' => $node->getNodeType()->getName() |
| 113 | + ], $templatePart); |
| 114 | + } |
| 115 | + |
| 116 | + return array_merge($contentNodeTemplates, $documentNodeTemplates); |
| 117 | + } |
| 118 | + |
| 119 | + private function nonDefaultConfiguredNodeProperties(NodeInterface $node, Comments $comments): array |
| 120 | + { |
| 121 | + $nodeType = $node->getNodeType(); |
| 122 | + $nodeProperties = $node->getProperties(); |
| 123 | + |
| 124 | + $filteredProperties = []; |
| 125 | + foreach ($nodeType->getProperties() as $propertyName => $configuration) { |
| 126 | + if ( |
| 127 | + $nodeProperties instanceof ArrayPropertyCollection |
| 128 | + ? !$nodeProperties->offsetExists($propertyName) |
| 129 | + : !array_key_exists($propertyName, $nodeProperties) |
| 130 | + ) { |
| 131 | + // node doesn't have the property set |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + if ( |
| 136 | + array_key_exists('defaultValue', $configuration) |
| 137 | + && $configuration['defaultValue'] === $nodeProperties[$propertyName] |
| 138 | + ) { |
| 139 | + // node property is the same as default |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + $propertyValue = $nodeProperties[$propertyName]; |
| 144 | + if ($propertyValue === null || $propertyValue === []) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + if (is_string($propertyValue) && trim($propertyValue) === '') { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + $label = $configuration['ui']['label'] ?? null; |
| 152 | + $augmentCommentWithLabel = fn (Comment $comment) => $comment; |
| 153 | + if ($label) { |
| 154 | + $label = $this->translationHelper->translate($label); |
| 155 | + $augmentCommentWithLabel = fn (Comment $comment) => Comment::fromRenderer( |
| 156 | + function ($indentation, $propertyName) use($comment, $propertyValue, $label) { |
| 157 | + return $indentation . '# ' . $label . "\n" . |
| 158 | + $comment->toYamlComment($indentation, $propertyName); |
| 159 | + } |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + if ($dataSourceIdentifier = $configuration['ui']['inspector']['editorOptions']['dataSourceIdentifier'] ?? null) { |
| 164 | + $filteredProperties[$propertyName] = $comments->addCommentAndGetMarker($augmentCommentWithLabel(Comment::fromRenderer( |
| 165 | + function ($indentation, $propertyName) use ($dataSourceIdentifier, $propertyValue) { |
| 166 | + return $indentation . '# ' . $propertyName . ' -> Datasource "' . $dataSourceIdentifier . '" with value ' . $this->valueToDebugString($propertyValue); |
| 167 | + } |
| 168 | + ))); |
| 169 | + continue; |
| 170 | + } |
| 171 | + |
| 172 | + if (($configuration['type'] ?? null) === 'reference') { |
| 173 | + $nodeTypesInReference = $configuration['ui']['inspector']['editorOptions']['nodeTypes'] ?? ['Neos.Neos:Document']; |
| 174 | + $filteredProperties[$propertyName] = $comments->addCommentAndGetMarker($augmentCommentWithLabel(Comment::fromRenderer( |
| 175 | + function ($indentation, $propertyName) use ($nodeTypesInReference, $propertyValue) { |
| 176 | + return $indentation . '# ' . $propertyName . ' -> Reference of NodeTypes (' . join(', ', $nodeTypesInReference) . ') with value ' . $this->valueToDebugString($propertyValue); |
| 177 | + } |
| 178 | + ))); |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + if (($configuration['ui']['inspector']['editor'] ?? null) === 'Neos.Neos/Inspector/Editors/SelectBoxEditor') { |
| 183 | + $selectBoxValues = array_keys($configuration['ui']['inspector']['editorOptions']['values'] ?? []); |
| 184 | + $filteredProperties[$propertyName] = $comments->addCommentAndGetMarker($augmentCommentWithLabel(Comment::fromRenderer( |
| 185 | + function ($indentation, $propertyName) use ($selectBoxValues, $propertyValue) { |
| 186 | + return $indentation . '# ' . $propertyName . ' -> SelectBox of ' |
| 187 | + . mb_strimwidth(json_encode($selectBoxValues), 0, 60, ' ...]') |
| 188 | + . ' with value ' . $this->valueToDebugString($propertyValue); |
| 189 | + } |
| 190 | + ))); |
| 191 | + continue; |
| 192 | + } |
| 193 | + |
| 194 | + if (is_object($propertyValue) || (is_array($propertyValue) && is_object(array_values($propertyValue)[0] ?? null))) { |
| 195 | + $filteredProperties[$propertyName] = $comments->addCommentAndGetMarker($augmentCommentWithLabel(Comment::fromRenderer( |
| 196 | + function ($indentation, $propertyName) use ($propertyValue) { |
| 197 | + return $indentation . '# ' . $propertyName . ' -> ' . $this->valueToDebugString($propertyValue); |
| 198 | + } |
| 199 | + ))); |
| 200 | + continue; |
| 201 | + } |
| 202 | + |
| 203 | + $filteredProperties[$propertyName] = $comments->addCommentAndGetMarker($augmentCommentWithLabel(Comment::fromRenderer( |
| 204 | + function ($indentation, $propertyName) use ($propertyValue) { |
| 205 | + return $indentation . $propertyName . ': ' . Yaml::dump($propertyValue); |
| 206 | + } |
| 207 | + ))); |
| 208 | + } |
| 209 | + |
| 210 | + return $filteredProperties; |
| 211 | + } |
| 212 | + |
| 213 | + private function valueToDebugString($value): string |
| 214 | + { |
| 215 | + if ($value instanceof NodeInterface) { |
| 216 | + return 'Node(' . $value->getIdentifier() . ')'; |
| 217 | + } |
| 218 | + if (is_iterable($value)) { |
| 219 | + $name = null; |
| 220 | + $entries = []; |
| 221 | + foreach ($value as $key => $item) { |
| 222 | + if ($item instanceof NodeInterface) { |
| 223 | + if ($name === null || $name === 'Nodes') { |
| 224 | + $name = 'Nodes'; |
| 225 | + } else { |
| 226 | + $name = 'array'; |
| 227 | + } |
| 228 | + $entries[$key] = $item->getIdentifier(); |
| 229 | + continue; |
| 230 | + } |
| 231 | + $name = 'array'; |
| 232 | + $entries[$key] = is_object($item) ? get_class($item) : json_encode($item); |
| 233 | + } |
| 234 | + return $name . '(' . join(', ', $entries) . ')'; |
| 235 | + } |
| 236 | + |
| 237 | + if (is_object($value)) { |
| 238 | + return 'object(' . get_class($value) . ')'; |
| 239 | + } |
| 240 | + return json_encode($value); |
| 241 | + } |
| 242 | +} |
0 commit comments