Something went wrong. Try again.
Parse and validate AT Protocol Lexicons with DTO generation for Laravel
Something went wrong. Try again.
5.0 kB · 226 lines
PHP
at dev
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227<?php
namespace SocialDept\AtpSchema\Generator;
use SocialDept\AtpSchema\Exceptions\GenerationException;
class TemplateRenderer{ /** * Templates directory. */ protected ?string $templatesDirectory = null;
/** * In-memory templates. * * @var array<string, string> */ protected array $templates = [];
/** * Create a new TemplateRenderer. */ public function __construct(?string $templatesDirectory = null) { $this->templatesDirectory = $templatesDirectory; $this->registerDefaultTemplates(); }
/** * Render a template with given data. */ public function render(string $templateName, array $data): string { $template = $this->getTemplate($templateName);
return $this->renderTemplate($template, $data); }
/** * Get template content. */ protected function getTemplate(string $templateName): string { // Check in-memory templates first if (isset($this->templates[$templateName])) { return $this->templates[$templateName]; }
// Check templates directory if ($this->templatesDirectory !== null) { $path = $this->templatesDirectory.'/'.$templateName.'.php.template';
if (file_exists($path)) { return file_get_contents($path); } }
throw GenerationException::templateNotFound($templateName); }
/** * Render template with data. */ protected function renderTemplate(string $template, array $data): string { // Simple variable replacement foreach ($data as $key => $value) { if (is_scalar($value) || $value === null) { $template = str_replace("{{{$key}}}", (string) $value, $template); } }
// Handle property lists if (isset($data['properties']) && is_array($data['properties'])) { $template = $this->renderProperties($template, $data['properties']); }
return $template; }
/** * Render properties section. */ protected function renderProperties(string $template, array $properties): string { $propertiesCode = [];
foreach ($properties as $prop) { $typeHint = $prop['phpType']; $nullable = ! ($prop['required'] ?? true);
if ($nullable && $typeHint !== 'mixed') { $typeHint = '?'.$typeHint; }
$docComment = ''; if ($prop['description'] ?? null) { $docComment = " /**\n * {$prop['description']}\n */\n"; }
$propertiesCode[] = sprintf( '%s public readonly %s $%s;', $docComment, $typeHint, $prop['name'] ); }
$propertiesString = implode("\n\n", $propertiesCode);
return str_replace('{{properties}}', $propertiesString, $template); }
/** * Register default templates. */ protected function registerDefaultTemplates(): void { $this->templates['record'] = <<<'PHP'<?php
namespace {{namespace}};
/** * {{description}} * * NSID: {{nsid}} */class {{className}}{{{properties}}
/** * Create a new {{className}}. */ public function __construct( // Constructor parameters will be generated ) { // Property assignments will be generated }
/** * Create from array data. */ public static function fromArray(array $data): self { return new self( // Assignments will be generated ); }
/** * Convert to array. */ public function toArray(): array { return [ // Array conversion will be generated ]; }}
PHP;
$this->templates['object'] = <<<'PHP'<?php
namespace {{namespace}};
/** * {{description}} */class {{className}}{{{properties}}
/** * Create a new {{className}}. */ public function __construct( // Constructor parameters will be generated ) { // Property assignments will be generated }
/** * Create from array data. */ public static function fromArray(array $data): self { return new self( // Assignments will be generated ); }
/** * Convert to array. */ public function toArray(): array { return [ // Array conversion will be generated ]; }}
PHP; }
/** * Register a custom template. */ public function registerTemplate(string $name, string $template): void { $this->templates[$name] = $template; }
/** * Set templates directory. */ public function setTemplatesDirectory(string $directory): void { $this->templatesDirectory = $directory; }}