generate_entity_fields 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/local/bin/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. foreach ($files as $file) {
  25. require_once $file;
  26. $declared = get_declared_classes();
  27. foreach ($declared as $dc) {
  28. if (preg_match('/(App|(Component|Plugin)\\\\[^\\\\]+)\\\\Entity/', $dc) && !in_array($dc, $classes)) {
  29. $class = $dc;
  30. $classes[] = $class;
  31. break;
  32. }
  33. }
  34. $no_ns_class = preg_replace('/.*?\\\\/', '', $class);
  35. $schema = $class::schemaDef();
  36. $fields = array_keys($schema['fields']);
  37. $fields_code = [];
  38. $methods_code = [];
  39. foreach ($fields as $field) {
  40. $nullable = !@$schema['fields'][$field]['not null'] ? '?' : '';
  41. $type = types[$schema['fields'][$field]['type']];
  42. $type = $type !== '' ? $nullable . $type : $type;
  43. $method_name = str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
  44. $default = @$schema['fields'][$field]['default'];
  45. if (isset($default) && $nullable != '?' && $type != 'DateTimeInterface') {
  46. if (is_string($default)) {
  47. $default = "'{$default}'";
  48. } elseif ($type == 'bool') {
  49. $default = $default ? 'true' : 'false';
  50. }
  51. $fields_code[] = " private {$type} \${$field} = {$default};";
  52. } else {
  53. $fields_code[] = " private {$type} \${$field};";
  54. }
  55. $methods_code[] = " public function set{$method_name}({$type} \${$field}): self" .
  56. "\n {\n \$this->{$field} = \${$field};\n return \$this;\n }" . "\n\n" .
  57. " public function get{$method_name}()" . ($type !== '' ? ": {$type}" : '') .
  58. "\n {\n return \$this->{$field};\n }" . "\n";
  59. }
  60. $fields_code = implode("\n", $fields_code);
  61. $methods_code = implode("\n", $methods_code) . "\n";
  62. $begin = '// {{{ Autocode';
  63. $end = '// }}} Autocode';
  64. $code = "
  65. {$begin}
  66. {$fields_code}
  67. {$methods_code}
  68. {$end}";
  69. foreach (['/\\//' => '\\/', '/ /' => '\\ '] as $from => $to) {
  70. $begin = preg_replace($from, $to, $begin);
  71. $end = preg_replace($from, $to, $end);
  72. }
  73. $in_file = file_get_contents($file);
  74. $out_file = preg_replace("/\\s*{$begin}[^\\/]*{$end}/m", $code, $in_file);
  75. file_put_contents($file, $out_file);
  76. }