Skip to content

Commit

Permalink
rework to extension
Browse files Browse the repository at this point in the history
  • Loading branch information
accgit committed Feb 4, 2020
1 parent 536cb2e commit 68bfdc8
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 78 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
],
"require": {
"php": ">=7.1",
"nette/di": "^3.0",
"nette/utils": "^3.0"
},
"require-dev": {
"nette/tester": "^2.3",
"nette/bootstrap": "^3.0",
"tracy/tracy": "^2.7"
},
"replace": {
Expand Down
37 changes: 37 additions & 0 deletions src/Drago/Localization/DI/TranslatorExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);

/**
* Drago Extension
* Package built on Nette Framework
*/

namespace Drago\Localization\DI;

use Drago\Localization\Translator;
use Nette\DI\CompilerExtension;


/**
* Register services.
*/
class TranslatorExtension extends CompilerExtension
{
/** @var string */
private $translateDir;


public function __construct(string $translateDir)
{
$this->translateDir = $translateDir;
}


public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$builder->addDefinition($this->prefix('translator'))
->setFactory(Translator::class, [$this->translateDir]);
}
}
18 changes: 11 additions & 7 deletions src/Drago/Localization/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ class Translator implements ITranslator
/** @var array */
private $message;

/** @var string */
private $translateDir;


public function __construct(string $translateDir)
{
$this->translateDir = $translateDir;
}


/**
* Path to the translation file.
* @throws \Exception
*/
public function setFile(string $file): array
public function setTranslate(string $translate): array
{
$file = $file . '.ini';
if (!is_file($file)) {
throw new \Exception('The translation file was not found.');
}

$file = $this->translateDir . '/' . $translate . '.ini';
$this->message = parse_ini_file($file);
return $this->message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,30 @@
/**
* Simple translator.
*/
trait Locale
trait TranslatorAdapter
{
/**
* @var string
* @persistent
*/
public $lang;

/**
* @var Translator
* @inject
*/
/** @var Translator */
public $translator;


public function injectTranslator(Translator $translator): void
{
$this->translator = $translator;
}


/**
* Create a translation.
* @throws \Exception
*/
public function createTranslator(string $file): Translator
public function getTranslator(): Translator
{
$translator = $this->translator;
$translator->setFile($file);
return $translator;
$this->translator->setTranslate($this->lang);
return $this->translator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
/**
* Simple translator.
*/
trait TranslateControl
trait TranslatorControl
{
/** @var Translator */
private $translator;


public function setTranslator(Translator $translator)
public function setTranslator(Translator $translator): void
{
$this->translator = $translator;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/TestContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types = 1);

namespace Test;

use Nette\DI\Container;
use Tester\TestCase;


abstract class TestContainer extends TestCase
{
/** @var Container */
protected $container;


public function __construct(Container $container)
{
$this->container = $container;
}
}
10 changes: 10 additions & 0 deletions tests/Translator/Control.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types = 1);

use Drago\Localization;

class Control
{
use Localization\TranslatorControl;
}
77 changes: 77 additions & 0 deletions tests/Translator/DI/TranslatorExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types = 1);

namespace Test\DI;

use Drago\Localization;
use Nette\DI;
use Test\TestContainer;
use Tester\Assert;

$container = require __DIR__ . '/../../bootstrap.php';


class TranslatorExtension extends TestContainer
{
private function createContainer(): DI\Container
{
$params = $this->container->getParameters();
$loader = new DI\ContainerLoader($params['tempDir'], true);

$class = $loader->load(function (DI\Compiler $compiler) use ($params): void {
$compiler->addExtension('translator', new Localization\DI\TranslatorExtension(
$params['appDir'] . '/locale'
));
});
return new $class;
}


private function getTranslatorByType(): Localization\Translator
{
$translator = $this->createContainer();
return $translator->getByType(Localization\Translator::class);
}


public function test01(): void
{
Assert::type(Localization\Translator::class, $this->getTranslatorByType());
}


public function test02(): void
{
$class = $this->getTranslatorByType();

Assert::type('array', $class->setTranslate('en'));
Assert::type('string', $class->translate('hello.world'));
Assert::same('Hello, world!', $class->translate('hello.world'));
}


public function test03(): void
{
$class = $this->getTranslatorByType();
$presenter = new \Presenter;
$presenter->lang = 'en';
$presenter->translator = $class;

Assert::type($presenter->getTranslator(), $class);

}


public function test04(): void
{
$class = $this->getTranslatorByType();
$control = new \Control;
$control->setTranslator($class);

Assert::type($control->getTranslator(), $class);
}
}

$extension = new TranslatorExtension($container);
$extension->run();
20 changes: 0 additions & 20 deletions tests/Translator/Locale.phpt

This file was deleted.

10 changes: 10 additions & 0 deletions tests/Translator/Presenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types = 1);

use Drago\Localization;

class Presenter
{
use Localization\TranslatorAdapter;
}
22 changes: 0 additions & 22 deletions tests/Translator/TranslateControl.phpt

This file was deleted.

17 changes: 0 additions & 17 deletions tests/Translator/Translator.phpt

This file was deleted.

10 changes: 10 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@

Tester\Environment::setup();
date_default_timezone_set('Europe/Prague');
define('TEMP_DIR', __DIR__ . '/tmp');

$boot = new Nette\Configurator;
$boot->setTempDirectory(TEMP_DIR);
$boot->createRobotLoader()
->addDirectory(__DIR__)
->addDirectory(__DIR__ . '/../src')
->register();

return $boot->createContainer();
File renamed without changes.

0 comments on commit 68bfdc8

Please sign in to comment.