Skip to content

Commit

Permalink
refactor: better Twig profile (#2126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny authored Feb 25, 2025
1 parent add370c commit 0ce3ee0
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/Step/Pages/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,17 @@ public function process(): void
],
];
$page->addRendered($rendered);
// profiler
if ($this->builder->isDebug()) {
$dumper = new \Twig\Profiler\Dumper\HtmlDumper();
file_put_contents(
Util::joinFile($this->config->getOutputPath(), '_debug_twig_profile.html'),
$dumper->dump($this->builder->getRenderer()->getDebugProfile())
);
}
} catch (\Twig\Error\Error $e) {
$template = !empty($e->getSourceContext()->getPath()) ? $e->getSourceContext()->getPath() : $e->getSourceContext()->getName();

throw new RuntimeException(\sprintf(
'Template "%s%s" (page: %s): %s',
$template,
$e->getTemplateLine() >= 0 ? \sprintf(':%s', $e->getTemplateLine()) : '',
$page->getId(),
$e->getMessage()
));
} catch (\Exception $e) {
throw new RuntimeException($e->getMessage());
}
}
$this->builder->getPages()->replace($page->getId(), $page);
Expand All @@ -190,6 +183,23 @@ public function process(): void
);
$this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
}
// profiler
if ($this->builder->isDebug()) {
try {
// HTML
$htmlDumper = new \Twig\Profiler\Dumper\HtmlDumper();
$profileHtmlFile = Util::joinFile($this->config->getDestinationDir(), '.debug/twig_profile.html');
Util\File::getFS()->dumpFile($profileHtmlFile, $htmlDumper->dump($this->builder->getRenderer()->getDebugProfile()));
// TXT
$textDumper = new \Twig\Profiler\Dumper\TextDumper();
$profileTextFile = Util::joinFile($this->config->getDestinationDir(), '.debug/twig_profile.txt');
Util\File::getFS()->dumpFile($profileTextFile, $textDumper->dump($this->builder->getRenderer()->getDebugProfile()));
// log
$this->builder->getLogger()->debug(\sprintf('Twig profile dumped in "%s"', Util::joinFile($this->config->getDestinationDir(), '.debug/')));
} catch (\Symfony\Component\Filesystem\Exception\IOException $e) {
throw new RuntimeException($e->getMessage());
}
}
}

/**
Expand Down

0 comments on commit 0ce3ee0

Please sign in to comment.