Skip to content

Commit

Permalink
LatteExtension: added option 'variables'
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2023
1 parent f704d22 commit 3c10161
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Bridges/ApplicationDI/LatteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getConfigSchema(): Nette\Schema\Schema
'strictTypes' => Expect::bool(false),
'strictParsing' => Expect::bool(false),
'phpLinter' => Expect::string(),
'variables' => Expect::array([]),
]);
}

Expand Down Expand Up @@ -70,8 +71,10 @@ public function loadConfiguration()
}

$builder->addDefinition($this->prefix('templateFactory'))
->setFactory(ApplicationLatte\TemplateFactory::class)
->setArguments(['templateClass' => $config->templateClass]);
->setFactory(ApplicationLatte\TemplateFactory::class, [
'templateClass' => $config->templateClass,
'configVars' => $config->variables,
]);

if ($this->name === 'latte') {
$builder->addAlias('nette.latteFactory', $this->prefix('latteFactory'));
Expand Down
3 changes: 3 additions & 0 deletions src/Bridges/ApplicationLatte/DefaultTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ final class DefaultTemplate extends Template
/** @var \stdClass[] */
public array $flashes = [];

/** @var \stdClass */
public mixed $config;


/**
* Adds new template parameter.
Expand Down
2 changes: 2 additions & 0 deletions src/Bridges/ApplicationLatte/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct(
private ?Nette\Security\User $user = null,
private ?Nette\Caching\Storage $cacheStorage = null,
$templateClass = null,
private array $configVars = [],
) {
if ($templateClass && (!class_exists($templateClass) || !is_a($templateClass, Template::class, true))) {
throw new Nette\InvalidArgumentException("Class $templateClass does not implement " . Template::class . ' or it does not exist.');
Expand Down Expand Up @@ -85,6 +86,7 @@ public function createTemplate(?UI\Control $control = null, ?string $class = nul
'flashes' => $flashes,
'control' => $control,
'presenter' => $presenter,
'config' => $control instanceof UI\Presenter && $this->configVars ? (object) $this->configVars : null,
];

foreach ($params as $key => $value) {
Expand Down
78 changes: 78 additions & 0 deletions tests/Bridges.DI/LatteExtension.variables.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Test: LatteExtension v3
*/

declare(strict_types=1);

use Nette\DI;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}

Tester\Environment::bypassFinals();


test('no variables', function () {
$loader = new DI\Config\Loader;
$config = $loader->load(Tester\FileMock::create('
latte:
variables:
', 'neon'));

$compiler = new DI\Compiler;
$compiler->addExtension('latte', new Nette\Bridges\ApplicationDI\LatteExtension('', false));
$code = $compiler
->addConfig($config)
->setClassName('Container1')
->compile();
eval($code);

$container = new Container1;
$latteFactory = $container->getService('latte.templateFactory');
$presenter = Mockery::mock(Nette\Application\UI\Presenter::class);
$presenter->shouldReceive('getHttpResponse')->andReturn(Mockery::mock(Nette\Http\IResponse::class)->shouldIgnoreMissing());
$presenter->shouldIgnoreMissing();

$template = $latteFactory->createTemplate($presenter);
Assert::notContains('config', $template->getParameters());
});


test('presenter presence', function () {
$loader = new DI\Config\Loader;
$config = $loader->load(Tester\FileMock::create('
latte:
variables:
foo: bar
', 'neon'));

$compiler = new DI\Compiler;
$compiler->addExtension('latte', new Nette\Bridges\ApplicationDI\LatteExtension('', false));
$code = $compiler
->addConfig($config)
->setClassName('Container2')
->compile();
eval($code);

$container = new Container2;
$latteFactory = $container->getService('latte.templateFactory');
$template = $latteFactory->createTemplate();
Assert::notContains('config', $template->getParameters());


$presenter = Mockery::mock(Nette\Application\UI\Presenter::class);
$presenter->shouldReceive('getHttpResponse')->andReturn(Mockery::mock(Nette\Http\IResponse::class)->shouldIgnoreMissing());
$presenter->shouldIgnoreMissing();

$template = $latteFactory->createTemplate($presenter);
Assert::equal(
(object) ['foo' => 'bar'],
$template->getParameters()['config'],
);
});

0 comments on commit 3c10161

Please sign in to comment.