-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
SearchCommand.php_bup
161 lines (139 loc) · 4.89 KB
/
SearchCommand.php_bup
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
<?php
declare(strict_types=1);
namespace Shel\Neos\Terminal\Command;
/**
* This file is part of the Shel.Neos.Terminal package.
*
* (c) 2021 Sebastian Helzle
*
* 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\ContentRepository\Core\Projection\ContentGraph\Node;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cache\CacheManager;
use Neos\Flow\I18n\Translator;
use Neos\Flow\Mvc\Controller\ControllerContext;
use Neos\Neos\Domain\Service\NodeSearchServiceInterface;
use Neos\Neos\Service\LinkingService;
use Shel\Neos\Terminal\Domain\CommandContext;
use Shel\Neos\Terminal\Domain\CommandInvocationResult;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\StringInput;
class SearchCommand implements TerminalCommandInterface
{
/**
* @Flow\Inject
* @var CacheManager
*/
protected $cacheManager;
/**
* @Flow\Inject
* @var Translator
*/
protected $translator;
/**
* @Flow\Inject
* @var LinkingService
*/
protected $linkingService;
/**
* @Flow\Inject
* @var NodeSearchServiceInterface
*/
protected $nodeSearchService;
public static function getCommandName(): string
{
return 'search';
}
public static function getCommandDescription(): string
{
return 'Shel.Neos.Terminal:Main:command.search.description';
}
public static function getCommandUsage(): string
{
return 'search ' . self::getInputDefinition()->getSynopsis();
}
public static function getInputDefinition(): InputDefinition
{
return new InputDefinition([
new InputArgument('searchword', InputArgument::REQUIRED),
new InputOption('contextNode', 'c', InputOption::VALUE_OPTIONAL),
new InputOption('nodeTypes', 'n', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL),
]);
}
public function invokeCommand(string $argument, CommandContext $commandContext): CommandInvocationResult
{
$input = new StringInput($argument);
$input->bind(self::getInputDefinition());
try {
$input->validate();
} catch (RuntimeException $e) {
return new CommandInvocationResult(false, $e->getMessage());
}
$siteNode = $commandContext->getSiteNode();
$documentNode = $commandContext->getDocumentNode();
switch ($input->getOption('contextNode')) {
case 'node':
case 'focusedNode':
$contextNode = $commandContext->getFocusedNode();
break;
case 'document':
case 'documentNode':
$contextNode = $commandContext->getDocumentNode();
break;
default:
$contextNode = $siteNode;
}
if (!$contextNode) {
return new CommandInvocationResult(false,
$this->translator->translateById('command.search.noContext', [], null, null, 'Main',
'Shel.Neos.Terminal'));
}
// The NodeSearchInterface does not yet have a 4th argument for the startingPoint but all known implementations do
$nodes = $this->nodeSearchService->findByProperties(
$input->getArgument('searchword'),
$input->getOption('nodeTypes'),
$contextNode->getContext(),
$contextNode
);
$results = array_map(function ($node) use ($documentNode, $commandContext) {
$breadcrumbs = [];
$parent = $node->getParent();
while ($parent) {
if ($parent->getNodeType()->isOfType('Neos.Neos:Document')) {
$breadcrumbs[] = $parent->getLabel();
}
$parent = $parent->getParent();
}
return [
'label' => $node->getLabel(),
'nodeType' => $node->getNodeType()->getName(),
'breadcrumb' => implode(' / ', array_reverse($breadcrumbs)),
'uri' => $this->getUriForNode($commandContext->getControllerContext(), $documentNode, $documentNode),
];
}, $nodes);
return new CommandInvocationResult(true, $results);
}
protected function getUriForNode(
ControllerContext $controllerContext,
Node $node,
Node $baseNode
): string {
try {
return $this->linkingService->createNodeUri(
$controllerContext,
$node,
$baseNode,
$controllerContext->getRequest()->getFormat(),
true
);
} catch (\Exception $e) {
}
return '';
}
}