update_autocode 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. 'float' => 'float', // TODO REMOVE THIS
  20. ];
  21. $files = array_merge(glob(ROOT . '/src/Entity/*.php'),
  22. array_merge(glob(ROOT . '/components/*/Entity/*.php'),
  23. glob(ROOT . '/plugins/*/Entity/*.php')));
  24. $nullable_no_defaults_warning = [];
  25. foreach ($files as $file) {
  26. require_once $file;
  27. $class = str_replace(['/', 'src', 'components', 'plugins'], ['\\', 'App', 'Component', 'Plugin'], substr($file, strlen(ROOT) + 1, -4));
  28. if (!method_exists($class, 'schemaDef')) {
  29. continue;
  30. }
  31. $no_ns_class = preg_replace('/.*?\\\\/', '', $class);
  32. $schema = $class::schemaDef();
  33. $fields = array_keys($schema['fields']);
  34. $fields_code = [];
  35. $methods_code = [];
  36. foreach ($fields as $field) {
  37. $field_schema = $schema['fields'][$field];
  38. $nullable = ($field_schema['not null'] ?? false) ? '' : '?';
  39. $type = types[$field_schema['type']];
  40. $type = $type !== '' ? $nullable . $type : $type;
  41. $method_name = str_replace([' ', 'actor'], ['', 'Actor'], ucwords(str_replace('_', ' ', $field)));
  42. $length = $field_schema['length'] ?? null;
  43. $field_setter = "\${$field}";
  44. if (\is_int($length)) {
  45. if ($nullable === '?') {
  46. $field_setter = "\is_null(\${$field}) ? null : \mb_substr(\${$field}, 0, $length)";
  47. } else {
  48. $field_setter = "\mb_substr(\${$field}, 0, $length)";
  49. }
  50. }
  51. if (($nullable === '?' || \array_key_exists('default', $field_schema)) && $type != '\DateTimeInterface') {
  52. if (!\array_key_exists('default', $field_schema)) {
  53. $nullable_no_defaults_warning[] = "{$class}::{$field}";
  54. }
  55. $default = $field_schema['default'] ?? null;
  56. if (\is_string($default)) {
  57. $default = "'{$default}'";
  58. } elseif (\is_null($default)) {
  59. $default = "null";
  60. } elseif ($type === 'bool' || $type === '?bool') {
  61. $default = $default ? 'true' : 'false';
  62. }
  63. $fields_code[] = " private {$type} \${$field} = {$default};";
  64. } else {
  65. $fields_code[] = " private {$type} \${$field};";
  66. }
  67. $methods_code[] = " public function set{$method_name}({$type} \${$field}): self" .
  68. "\n {\n \$this->{$field} = {$field_setter};\n return \$this;\n }" . "\n\n" .
  69. " public function get{$method_name}()" . ($type !== '' ? ": {$type}" : '') .
  70. "\n {\n return \$this->{$field};\n }" . "\n";
  71. }
  72. $fields_code = implode("\n", $fields_code);
  73. $methods_code = implode("\n", $methods_code);
  74. $begin = '// {{{ Autocode';
  75. $end = '// }}} Autocode';
  76. $code = "
  77. {$begin}
  78. // @codeCoverageIgnoreStart
  79. {$fields_code}
  80. {$methods_code}
  81. // @codeCoverageIgnoreEnd
  82. {$end}";
  83. foreach (['/\\//' => '\\/', '/ /' => '\\ '] as $from => $to) {
  84. $begin = preg_replace($from, $to, $begin);
  85. $end = preg_replace($from, $to, $end);
  86. }
  87. $in_file = file_get_contents($file);
  88. $out_file = preg_replace("%\\s*{$begin}.*{$end}%smu", $code, $in_file);
  89. file_put_contents($file, $out_file);
  90. }
  91. if (!empty($nullable_no_defaults_warning)) {
  92. 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";
  93. foreach ($nullable_no_defaults_warning as $n) {
  94. echo " {$n}\n";
  95. }
  96. }