Skip to content

Commit

Permalink
update template engine
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Aug 18, 2024
1 parent dcecd5e commit 6022b5d
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/Generator/Server/ServerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,15 @@
*/
abstract class ServerAbstract implements GeneratorInterface
{
private string $templateDir;
private Environment $templateEngine;
private Environment $engine;
private SchemaGeneratorInterface $generator;
private Type\GeneratorInterface $typeGenerator;
protected NormalizerInterface $normalizer;
protected Naming $naming;

public function __construct()
{
$this->templateDir = __DIR__ . '/Template/' . $this->getTemplateDir();

$this->templateEngine = new Environment(new FilesystemLoader($this->templateDir), [
'cache' => false,
]);
$this->engine = $this->newTemplateEngine();
$this->generator = $this->newGenerator();

if ($this->generator instanceof TypeAwareInterface) {
Expand All @@ -91,7 +86,7 @@ public function generate(SpecificationInterface $specification): Chunks|string
{
$context = $this->buildContext($specification);
$folder = $this->buildFolderStructure($specification);
$chunks = $this->copyFiles($this->templateDir, $context);
$chunks = $this->copyFiles($this->getTemplateDir(), $context);

$controllerChunks = $chunks->findByPath($this->getControllerPath());
if (!$controllerChunks instanceof Chunks) {
Expand Down Expand Up @@ -266,7 +261,7 @@ private function copyFiles(string $templateDir, Context $context): Chunks
$extension = pathinfo($file, PATHINFO_EXTENSION);
if ($extension === 'twig') {
$file = substr($file, 0, -5);
$result = $this->templateEngine->render(substr($templatePath, strlen($this->templateDir)), $context->getArrayCopy());
$result = $this->engine->render(substr($templatePath, strlen($this->getTemplateDir())), $context->getArrayCopy());
} else {
$result = file_get_contents($templatePath);
}
Expand All @@ -278,11 +273,15 @@ private function copyFiles(string $templateDir, Context $context): Chunks
return $chunks;
}

private function getTemplateDir(): string
private function newTemplateEngine(): Environment
{
return new Environment(new FilesystemLoader([$this->getTemplateDir()]));
}

protected function getTemplateDir(): string
{
$parts = explode('\\', static::class);
$lastKey = array_key_last($parts);
$className = (new \ReflectionClass(static::class))->getShortName();

return $parts[$lastKey];
return __DIR__ . '/Template/' . $className;
}
}

0 comments on commit 6022b5d

Please sign in to comment.