gnu-social/bin/update_autocode

118 lines
4.1 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/env php
<?php
use Symfony\Component\Yaml\Yaml;
define('ROOT', dirname(__DIR__));
require ROOT . '/vendor/autoload.php';
const types = [
'blob' => '',
'bool' => 'bool',
'char' => 'string',
'datetime' => '\DateTimeInterface',
'timestamp' => '\DateTimeInterface',
'html' => 'string',
'int' => 'int',
'numeric' => 'float',
'serial' => 'int',
'text' => 'string',
'varchar' => 'string',
'phone_number' => 'PhoneNumber',
'float' => 'float', // TODO REMOVE THIS
];
$files = array_merge(glob(ROOT . '/src/Entity/*.php'),
array_merge(glob(ROOT . '/components/*/Entity/*.php'),
glob(ROOT . '/plugins/*/Entity/*.php')));
$nullable_no_defaults_warning = [];
2020-08-08 17:10:57 +01:00
foreach ($files as $file) {
require_once $file;
$class = str_replace(['/', 'src', 'components', 'plugins'], ['\\', 'App', 'Component', 'Plugin'], substr($file, strlen(ROOT) + 1, -4));
if (!method_exists($class, 'schemaDef')) {
continue;
2020-08-08 17:10:57 +01:00
}
$no_ns_class = preg_replace('/.*?\\\\/', '', $class);
$schema = $class::schemaDef();
$fields = array_keys($schema['fields']);
$fields_code = [];
$methods_code = [];
foreach ($fields as $field) {
$field_schema = $schema['fields'][$field];
$nullable = ($field_schema['not null'] ?? false) ? '' : '?';
$type = types[$field_schema['type']];
$type = $type !== '' ? $nullable . $type : $type;
2021-09-18 03:22:27 +01:00
$method_name = str_replace([' ', 'actor'], ['', 'Actor'], ucwords(str_replace('_', ' ', $field)));
$length = $field_schema['length'] ?? null;
$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') {
if (!\array_key_exists('default', $field_schema)) {
$nullable_no_defaults_warning[] = "{$class}::{$field}";
}
$default = $field_schema['default'] ?? null;
if (\is_string($default)) {
$default = "'{$default}'";
} elseif (\is_null($default)) {
$default = "null";
} elseif ($type === 'bool' || $type === '?bool') {
$default = $default ? 'true' : 'false';
}
$fields_code[] = " private {$type} \${$field} = {$default};";
} else {
$fields_code[] = " private {$type} \${$field};";
}
2020-08-08 17:10:57 +01:00
$methods_code[] = " public function set{$method_name}({$type} \${$field}): self" .
"\n {\n \$this->{$field} = {$field_setter};\n return \$this;\n }" . "\n\n" .
2020-08-08 17:10:57 +01:00
" public function get{$method_name}()" . ($type !== '' ? ": {$type}" : '') .
"\n {\n return \$this->{$field};\n }" . "\n";
}
$fields_code = implode("\n", $fields_code);
$methods_code = implode("\n", $methods_code);
$begin = '// {{{ Autocode';
$end = '// }}} Autocode';
$code = "
{$begin}
// @codeCoverageIgnoreStart
{$fields_code}
{$methods_code}
// @codeCoverageIgnoreEnd
2020-08-08 17:10:57 +01:00
{$end}";
foreach (['/\\//' => '\\/', '/ /' => '\\ '] as $from => $to) {
$begin = preg_replace($from, $to, $begin);
$end = preg_replace($from, $to, $end);
}
$in_file = file_get_contents($file);
$out_file = preg_replace("%\\s*{$begin}.*{$end}%smu", $code, $in_file);
file_put_contents($file, $out_file);
}
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";
}
}