Skip to content

Commit

Permalink
LatteExtension: automatically passes translator to Latte\Essential\Tr…
Browse files Browse the repository at this point in the history
…anslatorExtension
  • Loading branch information
dg committed Jun 11, 2024
1 parent f503641 commit 2b89ca3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Bridges/ApplicationDI/LatteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public function loadConfiguration()
->addSetup('enablePhpLinter', [$config->phpLinter]);

foreach ($config->extensions as $extension) {
if ($extension === Latte\Essential\TranslatorExtension::class) {
$extension = new Statement($extension, [new Nette\DI\Definitions\Reference(Nette\Localization\Translator::class)]);
}
$this->addExtension($extension);
}
}
Expand Down
46 changes: 46 additions & 0 deletions tests/Bridges.DI/LatteExtension.translator.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* Test: LatteExtension passes Translator to TranslatorExtension
*/

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');
}


class Translator implements Nette\Localization\Translator
{
public function translate(Stringable|string $message, ...$parameters): string|Stringable
{
return '';
}
}

$loader = new DI\Config\Loader;
$config = $loader->load(Tester\FileMock::create('
latte:
extensions:
- Latte\Essential\TranslatorExtension
services:
- Translator
', 'neon'));

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

$container = new Container;

$latte = $container->getService('nette.latteFactory')->create();
$extensions = Assert::with($latte, fn() => $this->extensions);
Assert::equal(new Latte\Essential\TranslatorExtension(new Translator), $extensions[3]);

0 comments on commit 2b89ca3

Please sign in to comment.