[DOCTRINE][SCRIPT] Created a script to generate the class fields and accessors from the schema definition

This commit is contained in:
Hugo Sales 2020-03-18 15:32:30 +00:00 committed by Hugo Sales
parent 1b45936f19
commit e3c0669b37
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 395 additions and 1 deletions

67
bin/generate_entity_fields Executable file
View File

@ -0,0 +1,67 @@
#!/usr/bin/php
<?php
use Symfony\Component\Yaml\Yaml;
define('ROOT', dirname(__DIR__));
require ROOT . '/vendor/autoload.php';
const types = [
'blob' => '',
'bool' => 'bool',
'char' => 'string',
'datetime' => 'DateTime',
'html' => 'string',
'int' => 'int',
'numeric' => 'float',
'serial' => 'int',
'text' => 'string',
'varchar' => 'string',
];
$path = Yaml::parseFile(ROOT . '/config/services.yaml')['services']['app.util.schemadef_driver']['arguments'][0];
$path = str_replace('%kernel.project_dir%', ROOT, $path);
$files = glob($path . '/*.php');
foreach ($files as $file) {
require_once $file;
$classes = array_filter(get_declared_classes(),
function ($class) {
return method_exists($class, 'schemaDef');
});
$class = $classes[array_keys($classes)[0]];
$no_ns_class = preg_replace('/.*?\\\\/', '', $class);
$schema = $class::schemaDef();
$fields = array_keys($schema['fields']);
$fields_code = [];
$methods_code = [];
foreach ($fields as $field) {
$nullable = !@$schema['fields'][$field]['not null'] ? '?' : '';
$type = $nullable . types[$schema['fields'][$field]['type']];
$method_name = str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
$fields_code[] = " private {$type} \${$field};";
$methods_code[] = " public function set{$method_name}({$type} \${$field}): {$no_ns_class} " .
"{ \$this->{$field} = \${$field}; return \$this; }" . "\n" .
" public function get{$method_name}(): {$type} " .
"{ return \$this->{$field}; }" . "\n";
}
$begin = '// AUTOCODE BEGIN';
$end = '// AUTOCODE END';
$code = "\n\n" . ' ' . $begin . "\n\n" .
implode("\n", $fields_code) .
"\n\n" .
implode("\n", $methods_code) . "\n" .
' ' . $end . "\n";
$begin = preg_replace('/\\//', '\\/', preg_replace('/ /', '\\ ', $begin));
$end = preg_replace('/\\//', '\\/', preg_replace('/ /', '\\ ', $end));
$in_file = file_get_contents($file);
$out_file = preg_replace("/\\s*{$begin}[^\\/]*{$end}/m", $code, $in_file);
file_put_contents($file, $out_file);
}

View File

