Skip to content

Commit

Permalink
LatteExtension: initializes Latte Panel
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 16, 2021
1 parent 7f29e32 commit 68809b8
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/Bridges/ApplicationDI/LatteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Latte;
use Nette;
use Nette\Bridges\ApplicationLatte;
use Tracy;


/**
Expand All @@ -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;

Expand All @@ -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])
Expand All @@ -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) {
Expand All @@ -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();
Expand Down

0 comments on commit 68809b8

Please sign in to comment.