From 65c419ac97ae8b935f69d9b15e5f8583b87b92fe Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 16 Jan 2021 04:19:09 +0100 Subject: [PATCH] LatteExtension: initializes Latte Panel, added option 'debugger' --- src/Bridges/ApplicationDI/LatteExtension.php | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Bridges/ApplicationDI/LatteExtension.php b/src/Bridges/ApplicationDI/LatteExtension.php index 68877aaf6..afbc78391 100644 --- a/src/Bridges/ApplicationDI/LatteExtension.php +++ b/src/Bridges/ApplicationDI/LatteExtension.php @@ -13,6 +13,7 @@ use Nette; use Nette\Bridges\ApplicationLatte; use Nette\Schema\Expect; +use Tracy; /** @@ -37,6 +38,7 @@ public function __construct(string $tempDir, bool $debugMode = false) public function getConfigSchema(): Nette\Schema\Schema { return Expect::structure([ + 'debugger' => Expect::anyOf(true, false, 'all'), 'xhtml' => Expect::bool(false), 'macros' => Expect::arrayOf('string'), 'templateClass' => Expect::string(), @@ -83,6 +85,34 @@ public function loadConfiguration() } + public function beforeCompile() + { + $builder = $this->getContainerBuilder(); + + if ( + $this->debugMode + && ($this->config->debugger ?? $builder->getByType(Tracy\Bar::class)) + && class_exists(Latte\Bridges\Tracy\LattePanel::class) + ) { + $factory = $builder->getDefinition($this->prefix('templateFactory')); + $factory->addSetup([self::class, 'initLattePanel'], [$factory, 'all' => $this->config->debugger === 'all']); + } + } + + + public static function initLattePanel(ApplicationLatte\TemplateFactory $factory, Tracy\Bar $bar, bool $all = false) + { + $factory->onCreate[] = function (ApplicationLatte\Template $template) use ($bar, $all) { + if ($all || $template->control instanceof Nette\Application\UI\Presenter) { + $bar->addPanel(new Latte\Bridges\Tracy\LattePanel( + $template->getLatte(), + $all ? (new \ReflectionObject($template->control))->getShortName() : null + )); + } + }; + } + + public function addMacro(string $macro): void { $builder = $this->getContainerBuilder();