9 use Magento\Framework\Code\Generator\Io;
14 use Zend\Code\Reflection\MethodReflection;
34 private $serviceMethodsMap;
39 private $reflectionGenerator;
57 ServiceMethodsMap $serviceMethodsMap,
58 RemoteServiceReader $communicationRemoteServiceReader,
59 $sourceClassName =
null,
60 $resultClassName =
null,
62 \
Magento\Framework\Code\Generator\CodeGeneratorInterface $classGenerator =
null,
66 $this->serviceMethodsMap = $serviceMethodsMap;
82 'name' =>
'__construct',
84 [
'name' =>
'publisher',
'type' =>
'\\' . \Magento\Framework\MessageQueue\PublisherInterface::class],
86 'body' =>
"\$this->publisher = \$publisher;",
88 'shortDescription' =>
'Initialize dependencies.',
92 'description' =>
'\Magento\Framework\MessageQueue\PublisherInterface $publisher',
106 'name' =>
'publisher',
107 'visibility' =>
'protected',
109 'shortDescription' =>
'Publisher',
113 'description' =>
'\\' . \Magento\Framework\MessageQueue\PublisherInterface::class,
127 $interfaceMethodsMap = $this->serviceMethodsMap->getMethodsMap($this->
getSourceClassName());
128 foreach (array_keys($interfaceMethodsMap) as $methodName) {
131 $sourceMethodParameters = $methodReflection->getParameters();
132 $methodParameters = [];
133 $topicParameters = [];
135 foreach ($sourceMethodParameters as $methodParameter) {
136 $parameterName = $methodParameter->getName();
138 'name' => $parameterName,
139 'type' => $methodParameter->getType(),
141 if ($methodParameter->isDefaultValueAvailable()) {
142 $parameter[
'defaultValue'] = $methodParameter->getDefaultValue() !==
null 145 $methodParameters[] = $parameter;
146 $topicParameters[] =
"'{$parameterName}' => \${$parameterName}";
148 $topicName = $this->getReflectionGenerator()->generateTopicName($this->
getSourceClassName(), $methodName);
149 $topicConfig = $this->communicationConfig->getTopic($topicName);
150 $methodBody = $topicConfig[CommunicationConfig::TOPIC_IS_SYNCHRONOUS] ?
'return ' :
'';
151 $methodBody .=
"\$this->publisher->publish(\n" 152 .
" '{$topicName}',\n" 153 .
" [" . implode(
', ', $topicParameters) .
"]\n" 155 $annotations = [[
'name' =>
'inheritdoc']];
157 'name' => $methodName,
158 'returnType' => $methodReflection->getReturnType(),
159 'parameters' => $methodParameters,
160 'body' => $methodBody,
161 'docblock' => [
'tags' => $annotations],
174 return parent::_validateData() && $classNameValidationResults;
183 return parent::_generateCode();
196 $interfaceSuffix =
'Interface';
197 if (substr($sourceClassName, -strlen($interfaceSuffix)) !== $interfaceSuffix) {
200 'Remote service class "%s" should be set as preference for an interface, "%s" given',
207 if ($resultClassName !== $expectedResultClassName) {
209 'Invalid remote service class name [' . $resultClassName .
']. Use ' . $expectedResultClassName
223 private function getReflectionGenerator()
225 if ($this->reflectionGenerator ===
null) {
227 ->get(ReflectionGenerator::class);
229 return $this->reflectionGenerator;
_getDefaultConstructorDefinition()
const REMOTE_SERVICE_SUFFIX
validateResultClassName()
__construct(CommunicationConfig $communicationConfig, ServiceMethodsMap $serviceMethodsMap, RemoteServiceReader $communicationRemoteServiceReader, $sourceClassName=null, $resultClassName=null, Io $ioObject=null, \Magento\Framework\Code\Generator\CodeGeneratorInterface $classGenerator=null, DefinedClasses $definedClasses=null)