From 31d66caa5e539fe01bd10eed07d5459c22f08591 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 25 Feb 2025 17:31:58 +0100 Subject: [PATCH] refactor: better Twig profile --- src/Step/Pages/Render.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Step/Pages/Render.php b/src/Step/Pages/Render.php index 79c952e4e..63e00ee44 100644 --- a/src/Step/Pages/Render.php +++ b/src/Step/Pages/Render.php @@ -160,17 +160,8 @@ 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, @@ -178,6 +169,8 @@ public function process(): void $page->getId(), $e->getMessage() )); + } catch (\Exception $e) { + throw new RuntimeException($e->getMessage()); } } $this->builder->getPages()->replace($page->getId(), $page); @@ -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()); + } + } } /**