@ -4,7 +4,334 @@ namespace App\Entity;
class User
{
public $id;
// AUTOCODE BEGIN
private int $id;
private ?string $nickname;
private ?string $password;
private ?string $email;
private ?string $incomingemail;
private ?bool $emailnotifysub;
private ?int $emailnotifyfav;
private ?bool $emailnotifynudge;
private ?bool $emailnotifymsg;
private ?bool $emailnotifyattn;
private ?string $language;
private ?string $timezone;
private ?bool $emailpost;
private ?string $sms;
private ?int $carrier;
private ?bool $smsnotify;
private ?bool $smsreplies;
private ?string $smsemail;
private ?string $uri;
private ?bool $autosubscribe;
private ?int $subscribe_policy;
private ?string $urlshorteningservice;
private ?bool $private_stream;
private DateTime $created;
private DateTime $modified;
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
public function getId(): int
{
return $this->id;
}
public function setNickname(?string $nickname): self
{
$this->nickname = $nickname;
return $this;
}
public function getNickname(): ?string
{
return $this->nickname;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setIncomingemail(?string $incomingemail): self
{
$this->incomingemail = $incomingemail;
return $this;
}
public function getIncomingemail(): ?string
{
return $this->incomingemail;
}
public function setEmailnotifysub(?bool $emailnotifysub): self
{
$this->emailnotifysub = $emailnotifysub;
return $this;
}
public function getEmailnotifysub(): ?bool
{
return $this->emailnotifysub;
}
public function setEmailnotifyfav(?int $emailnotifyfav): self
{
$this->emailnotifyfav = $emailnotifyfav;
return $this;
}
public function getEmailnotifyfav(): ?int
{
return $this->emailnotifyfav;
}
public function setEmailnotifynudge(?bool $emailnotifynudge): self
{
$this->emailnotifynudge = $emailnotifynudge;
return $this;
}
public function getEmailnotifynudge(): ?bool
{
return $this->emailnotifynudge;
}
public function setEmailnotifymsg(?bool $emailnotifymsg): self
{
$this->emailnotifymsg = $emailnotifymsg;
return $this;
}
public function getEmailnotifymsg(): ?bool
{
return $this->emailnotifymsg;
}
public function setEmailnotifyattn(?bool $emailnotifyattn): self
{
$this->emailnotifyattn = $emailnotifyattn;
return $this;
}
public function getEmailnotifyattn(): ?bool
{
return $this->emailnotifyattn;
}
public function setLanguage(?string $language): self
{
$this->language = $language;
return $this;
}
public function getLanguage(): ?string
{
return $this->language;
}
public function setTimezone(?string $timezone): self
{
$this->timezone = $timezone;
return $this;
}
public function getTimezone(): ?string
{
return $this->timezone;
}
public function setEmailpost(?bool $emailpost): self
{
$this->emailpost = $emailpost;
return $this;
}
public function getEmailpost(): ?bool
{
return $this->emailpost;
}
public function setSms(?string $sms): self
{
$this->sms = $sms;
return $this;
}
public function getSms(): ?string
{
return $this->sms;
}
public function setCarrier(?int $carrier): self
{
$this->carrier = $carrier;
return $this;
}
public function getCarrier(): ?int
{
return $this->carrier;
}
public function setSmsnotify(?bool $smsnotify): self
{
$this->smsnotify = $smsnotify;
return $this;
}
public function getSmsnotify(): ?bool
{
return $this->smsnotify;
}
public function setSmsreplies(?bool $smsreplies): self
{
$this->smsreplies = $smsreplies;
return $this;
}
public function getSmsreplies(): ?bool
{
return $this->smsreplies;
}
public function setSmsemail(?string $smsemail): self
{
$this->smsemail = $smsemail;
return $this;
}
public function getSmsemail(): ?string
{
return $this->smsemail;
}
public function setUri(?string $uri): self
{
$this->uri = $uri;
return $this;
}
public function getUri(): ?string
{
return $this->uri;
}
public function setAutosubscribe(?bool $autosubscribe): self
{
$this->autosubscribe = $autosubscribe;
return $this;
}
public function getAutosubscribe(): ?bool
{
return $this->autosubscribe;
}
public function setSubscribePolicy(?int $subscribe_policy): self
{
$this->subscribe_policy = $subscribe_policy;
return $this;
}
public function getSubscribePolicy(): ?int
{
return $this->subscribe_policy;
}
public function setUrlshorteningservice(?string $urlshorteningservice): self
{
$this->urlshorteningservice = $urlshorteningservice;
return $this;
}
public function getUrlshorteningservice(): ?string
{
return $this->urlshorteningservice;
}
public function setPrivateStream(?bool $private_stream): self
{
$this->private_stream = $private_stream;
return $this;
}
public function getPrivateStream(): ?bool
{
return $this->private_stream;
}
public function setCreated(DateTime $created): self
{
$this->created = $created;
return $this;
}
public function getCreated(): DateTime
{
return $this->created;
}
public function setModified(DateTime $modified): self
{
$this->modified = $modified;
return $this;
}
public function getModified(): DateTime
{
return $this->modified;
}
// AUTOCODE END
public static function schemaDef()
{