2021-12-26 23:40:54 +09:00
|
|
|
#!/usr/bin/env php
|
2020-03-19 00:32:30 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
|
|
|
|
|
|
define('ROOT', dirname(__DIR__));
|
|
|
|
|
|
|
|
require ROOT . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
const types = [
|
2020-07-26 09:18:15 +09:00
|
|
|
'blob' => '',
|
|
|
|
'bool' => 'bool',
|
|
|
|
'char' => 'string',
|
2021-04-28 06:23:00 +09:00
|
|
|
'datetime' => '\DateTimeInterface',
|
|
|
|
'timestamp' => '\DateTimeInterface',
|
2020-07-26 09:18:15 +09:00
|
|
|
'html' => 'string',
|
|
|
|
'int' => 'int',
|
|
|
|
'numeric' => 'float',
|
|
|
|
'serial' => 'int',
|
|
|
|
'text' => 'string',
|
|
|
|
'varchar' => 'string',
|
2020-07-27 12:36:34 +09:00
|
|
|
'phone_number' => 'PhoneNumber',
|
2020-03-19 00:32:30 +09:00
|
|
|
];
|
|
|
|
|
2020-09-08 08:47:50 +09:00
|
|
|
$files = array_merge(glob(ROOT . '/src/Entity/*.php'),
|
|
|
|
array_merge(glob(ROOT . '/components/*/Entity/*.php'),
|
|
|
|
glob(ROOT . '/plugins/*/Entity/*.php')));
|
2020-03-19 00:32:30 +09:00
|
|
|
|
2020-08-09 01:10:57 +09:00
|
|
|
$classes = [];
|
2021-12-27 07:16:19 +09:00
|
|
|
$nullable_no_defaults_warning = [];
|
2020-08-09 01:10:57 +09:00
|
|
|
|
2020-03-19 00:32:30 +09:00
|
|
|
foreach ($files as $file) {
|
2020-07-01 03:19:50 +09:00
|
|
|
|
2020-03-19 00:32:30 +09:00
|
|
|
require_once $file;
|
2020-03-31 02:16:44 +09:00
|
|
|
|
2020-03-31 00:15:31 +09:00
|
|
|
$declared = get_declared_classes();
|
2020-08-09 01:10:57 +09:00
|
|
|
foreach ($declared as $dc) {
|
2020-09-08 08:47:50 +09:00
|
|
|
if (preg_match('/(App|(Component|Plugin)\\\\[^\\\\]+)\\\\Entity/', $dc) && !in_array($dc, $classes)) {
|
2020-08-09 01:10:57 +09:00
|
|
|
$class = $dc;
|
|
|
|
$classes[] = $class;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 00:32:30 +09:00
|
|
|
|
|
|
|
$no_ns_class = preg_replace('/.*?\\\\/', '', $class);
|
|
|
|
$schema = $class::schemaDef();
|
|
|
|
$fields = array_keys($schema['fields']);
|
|
|
|
$fields_code = [];
|
|
|
|
$methods_code = [];
|
|
|
|
foreach ($fields as $field) {
|
2021-12-27 07:08:39 +09:00
|
|
|
$field_schema = $schema['fields'][$field];
|
|
|
|
$nullable = ($field_schema['not null'] ?? false) ? '' : '?';
|
|
|
|
$type = types[$field_schema['type']];
|
2020-03-31 00:15:31 +09:00
|
|
|
$type = $type !== '' ? $nullable . $type : $type;
|
2021-09-18 11:22:27 +09:00
|
|
|
$method_name = str_replace([' ', 'actor'], ['', 'Actor'], ucwords(str_replace('_', ' ', $field)));
|
2021-12-27 07:08:39 +09:00
|
|
|
$length = $field_schema['length'] ?? null;
|
2020-07-25 10:54:18 +09:00
|
|
|
|
2021-12-27 07:08:39 +09:00
|
|
|
$field_setter = "\${$field}";
|
|
|
|
if (\is_int($length)) {
|
|
|
|
if ($nullable === '?') {
|
|
|
|
$field_setter = "\is_null(\${$field}) ? null : \mb_substr(\${$field}, 0, $length)";
|
|
|
|
} else {
|
|
|
|
$field_setter = "\mb_substr(\${$field}, 0, $length)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($nullable === '?' || \array_key_exists('default', $field_schema)) && $type != '\DateTimeInterface') {
|
2021-12-27 07:16:19 +09:00
|
|
|
if (!\array_key_exists('default', $field_schema)) {
|
|
|
|
$nullable_no_defaults_warning[] = "{$class}::{$field}";
|
|
|
|
}
|
2021-12-27 07:08:39 +09:00
|
|
|
$default = $field_schema['default'] ?? null;
|
|
|
|
if (\is_string($default)) {
|
2020-07-25 10:54:18 +09:00
|
|
|
$default = "'{$default}'";
|
2021-12-27 07:08:39 +09:00
|
|
|
} elseif (\is_null($default)) {
|
|
|
|
$default = "null";
|
|
|
|
} elseif ($type === 'bool' || $type === '?bool') {
|
2020-07-25 10:54:18 +09:00
|
|
|
$default = $default ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields_code[] = " private {$type} \${$field} = {$default};";
|
|
|
|
} else {
|
|
|
|
$fields_code[] = " private {$type} \${$field};";
|
|
|
|
}
|
|
|
|
|
2020-08-09 01:10:57 +09:00
|
|
|
$methods_code[] = " public function set{$method_name}({$type} \${$field}): self" .
|
2021-12-26 23:40:54 +09:00
|
|
|
"\n {\n \$this->{$field} = {$field_setter};\n return \$this;\n }" . "\n\n" .
|
2020-08-09 01:10:57 +09:00
|
|
|
" public function get{$method_name}()" . ($type !== '' ? ": {$type}" : '') .
|
|
|
|
"\n {\n return \$this->{$field};\n }" . "\n";
|
2020-03-19 00:32:30 +09:00
|
|
|
}
|
2020-03-31 00:15:31 +09:00
|
|
|
|
|
|
|
$fields_code = implode("\n", $fields_code);
|
2021-12-26 23:40:54 +09:00
|
|
|
$methods_code = implode("\n", $methods_code);
|
2020-03-31 00:15:31 +09:00
|
|
|
|
|
|
|
$begin = '// {{{ Autocode';
|
|
|
|
$end = '// }}} Autocode';
|
|
|
|
$code = "
|
|
|
|
{$begin}
|
2021-05-06 00:56:03 +09:00
|
|
|
// @codeCoverageIgnoreStart
|
2020-03-31 00:15:31 +09:00
|
|
|
{$fields_code}
|
|
|
|
|
|
|
|
{$methods_code}
|
2021-05-06 00:56:03 +09:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-08-09 01:10:57 +09:00
|
|
|
{$end}";
|
2020-03-31 00:15:31 +09:00
|
|
|
|
|
|
|
foreach (['/\\//' => '\\/', '/ /' => '\\ '] as $from => $to) {
|
|
|
|
$begin = preg_replace($from, $to, $begin);
|
|
|
|
$end = preg_replace($from, $to, $end);
|
|
|
|
}
|
2020-03-19 00:32:30 +09:00
|
|
|
|
|
|
|
$in_file = file_get_contents($file);
|
2021-12-26 23:40:54 +09:00
|
|
|
$out_file = preg_replace("%\\s*{$begin}.*{$end}%smu", $code, $in_file);
|
2020-03-19 00:32:30 +09:00
|
|
|
file_put_contents($file, $out_file);
|
|
|
|
}
|
2021-12-27 07:16:19 +09:00
|
|
|
|
|
|
|
if (!empty($nullable_no_defaults_warning)) {
|
|
|
|
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";
|
|
|
|
foreach ($nullable_no_defaults_warning as $n) {
|
|
|
|
echo " {$n}\n";
|
|
|
|
}
|
|
|
|
}
|