-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
OperationResolver.php
144 lines (126 loc) · 4.67 KB
/
OperationResolver.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
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
<?php
namespace Neos\Eel\FlowQuery;
/*
* This file is part of the Neos.Eel 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\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Reflection\ReflectionService;
/**
* FlowQuery Operation Resolver
*
* @Flow\Scope("singleton")
*/
class OperationResolver implements OperationResolverInterface
{
/**
* @var ObjectManagerInterface
* @Flow\Inject
*/
protected $objectManager;
/**
* @var ReflectionService
* @Flow\Inject
*/
protected $reflectionService;
/**
* 2-dimensional array of registered operations:
* shortOperationName => priority => operation class name
*
* @var array
*/
protected $operations = [];
/**
* associative array of registered final operations:
* shortOperationName => shortOperationName
*
* @var array
*/
protected $finalOperationNames = [];
/**
* Initializer, building up $this->operations and $this->finalOperationNames
*/
public function initializeObject()
{
$operationsAndFinalOperationNames = static::buildOperationsAndFinalOperationNames($this->objectManager);
$this->operations = $operationsAndFinalOperationNames[0];
$this->finalOperationNames = $operationsAndFinalOperationNames[1];
}
/**
* @param ObjectManagerInterface $objectManager
* @return array Array of sorted operations and array of final operation names
* @throws FlowQueryException
* @Flow\CompileStatic
*/
public static function buildOperationsAndFinalOperationNames($objectManager)
{
$operations = [];
$finalOperationNames = [];
$reflectionService = $objectManager->get(ReflectionService::class);
$operationClassNames = $reflectionService->getAllImplementationClassNamesForInterface(OperationInterface::class);
/** @var $operationClassName OperationInterface */
foreach ($operationClassNames as $operationClassName) {
$shortOperationName = $operationClassName::getShortName();
$operationPriority = $operationClassName::getPriority();
$isFinalOperation = $operationClassName::isFinal();
if (!isset($operations[$shortOperationName])) {
$operations[$shortOperationName] = [];
}
if (isset($operations[$shortOperationName][$operationPriority])) {
throw new FlowQueryException(sprintf('Operation with name "%s" and priority %s is already defined in class %s, and the class %s has the same priority and name.', $shortOperationName, $operationPriority, $operations[$shortOperationName][$operationPriority], $operationClassName), 1332491678);
}
$operations[$shortOperationName][$operationPriority] = $operationClassName;
if ($isFinalOperation) {
$finalOperationNames[$shortOperationName] = $shortOperationName;
}
}
foreach ($operations as &$operation) {
krsort($operation, SORT_NUMERIC);
}
return [$operations, $finalOperationNames];
}
/**
* @param string $operationName
* @return boolean true if $operationName is final
*/
public function isFinalOperation($operationName)
{
return isset($this->finalOperationNames[$operationName]);
}
/**
* Resolve an operation, taking runtime constraints into account.
*
* @param string $operationName
* @param array|mixed $context
* @throws FlowQueryException
* @return OperationInterface the resolved operation
*/
public function resolveOperation($operationName, $context)
{
if (!isset($this->operations[$operationName])) {
throw new FlowQueryException('Operation "' . $operationName . '" not found.', 1332491837);
}
foreach ($this->operations[$operationName] as $operationClassName) {
/** @var OperationInterface $operation */
$operation = $this->objectManager->get($operationClassName);
if ($operation->canEvaluate($context)) {
return $operation;
}
}
throw new FlowQueryException('No operation which satisfies the runtime constraints found for "' . $operationName . '".', 1332491864);
}
/**
* @param string $operationName
* @return boolean
*/
public function hasOperation($operationName)
{
return isset($this->operations[$operationName]);
}
}