Skip to content

Commit

Permalink
DI: LatteExtension [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 15, 2023
1 parent 9cea1b8 commit 2468696
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
14 changes: 12 additions & 2 deletions src/Bridges/ApplicationDI/LatteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ public function loadConfiguration(): void
$this->addExtension($extension);
}

$this->addExtension(ApplicationLatte\UIExtension::class, [$builder::literal('$control')]);

if (class_exists(Nette\Bridges\CacheLatte\CacheExtension::class)/* && existuje_instance*/) { // TODO
$this->addExtension(Nette\Bridges\CacheLatte\CacheExtension::class);
}

if (class_exists(Nette\Bridges\FormsLatte\FormsExtension::class)) {
$this->addExtension(Nette\Bridges\FormsLatte\FormsExtension::class);
}

$builder->addDefinition($this->prefix('templateFactory'))
->setFactory(ApplicationLatte\TemplateFactory::class, [
'templateClass' => $config->templateClass,
Expand Down Expand Up @@ -113,10 +123,10 @@ public static function initLattePanel(
}


public function addExtension(Nette\DI\Definitions\Statement|string $extension): void
public function addExtension(Nette\DI\Definitions\Statement|string $extension, array $args = []): void
{
$extension = is_string($extension)
? new Nette\DI\Definitions\Statement($extension)
? new Nette\DI\Definitions\Statement($extension, $args)
: $extension;

$builder = $this->getContainerBuilder();
Expand Down
3 changes: 2 additions & 1 deletion src/Bridges/ApplicationLatte/LatteFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
namespace Nette\Bridges\ApplicationLatte;

use Latte;
use Nette\Application\UI\Control;


interface LatteFactory
{
function create(): Latte\Engine;
function create(?Control $control = null): Latte\Engine;
}


Expand Down
14 changes: 1 addition & 13 deletions src/Bridges/ApplicationLatte/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Nette\Bridges\ApplicationLatte;

use Latte;
use Nette;
use Nette\Application\UI;

Expand All @@ -28,7 +27,6 @@ public function __construct(
private readonly LatteFactory $latteFactory,
private readonly ?Nette\Http\IRequest $httpRequest = null,
private readonly ?Nette\Security\User $user = null,
private readonly ?Nette\Caching\Storage $cacheStorage = null,
$templateClass = null,
private array $configVars = [],
) {
Expand All @@ -47,20 +45,10 @@ public function createTemplate(?UI\Control $control = null, ?string $class = nul
throw new Nette\InvalidArgumentException("Class $class does not implement " . Template::class . ' or it does not exist.');
}

$latte = $this->latteFactory->create();
$latte = $this->latteFactory->create($control);
$template = new $class($latte);
$presenter = $control?->getPresenterIfExists();

$latte->addExtension(new UIExtension($control));

if ($this->cacheStorage && class_exists(Nette\Bridges\CacheLatte\CacheExtension::class)) {
$latte->addExtension(new Nette\Bridges\CacheLatte\CacheExtension($this->cacheStorage));
}

if (class_exists(Nette\Bridges\FormsLatte\FormsExtension::class)) {
$latte->addExtension(new Nette\Bridges\FormsLatte\FormsExtension);
}

// default parameters
$baseUrl = $this->httpRequest
? rtrim($this->httpRequest->getUrl()->withoutUserInfo()->getBaseUrl(), '/')
Expand Down

0 comments on commit 2468696

Please sign in to comment.