9
9
use Magento \Framework \Api \Code \Generator \ExtensionAttributesInterfaceGenerator ;
10
10
use Magento \Framework \ObjectManager \Code \Generator \Factory as FactoryGenerator ;
11
11
use Magento \Setup \Module \Di \Compiler \Log \Log ;
12
+ use \Magento \Framework \Reflection \TypeProcessor ;
12
13
13
14
class PhpScanner implements ScannerInterface
14
15
{
@@ -18,11 +19,21 @@ class PhpScanner implements ScannerInterface
18
19
protected $ _log ;
19
20
20
21
/**
22
+ * @var TypeProcessor
23
+ */
24
+ private $ typeProcessor ;
25
+
26
+ /**
27
+ * Initialize dependencies.
28
+ *
21
29
* @param Log $log
30
+ * @param TypeProcessor|null $typeProcessor
22
31
*/
23
- public function __construct (Log $ log )
32
+ public function __construct (Log $ log, TypeProcessor $ typeProcessor = null )
24
33
{
25
34
$ this ->_log = $ log ;
35
+ $ this ->typeProcessor = $ typeProcessor
36
+ ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (TypeProcessor::class);
26
37
}
27
38
28
39
/**
@@ -45,22 +56,9 @@ protected function _findMissingClasses($file, $classReflection, $methodName, $en
45
56
preg_match ('/\[\s\<\w+?>\s([\w \\\\]+)/s ' , $ parameter ->__toString (), $ matches );
46
57
if (isset ($ matches [1 ]) && substr ($ matches [1 ], -strlen ($ entityType )) == $ entityType ) {
47
58
$ missingClassName = $ matches [1 ];
48
- try {
49
- if (class_exists ($ missingClassName )) {
50
- continue ;
51
- }
52
- } catch (\RuntimeException $ e ) {
53
- }
54
- $ sourceClassName = $ this ->getSourceClassName ($ missingClassName , $ entityType );
55
- if (!class_exists ($ sourceClassName ) && !interface_exists ($ sourceClassName )) {
56
- $ this ->_log ->add (
57
- Log::CONFIGURATION_ERROR ,
58
- $ missingClassName ,
59
- "Invalid {$ entityType } for nonexistent class {$ sourceClassName } in file {$ file }"
60
- );
61
- continue ;
59
+ if ($ this ->shouldGenerateClass ($ missingClassName , $ entityType , $ file )) {
60
+ $ missingClasses [] = $ missingClassName ;
62
61
}
63
- $ missingClasses [] = $ missingClassName ;
64
62
}
65
63
}
66
64
}
@@ -137,12 +135,18 @@ protected function _fetchFactories($reflectionClass, $file)
137
135
*/
138
136
protected function _fetchMissingExtensionAttributesClasses ($ reflectionClass , $ file )
139
137
{
140
- $ missingExtensionInterfaces = $ this ->_findMissingClasses (
141
- $ file ,
142
- $ reflectionClass ,
143
- 'setExtensionAttributes ' ,
144
- ucfirst (\Magento \Framework \Api \Code \Generator \ExtensionAttributesInterfaceGenerator::ENTITY_TYPE )
145
- );
138
+ $ missingExtensionInterfaces = [];
139
+ $ methodName = 'getExtensionAttributes ' ;
140
+ $ entityType = ucfirst (\Magento \Framework \Api \Code \Generator \ExtensionAttributesInterfaceGenerator::ENTITY_TYPE );
141
+ if ($ reflectionClass ->hasMethod ($ methodName ) && $ reflectionClass ->isInterface ()) {
142
+ $ returnType = $ this ->typeProcessor ->getGetterReturnType (
143
+ (new \Zend \Code \Reflection \ClassReflection ($ reflectionClass ->getName ()))->getMethod ($ methodName )
144
+ );
145
+ $ missingClassName = $ returnType ['type ' ];
146
+ if ($ this ->shouldGenerateClass ($ missingClassName , $ entityType , $ file )) {
147
+ $ missingExtensionInterfaces [] = $ missingClassName ;
148
+ }
149
+ }
146
150
$ missingExtensionClasses = [];
147
151
$ missingExtensionFactories = [];
148
152
foreach ($ missingExtensionInterfaces as $ missingExtensionInterface ) {
@@ -244,4 +248,32 @@ protected function _getDeclaredClasses($file)
244
248
}
245
249
return array_unique ($ classes );
246
250
}
251
+
252
+ /**
253
+ * Check if specified class is missing and if it can be generated.
254
+ *
255
+ * @param string $missingClassName
256
+ * @param string $entityType
257
+ * @param string $file
258
+ * @return bool
259
+ */
260
+ private function shouldGenerateClass ($ missingClassName , $ entityType , $ file )
261
+ {
262
+ try {
263
+ if (class_exists ($ missingClassName )) {
264
+ return false ;
265
+ }
266
+ } catch (\RuntimeException $ e ) {
267
+ }
268
+ $ sourceClassName = $ this ->getSourceClassName ($ missingClassName , $ entityType );
269
+ if (!class_exists ($ sourceClassName ) && !interface_exists ($ sourceClassName )) {
270
+ $ this ->_log ->add (
271
+ Log::CONFIGURATION_ERROR ,
272
+ $ missingClassName ,
273
+ "Invalid {$ entityType } for nonexistent class {$ sourceClassName } in file {$ file }"
274
+ );
275
+ return false ;
276
+ }
277
+ return true ;
278
+ }
247
279
}
0 commit comments