|
2 | 2 |
|
3 | 3 | namespace Doctrine\Common\Proxy; |
4 | 4 |
|
| 5 | +use BackedEnum; |
5 | 6 | use Doctrine\Common\Proxy\Exception\InvalidArgumentException; |
6 | 7 | use Doctrine\Common\Proxy\Exception\UnexpectedValueException; |
7 | 8 | use Doctrine\Common\Util\ClassUtils; |
|
19 | 20 | use function array_key_exists; |
20 | 21 | use function array_map; |
21 | 22 | use function array_slice; |
| 23 | +use function array_unique; |
22 | 24 | use function assert; |
23 | 25 | use function call_user_func; |
24 | 26 | use function chmod; |
|
27 | 29 | use function explode; |
28 | 30 | use function file; |
29 | 31 | use function file_put_contents; |
| 32 | +use function get_class; |
30 | 33 | use function implode; |
31 | 34 | use function in_array; |
32 | 35 | use function interface_exists; |
|
53 | 56 | use function var_export; |
54 | 57 |
|
55 | 58 | use const DIRECTORY_SEPARATOR; |
| 59 | +use const PHP_VERSION_ID; |
56 | 60 |
|
57 | 61 | /** |
58 | 62 | * This factory is used to generate proxy classes. |
@@ -98,7 +102,7 @@ class ProxyGenerator |
98 | 102 | protected $proxyClassTemplate = '<?php |
99 | 103 |
|
100 | 104 | namespace <namespace>; |
101 | | -
|
| 105 | +<enumUseStatements> |
102 | 106 | /** |
103 | 107 | * DO NOT EDIT THIS FILE - IT WAS CREATED BY DOCTRINE\'S PROXY GENERATOR |
104 | 108 | */ |
@@ -381,6 +385,43 @@ private function generateNamespace(ClassMetadata $class) |
381 | 385 | return strrev($parts[1]); |
382 | 386 | } |
383 | 387 |
|
| 388 | + /** |
| 389 | + * Enums must have a use statement when used as public property defaults. |
| 390 | + */ |
| 391 | + public function generateEnumUseStatements(ClassMetadata $class): string |
| 392 | + { |
| 393 | + if (PHP_VERSION_ID < 80100) { |
| 394 | + return "\n"; |
| 395 | + } |
| 396 | + |
| 397 | + $defaultProperties = $class->getReflectionClass()->getDefaultProperties(); |
| 398 | + $lazyLoadedPublicProperties = $this->getLazyLoadedPublicPropertiesNames($class); |
| 399 | + $enumClasses = []; |
| 400 | + |
| 401 | + foreach ($class->getReflectionClass()->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
| 402 | + $name = $property->getName(); |
| 403 | + |
| 404 | + if (! in_array($name, $lazyLoadedPublicProperties, true)) { |
| 405 | + continue; |
| 406 | + } |
| 407 | + |
| 408 | + if (array_key_exists($name, $defaultProperties) && $defaultProperties[$name] instanceof BackedEnum) { |
| 409 | + $enumClassNameParts = explode('\\', get_class($defaultProperties[$name])); |
| 410 | + $enumClasses[] = $enumClassNameParts[0]; |
| 411 | + } |
| 412 | + } |
| 413 | + |
| 414 | + return implode( |
| 415 | + "\n", |
| 416 | + array_map( |
| 417 | + static function ($className) { |
| 418 | + return 'use ' . $className . ';'; |
| 419 | + }, |
| 420 | + array_unique($enumClasses) |
| 421 | + ) |
| 422 | + ) . "\n"; |
| 423 | + } |
| 424 | + |
384 | 425 | /** |
385 | 426 | * Generates the original class name. |
386 | 427 | * |
|
0 commit comments