5
5
*/
6
6
namespace Magento \Framework \Code \Generator ;
7
7
8
- use Magento \Framework \Exception \FileSystemException ;
9
8
use Zend \Code \Generator \ValueGenerator ;
10
- use Magento \Framework \Filesystem \Driver \File ;
11
- use Magento \Framework \ObjectManagerInterface ;
12
9
13
10
abstract class EntityAbstract
14
11
{
@@ -44,7 +41,7 @@ abstract class EntityAbstract
44
41
/**
45
42
* Class generator object
46
43
*
47
- * @var CodeGeneratorInterface
44
+ * @var \Magento\Framework\Code\Generator\ CodeGeneratorInterface
48
45
*/
49
46
protected $ _classGenerator ;
50
47
@@ -57,20 +54,20 @@ abstract class EntityAbstract
57
54
* @param null|string $sourceClassName
58
55
* @param null|string $resultClassName
59
56
* @param Io $ioObject
60
- * @param CodeGeneratorInterface $classGenerator
57
+ * @param \Magento\Framework\Code\Generator\ CodeGeneratorInterface $classGenerator
61
58
* @param DefinedClasses $definedClasses
62
59
*/
63
60
public function __construct (
64
61
$ sourceClassName = null ,
65
62
$ resultClassName = null ,
66
63
Io $ ioObject = null ,
67
- CodeGeneratorInterface $ classGenerator = null ,
64
+ \ Magento \ Framework \ Code \ Generator \ CodeGeneratorInterface $ classGenerator = null ,
68
65
DefinedClasses $ definedClasses = null
69
66
) {
70
67
if ($ ioObject ) {
71
68
$ this ->_ioObject = $ ioObject ;
72
69
} else {
73
- $ this ->_ioObject = new Io (new File ());
70
+ $ this ->_ioObject = new Io (new \ Magento \ Framework \ Filesystem \ Driver \ File ());
74
71
}
75
72
if ($ classGenerator ) {
76
73
$ this ->_classGenerator = $ classGenerator ;
@@ -109,17 +106,6 @@ public function generate()
109
106
$ this ->_addError ('Can \'t generate source code. ' );
110
107
}
111
108
}
112
- } catch (FileSystemException $ e ) {
113
- $ message = 'Error: an object of a generated class may be a dependency for another object, but this '
114
- . 'dependency has not been defined or set correctly in the signature of the related construct method. '
115
- . 'Due to the current error, executing the CLI commands `bin/magento setup:di:compile` or `bin/magento '
116
- . 'deploy:mode:set production` does not create the required generated classes. '
117
- . 'Magento cannot write a class file to the "generated" directory that is read-only. Before using the '
118
- . 'read-only file system, the classes to be generated must be created beforehand in the "generated" '
119
- . 'directory. For details, see the "File systems access permissions" topic '
120
- . 'at http://devdocs.magento.com. ' ;
121
- $ this ->_addError ($ message );
122
- $ this ->_addError ($ e ->getMessage ());
123
109
} catch (\Exception $ e ) {
124
110
$ this ->_addError ($ e ->getMessage ());
125
111
}
@@ -203,7 +189,7 @@ protected function _getClassProperties()
203
189
'visibility ' => 'protected ' ,
204
190
'docblock ' => [
205
191
'shortDescription ' => 'Object Manager instance ' ,
206
- 'tags ' => [['name ' => 'var ' , 'description ' => '\\' . ObjectManagerInterface::class]],
192
+ 'tags ' => [['name ' => 'var ' , 'description ' => '\\' . \ Magento \ Framework \ ObjectManagerInterface::class]],
207
193
],
208
194
];
209
195
@@ -264,9 +250,9 @@ protected function _validateData()
264
250
$ this ->_addError ('Source class ' . $ sourceClassName . ' doesn \'t exist. ' );
265
251
return false ;
266
252
} elseif (/**
267
- * If makeResultFileDirectory only fails because the file is already created,
268
- * a competing process has generated the file, no exception should be thrown.
269
- */
253
+ * If makeResultFileDirectory only fails because the file is already created,
254
+ * a competing process has generated the file, no exception should be thrown.
255
+ */
270
256
!$ this ->_ioObject ->makeResultFileDirectory ($ resultClassName )
271
257
&& !$ this ->_ioObject ->fileExists ($ resultDir )
272
258
) {
@@ -318,6 +304,59 @@ protected function _getNullDefaultValue()
318
304
return $ value ;
319
305
}
320
306
307
+ /**
308
+ * @param \ReflectionParameter $parameter
309
+ *
310
+ * @return null|string
311
+ */
312
+ private function extractParameterType (
313
+ \ReflectionParameter $ parameter
314
+ ): ?string {
315
+ /** @var string|null $typeName */
316
+ $ typeName = null ;
317
+ if ($ parameter ->hasType ()) {
318
+ if ($ parameter ->isArray ()) {
319
+ $ typeName = 'array ' ;
320
+ } elseif ($ parameter ->getClass ()) {
321
+ $ typeName = $ this ->_getFullyQualifiedClassName (
322
+ $ parameter ->getClass ()->getName ()
323
+ );
324
+ } elseif ($ parameter ->isCallable ()) {
325
+ $ typeName = 'callable ' ;
326
+ } else {
327
+ $ typeName = $ parameter ->getType ()->getName ();
328
+ }
329
+
330
+ if ($ parameter ->allowsNull ()) {
331
+ $ typeName = '? ' .$ typeName ;
332
+ }
333
+ }
334
+
335
+ return $ typeName ;
336
+ }
337
+
338
+ /**
339
+ * @param \ReflectionParameter $parameter
340
+ *
341
+ * @return null|ValueGenerator
342
+ */
343
+ private function extractParameterDefaultValue (
344
+ \ReflectionParameter $ parameter
345
+ ): ?ValueGenerator {
346
+ /** @var ValueGenerator|null $value */
347
+ $ value = null ;
348
+ if ($ parameter ->isOptional () && $ parameter ->isDefaultValueAvailable ()) {
349
+ $ valueType = ValueGenerator::TYPE_AUTO ;
350
+ $ defaultValue = $ parameter ->getDefaultValue ();
351
+ if ($ defaultValue === null ) {
352
+ $ valueType = ValueGenerator::TYPE_NULL ;
353
+ }
354
+ $ value = new ValueGenerator ($ defaultValue , $ valueType );
355
+ }
356
+
357
+ return $ value ;
358
+ }
359
+
321
360
/**
322
361
* Retrieve method parameter info
323
362
*
@@ -329,26 +368,13 @@ protected function _getMethodParameterInfo(\ReflectionParameter $parameter)
329
368
$ parameterInfo = [
330
369
'name ' => $ parameter ->getName (),
331
370
'passedByReference ' => $ parameter ->isPassedByReference (),
332
- 'type ' => $ parameter ->getType ()
371
+ 'variadic ' => $ parameter ->isVariadic ()
333
372
];
334
-
335
- if ($ parameter ->isArray ()) {
336
- $ parameterInfo ['type ' ] = 'array ' ;
337
- } elseif ($ parameter ->getClass ()) {
338
- $ parameterInfo ['type ' ] = $ this ->_getFullyQualifiedClassName ($ parameter ->getClass ()->getName ());
339
- } elseif ($ parameter ->isCallable ()) {
340
- $ parameterInfo ['type ' ] = 'callable ' ;
373
+ if ($ type = $ this ->extractParameterType ($ parameter )) {
374
+ $ parameterInfo ['type ' ] = $ type ;
341
375
}
342
-
343
- if ($ parameter ->isOptional () && $ parameter ->isDefaultValueAvailable ()) {
344
- $ defaultValue = $ parameter ->getDefaultValue ();
345
- if (is_string ($ defaultValue )) {
346
- $ parameterInfo ['defaultValue ' ] = $ parameter ->getDefaultValue ();
347
- } elseif ($ defaultValue === null ) {
348
- $ parameterInfo ['defaultValue ' ] = $ this ->_getNullDefaultValue ();
349
- } else {
350
- $ parameterInfo ['defaultValue ' ] = $ defaultValue ;
351
- }
376
+ if ($ default = $ this ->extractParameterDefaultValue ($ parameter )) {
377
+ $ parameterInfo ['defaultValue ' ] = $ default ;
352
378
}
353
379
354
380
return $ parameterInfo ;
0 commit comments