-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
PublishingService.php
109 lines (99 loc) · 3.56 KB
/
PublishingService.php
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
<?php
namespace Neos\Neos\Service;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\ContentRepository\Domain\Model\Workspace;
/**
* The workspaces service adds some basic helper methods for getting workspaces,
* unpublished nodes and methods for publishing nodes or whole workspaces.
*
* @api
* @Flow\Scope("singleton")
*/
class PublishingService extends \Neos\ContentRepository\Domain\Service\PublishingService
{
/**
* Publishes the given node to the specified target workspace. If no workspace is specified, the base workspace
* is assumed.
*
* If the given node is a Document or has ContentCollection child nodes, these nodes are published as well.
*
* @param NodeInterface $node
* @param Workspace $targetWorkspace If not set the base workspace is assumed to be the publishing target
* @return void
* @api
*/
public function publishNode(NodeInterface $node, Workspace $targetWorkspace = null)
{
if ($targetWorkspace === null) {
$targetWorkspace = $node->getWorkspace()->getBaseWorkspace();
}
if (!$targetWorkspace instanceof Workspace) {
return;
}
$nodes = [$node];
$nodeType = $node->getNodeType();
if ($nodeType->isOfType('Neos.Neos:Document') || $nodeType->hasConfiguration('childNodes')) {
$nodes = array_merge($nodes, $this->collectAllContentChildNodes($node));
}
$sourceWorkspace = $node->getWorkspace();
$sourceWorkspace->publishNodes($nodes, $targetWorkspace);
$this->emitNodePublished($node, $targetWorkspace);
}
/**
* Discards the given node from its workspace.
*
* If the given node is a Document or has ContentCollection child nodes, these nodes are discarded as well.
*
* @param NodeInterface $node
*/
public function discardNode(NodeInterface $node)
{
$nodes = [$node];
$nodeType = $node->getNodeType();
if ($nodeType->isOfType('Neos.Neos:Document') || $nodeType->hasConfiguration('childNodes')) {
$nodes = array_filter(
array_merge($nodes, $this->collectAllContentChildNodes($node)),
function ($possiblyPublishableNode) use ($node) {
return $possiblyPublishableNode->getWorkspace()->getName() === $node->getWorkspace()->getName();
}
);
}
parent::discardNodes($nodes);
}
/**
* Discards the given nodes.
*
* @param array<\Neos\ContentRepository\Domain\Model\NodeInterface> $nodes The nodes to discard
* @return void
* @api
*/
public function discardNodes(array $nodes)
{
foreach ($nodes as $node) {
$this->discardNode($node);
}
}
/**
* @param NodeInterface $parentNode
* @param array $collectedNodes
* @return array
*/
protected function collectAllContentChildNodes(NodeInterface $parentNode, $collectedNodes = [])
{
foreach ($parentNode->getChildNodes('!Neos.Neos:Document') as $contentNode) {
$collectedNodes[] = $contentNode;
$collectedNodes = array_merge($collectedNodes, $this->collectAllContentChildNodes($contentNode));
}
return $collectedNodes;
}
}