diff --git a/src/Bridges/ApplicationDI/LatteExtension.php b/src/Bridges/ApplicationDI/LatteExtension.php index cc3b1034a..b0b026156 100644 --- a/src/Bridges/ApplicationDI/LatteExtension.php +++ b/src/Bridges/ApplicationDI/LatteExtension.php @@ -11,6 +11,8 @@ use Latte; use Nette; +use Nette\Bridges\ApplicationLatte; +use Tracy; /** @@ -31,6 +33,9 @@ public function __construct(string $tempDir, bool $debugMode = false) $this->debugMode = $debugMode; $this->config = new class { + /** @var ?bool */ + public $debugger = null; + /** @var bool */ public $xhtml = false; @@ -56,7 +61,7 @@ public function loadConfiguration() $builder = $this->getContainerBuilder(); $latteFactory = $builder->addFactoryDefinition($this->prefix('latteFactory')) - ->setImplement(Nette\Bridges\ApplicationLatte\LatteFactory::class) + ->setImplement(ApplicationLatte\LatteFactory::class) ->getResultDefinition() ->setFactory(Latte\Engine::class) ->addSetup('setTempDirectory', [$this->tempDir]) @@ -70,7 +75,7 @@ public function loadConfiguration() $builder->addDefinition($this->prefix('templateFactory')) ->setType(Nette\Application\UI\TemplateFactory::class) - ->setFactory(Nette\Bridges\ApplicationLatte\TemplateFactory::class) + ->setFactory(ApplicationLatte\TemplateFactory::class) ->setArguments(['templateClass' => $config->templateClass]); foreach ($config->macros as $macro) { @@ -84,6 +89,31 @@ 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]); + } + } + + + public static function initLattePanel(ApplicationLatte\TemplateFactory $factory, Tracy\Bar $bar) + { + $factory->onCreate[] = function (ApplicationLatte\Template $template) use ($bar) { + if ($template->control instanceof Nette\Application\UI\Presenter) { + $bar->addPanel(new Latte\Bridges\Tracy\LattePanel($template->getLatte())); + } + }; + } + + public function addMacro(string $macro): void { $builder = $this->getContainerBuilder();