generate_entity_fields 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env php
  2. <?php
  3. use Symfony\Component\Yaml\Yaml;
  4. define('ROOT', dirname(__DIR__));
  5. require ROOT . '/vendor/autoload.php';
  6. const types = [
  7. 'blob' => '',
  8. 'bool' => 'bool',
  9. 'char' => 'string',
  10. 'datetime' => '\DateTimeInterface',
  11. 'timestamp' => '\DateTimeInterface',
  12. 'html' => 'string',
  13. 'int' => 'int',
  14. 'numeric' => 'float',
  15. 'serial' => 'int',
  16. 'text' => 'string',
  17. 'varchar' => 'string',
  18. 'phone_number' => 'PhoneNumber',
  19. ];
  20. $files = array_merge(glob(ROOT . '/src/Entity/*.php'),
  21. array_merge(glob(ROOT . '/components/*/Entity/*.php'),
  22. glob(ROOT . '/plugins/*/Entity/*.php')));
  23. $classes = [];
  24. $nullable_no_defaults_warning = [];
  25. foreach ($files as $file) {
  26. require_once $file;
  27. $declared = get_declared_classes();
  28. foreach ($declared as $dc) {
  29. if (preg_match('/(App|(Component|Plugin)\\\\[^\\\\]+)\\\\Entity/', $dc) && !in_array($dc, $classes)) {
  30. $class = $dc;
  31. $classes[] = $class;
  32. break;
  33. }
  34. }
  35. $no_ns_class = preg_replace('/.*?\\\\/', '', $class);
  36. $schema = $class::schemaDef();
  37. $fields = array_keys($schema['fields']);
  38. $fields_code = [];
  39. $methods_code = [];
  40. foreach ($fields as $field) {
  41. $field_schema = $schema['fields'][$field];
  42. $nullable = ($field_schema['not null'] ?? false) ? '' : '?';
  43. $type = types[$field_schema['type']];
  44. $type = $type !== '' ? $nullable . $type : $type;
  45. $method_name = str_replace([' ', 'actor'], ['', 'Actor'], ucwords(str_replace('_', ' ', $field)));
  46. $length = $field_schema['length'] ?? null;
  47. $field_setter = "\${$field}";
  48. if (\is_int($length)) {
  49. if ($nullable === '?') {
  50. $field_setter = "\is_null(\${$field}) ? null : \mb_substr(\${$field}, 0, $length)";
  51. } else {
  52. $field_setter = "\mb_substr(\${$field}, 0, $length)";
  53. }
  54. }
  55. if (($nullable === '?' || \array_key_exists('default', $field_schema)) && $type != '\DateTimeInterface') {
  56. if (!\array_key_exists('default', $field_schema)) {
  57. $nullable_no_defaults_warning[] = "{$class}::{$field}";
  58. }
  59. $default = $field_schema['default'] ?? null;
  60. if (\is_string($default)) {
  61. $default = "'{$default}'";
  62. } elseif (\is_null($default)) {
  63. $default = "null";
  64. } elseif ($type === 'bool' || $type === '?bool') {
  65. $default = $default ? 'true' : 'false';
  66. }
  67. $fields_code[] = " private {$type} \${$field} = {$default};";
  68. } else {
  69. $fields_code[] = " private {$type} \${$field};";
  70. }
  71. $methods_code[] = " public function set{$method_name}({$type} \${$field}): self" .
  72. "\n {\n \$this->{$field} = {$field_setter};\n return \$this;\n }" . "\n\n" .
  73. " public function get{$method_name}()" . ($type !== '' ? ": {$type}" : '') .
  74. "\n {\n return \$this->{$field};\n }" . "\n";
  75. }
  76. $fields_code = implode("\n", $fields_code);
  77. $methods_code = implode("\n", $methods_code);
  78. $begin = '// {{{ Autocode';
  79. $end = '// }}} Autocode';
  80. $code = "
  81. {$begin}
  82. // @codeCoverageIgnoreStart
  83. {$fields_code}
  84. {$methods_code}
  85. // @codeCoverageIgnoreEnd
  86. {$end}";
  87. foreach (['/\\//' => '\\/', '/ /' => '\\ '] as $from => $to) {
  88. $begin = preg_replace($from, $to, $begin);
  89. $end = preg_replace($from, $to, $end);
  90. }
  91. $in_file = file_get_contents($file);
  92. $out_file = preg_replace("%\\s*{$begin}.*{$end}%smu", $code, $in_file);
  93. file_put_contents($file, $out_file);
  94. }
  95. if (!empty($nullable_no_defaults_warning)) {
  96. echo "Warning: The following don't have a default value, but we're assigning it `null`. Doctrine might not like this, so update it\n";
  97. foreach ($nullable_no_defaults_warning as $n) {
  98. echo " {$n}\n";
  99. }
  100. }