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 Apr 7, 2024
1 parent 9be6cea commit 30d90f4
Show file tree
Hide file tree
Showing 4 changed files with 82 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 @@ -38,6 +38,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 @@ -75,8 +76,10 @@ public function loadConfiguration(): void
}

$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
1 change: 1 addition & 0 deletions src/Bridges/ApplicationLatte/DefaultTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class DefaultTemplate extends Template

/** @var \stdClass[] */
public array $flashes = [];
public \stdClass $config;


/**
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 @@ -28,6 +28,7 @@ public function __construct(
private readonly ?Nette\Http\IRequest $httpRequest = null,
private readonly ?Nette\Security\User $user = 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 @@ -63,6 +64,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
74 changes: 74 additions & 0 deletions tests/Bridges.DI/LatteExtension.variables.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Test: LatteExtension v3
*/

declare(strict_types=1);

use Nette\DI;
use Tester\Assert;

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

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 30d90f4

Please sign in to comment.