Skip to content
Permalink
Browse files

remove dependency on nette/application

  • Loading branch information
Aleš
Aleš committed Sep 23, 2019
1 parent c926c07 commit a54fb66383e189ef86beaeb8d8db48f5c626ae78
@@ -13,12 +13,12 @@
"require": {
"php": "^7.2.9",
"latte/latte": "^2.5",
"nette/application": "^3.0",
"nette/di": "^3.0",
"nette/finder": "^2.5",
"nette/http": "^3.0",
"nette/neon": "^3.0",
"nette/schema": "^1.0",
"nette/routing": "^3.0",
"nette/utils": "^3.0",
"symfony/translation": "^4.2",
"symfony/config": "^4.2"
@@ -168,25 +168,17 @@ public function beforeCompile(): void
$tracyPanel = $builder->getDefinition($this->prefix('tracyPanel'));
}
$templateFactoryName = $builder->getByType(Nette\Application\UI\ITemplateFactory::class);
if ($templateFactoryName !== null) {
/** @var Nette\DI\Definitions\ServiceDefinition $templateFactory */
$templateFactory = $builder->getDefinition($templateFactoryName);
$templateFactory->addSetup('
$service->onCreate[] = function (Nette\\Bridges\\ApplicationLatte\\Template $template): void {
$template->setTranslator(?);
};', [$translator]);
}
if ($builder->hasDefinition('latte.latteFactory')) {
$latteFilters = $builder->addDefinition($this->prefix('latte.filters'))
->setFactory(Contributte\Translation\Latte\Filters::class);
/** @var Nette\DI\Definitions\FactoryDefinition $latteFactory */
$latteFactory = $builder->getDefinition('latte.latteFactory');
$latteFactory->getResultDefinition()
->addSetup('?->onCompile[] = function (Latte\\Engine $engine): void { ?::install($engine->getCompiler()); }', ['@self', new Nette\PhpGenerator\PhpLiteral(Contributte\Translation\Latte\Macros::class)])
->addSetup('addProvider', ['translator', $builder->getDefinition($this->prefix('translator'))]);
->addSetup('addProvider', ['translator', $builder->getDefinition($this->prefix('translator'))])
->addSetup('addFilter', ['translate', [$latteFilters, 'translate']]);
}
/** @var Contributte\Translation\DI\TranslationProviderInterface $v1 */
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);
namespace Contributte\Translation\Latte;
use Latte;
use Nette;
class Filters
{
/** @var Nette\Localization\ITranslator */
private $translator;
public function __construct(Nette\Localization\ITranslator $translator)
{
$this->translator = $translator;
}
public function translate(Latte\Runtime\FilterInfo $filterInfo, $message, ...$args): string
{
return $this->translator->translate($message, ...$args);
}
}
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);
namespace Tests;
use Contributte;
use Latte;
use Mockery;
use Tester;
use Tests;
$container = require __DIR__ . '/../../bootstrap.php';
class Filters extends Tests\TestAbstract
{
public function test01(): void
{
$translatorMock = Mockery::mock(Contributte\Translation\Translator::class);
$translatorMock->shouldReceive('translate')
->once()
->withArgs(['message', 'args'])
->andReturn('');
$filters = new Contributte\Translation\Latte\Filters($translatorMock);
Tester\Assert::same('', $filters->translate(new Latte\Runtime\FilterInfo(), 'message', 'args'));
}
}
(new Filters($container))->run();
@@ -27,7 +27,7 @@ class Router extends Tests\TestAbstract
private function resolve(?string $locale): ?string
{
$request = new Nette\Http\Request(new Nette\Http\UrlScript());
$routeListMock = Mockery::mock(Nette\Application\Routers\RouteList::class);
$routeListMock = Mockery::mock(Nette\Routing\RouteList::class);
$routeListMock->shouldReceive('match')
->withArgs([$request])
@@ -150,60 +150,62 @@ class Translator extends Tests\TestAbstract
{
$container = $this->createContainer();
/** @var Nette\Application\UI\ITemplateFactory $translator */
$template = $container->getByType(Nette\Application\UI\ITemplateFactory::class);
/** @var Nette\Localization\ITranslator $translator */
$translator = $container->getByType(Nette\Localization\ITranslator::class);
$template->onCreate[] = function (Nette\Bridges\ApplicationLatte\Template $template): void {
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_messages.hello}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_}messages.hello{/_}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_}{php $message = "messages.hello"}{$message}{/_}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{php $message = "messages.hello"}{$message|translate}')));
$latte = (new Latte\Engine())
->addProvider('translator', $translator)
->addFilter('translate', [new Contributte\Translation\Latte\Filters($translator), 'translate']);
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_hello}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_}hello{/_}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_}{php $message = "hello"}{$message}{/_}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{php $message = "hello"}{$message|translate}')));
Contributte\Translation\Latte\Macros::install($latte->getCompiler());
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_//messages.hello}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_}//messages.hello{/_}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_}{php $message = "//messages.hello"}{$message}{/_}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{php $message = "//messages.hello"}{$message|translate}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_messages.hello}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_}messages.hello{/_}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_}{php $message = "messages.hello"}{$message}{/_}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{php $message = "messages.hello"}{$message|translate}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_hello, [], messages, en}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{php $message = "hello"}{$message|translate: [], messages, en}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_hello}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_}hello{/_}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_}{php $message = "hello"}{$message}{/_}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{php $message = "hello"}{$message|translate}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{_hello, null, [], messages, en}')));
Tester\Assert::same('Hello', $template->renderToString(Tester\FileMock::create('{php $message = "hello"}{$message|translate: null, [], messages, en}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_//messages.hello}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_}//messages.hello{/_}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_}{php $message = "//messages.hello"}{$message}{/_}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{php $message = "//messages.hello"}{$message|translate}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{_messages.hi, [name => Ales]}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{php $message = "messages.hi"}{$message|translate: [name => Ales]}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_hello, [], messages, en}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{php $message = "hello"}{$message|translate: [], messages, en}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{_hi, [name => Ales]}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{php $message = "hi"}{$message|translate: [name => Ales]}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{_hello, null, [], messages, en}')));
Tester\Assert::same('Hello', $latte->renderToString(Tester\FileMock::create('{php $message = "hello"}{$message|translate: null, [], messages, en}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{_//messages.hi, [name => Ales]}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{php $message = "//messages.hi"}{$message|translate: [name => Ales]}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{_messages.hi, [name => Ales]}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{php $message = "messages.hi"}{$message|translate: [name => Ales]}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{_hi, [name => Ales], messages, en}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{php $message = "hi"}{$message|translate: [name => Ales], messages, en}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{_hi, [name => Ales]}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{php $message = "hi"}{$message|translate: [name => Ales]}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{_hi, null, [name => Ales], messages, en}')));
Tester\Assert::same('Hi Ales!', $template->renderToString(Tester\FileMock::create('{php $message = "hi"}{$message|translate: null, [name => Ales], messages, en}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{_//messages.hi, [name => Ales]}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{php $message = "//messages.hi"}{$message|translate: [name => Ales]}')));
Tester\Assert::same('Depth message', $template->renderToString(Tester\FileMock::create('{_messages.depth.message}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{_hi, [name => Ales], messages, en}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{php $message = "hi"}{$message|translate: [name => Ales], messages, en}')));
Tester\Assert::same('missing.translation', $template->renderToString(Tester\FileMock::create('{_messages.missing.translation}')));
Tester\Assert::same('missing.translation', $template->renderToString(Tester\FileMock::create('{php $message = "messages.missing.translation"}{$message|translate}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{_hi, null, [name => Ales], messages, en}')));
Tester\Assert::same('Hi Ales!', $latte->renderToString(Tester\FileMock::create('{php $message = "hi"}{$message|translate: null, [name => Ales], messages, en}')));
Tester\Assert::same('Depth message', $template->renderToString(Tester\FileMock::create('{translator messages}{_depth.message}{/translator}')));
Tester\Assert::same('Very very depth message', $template->renderToString(Tester\FileMock::create('{translator messages}{translator messages.very.very.depth}{_message}{/translator}{/translator}')));
Tester\Assert::same('Depth message-Very very depth message-Depth message', $template->renderToString(Tester\FileMock::create('{translator messages}{_depth.message}{translator messages.very.very.depth}-{_message}-{/translator}{_depth.message}{/translator}')));
Tester\Assert::exception(function () use ($template): void {
$template->renderToString(Tester\FileMock::create('{translator}{_depth.message}{/translator}'));
}, Latte\CompileException::class);
};
Tester\Assert::same('Depth message', $latte->renderToString(Tester\FileMock::create('{_messages.depth.message}')));
Tester\Assert::same('missing.translation', $latte->renderToString(Tester\FileMock::create('{_messages.missing.translation}')));
Tester\Assert::same('missing.translation', $latte->renderToString(Tester\FileMock::create('{php $message = "messages.missing.translation"}{$message|translate}')));
$template->createTemplate();
Tester\Assert::same('Depth message', $latte->renderToString(Tester\FileMock::create('{translator messages}{_depth.message}{/translator}')));
Tester\Assert::same('Very very depth message', $latte->renderToString(Tester\FileMock::create('{translator messages}{translator messages.very.very.depth}{_message}{/translator}{/translator}')));
Tester\Assert::same('Depth message-Very very depth message-Depth message', $latte->renderToString(Tester\FileMock::create('{translator messages}{_depth.message}{translator messages.very.very.depth}-{_message}-{/translator}{_depth.message}{/translator}')));
Tester\Assert::exception(function () use ($latte): void {
$latte->renderToString(Tester\FileMock::create('{translator}{_depth.message}{/translator}'));
}, Latte\CompileException::class);
}
public function test04(): void

0 comments on commit a54fb66

Please sign in to comment.
You can’t perform that action at this time.