Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added nextras dbal support #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [Database loaders](#database-loaders)
- [Doctrine](#doctrine)
- [Nette Database](#nette-database)
- [Nextras Dbal](#nextras-dbal)
- [Features](#features)
- [Wrappers](#wrappers)
- [TranslationProviderInterface](#translationproviderinterface)
Expand Down Expand Up @@ -219,7 +220,7 @@ yml: Symfony\Component\Translation\Loader\YamlFileLoader

### Database loaders

Package includes database loaders for **[Doctrine 2](https://www.doctrine-project.org/)** and **[Nette Database 3](https://doc.nette.org/cs/3.0/database)**.
Package includes database loaders for **[Doctrine 2](https://www.doctrine-project.org/)** and **[Nette Database 3](https://doc.nette.org/cs/3.0/database)** and **[Nextras Dbal](https://nextras.org/dbal/docs/main/)**.

#### Doctrine

Expand Down Expand Up @@ -300,6 +301,25 @@ translation:
nettedatabase: Contributte\Translation\Loaders\NetteDatabase
```

#### Nextras Dbal

You must create a file with specific format in scanned dirs such as **messages.en_US.nextrasdbal**. All parameters are optional, but the file has to exist.

```neon
table: "my_table" # if you specify table key, "messages" from file name will be ignored
id: "id" # id column name, default is "id"
locale: "locale" # locale column name, default is "locale"
message: "message" # message column name, default is "message"
```

Add loader to translation configuration:

```neon
translation:
loaders:
nextrasdbal: Contributte\Translation\Loaders\NextrasDbal
```

DB table example:

```sql
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"nette/database": "^3.1.1",
"nette/robot-loader": "^3.4.0|~4.0.0",
"nette/tester": "^2.3.1",
"nextras/dbal": "^4.0",
"ninjify/nunjuck": "^0.3.0",
"ninjify/qa": "^0.13",
"phpstan/phpstan": "^1.8",
Expand Down
60 changes: 60 additions & 0 deletions src/Loaders/NextrasDbal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace Contributte\Translation\Loaders;

use Contributte\Translation\Exceptions\InvalidState;
use Nextras\Dbal\Connection;
use Nextras\Dbal\Drivers\Exception\QueryException;
use Symfony\Component\Translation\Loader\LoaderInterface;

class NextrasDbal extends DatabaseAbstract implements LoaderInterface
{

private Connection $connection;

public function __construct(
Connection $connection
)
{
$this->connection = $connection;
}

/**
* @inheritdoc
* @throws \Contributte\Translation\Exceptions\InvalidState
* @throws QueryException
*/
protected function getMessages(
array $config,
string $resource,
string $locale,
string $domain
): array
{
$messages = [];

$result = $this->connection
->query(
'SELECT %column AS `id`, %column AS `locale`, %column AS `message` FROM %table WHERE %column = %s',
$config['id'],
$config['locale'],
$config['message'],
$config['table'],
$config['locale'],
$locale
)
->fetchAll();

foreach ($result as $row) {
$row = (array) $row;
if (array_key_exists($row['id'], $messages)) {
throw new InvalidState('Id "' . $row['id'] . '" declared twice in "' . $config['table'] . '" table/domain.');
}

$messages[$row['id']] = $row['message'];
}

return $messages;
}

}
84 changes: 84 additions & 0 deletions tests/Tests/Loaders/NextrasDbalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php declare(strict_types = 1);

namespace Tests\Loaders;

use Contributte\Translation\Loaders\NextrasDbal;
use Contributte\Translation\Translator;
use Nette\Localization\ITranslator;
use Nextras\Dbal\Connection;
use Nextras\Dbal\ConnectionException;
use Tester\Assert;
use Tests\Helpers;
use Tests\TestAbstract;

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

final class NextrasDbalTest extends TestAbstract
{

private Translator $translator;

private Connection $connection;

protected function setUp()
{
parent::setUp();

$container = Helpers::createContainerFromConfigurator(
$this->container->getParameters()['tempDir'],
[
'extensions' => [
'nextras.dbal' => 'Nextras\Dbal\Bridges\NetteDI\DbalExtension',
],
'nextras.dbal' => [
'driver' => 'mysqli',
'host' => '127.0.0.1',
'port' => 13306,
'database' => 'test',
'username' => 'root',
'password' => '1234',
'connectionTz' => 'auto-offset',
],
'translation' => [
'loaders' => [
'nextrasdbal' => NextrasDbal::class,
],
],
]
);

$this->translator = $container->getByType(ITranslator::class);
$this->connection = $container->getByType(Connection::class);

try {
$this->connection->connect();
} catch (ConnectionException $e) {
$this->skip('Database not connected');
}
}

public function test01(): void
{
$queries = file_get_contents(__DIR__ . '/../../sql.sql');
$queries = explode(';', (string) $queries);
$queries = array_filter($queries);

foreach ($queries as $query) {
$query = trim($query);
if ($query) {
$this->connection->query($query);
}
}

$this->translator->setLocale('cs_CZ');

Assert::same('Ahoj', $this->translator->translate('db_table.hello'));

$this->translator->setLocale('en_US');

Assert::same('Hello', $this->translator->translate('db_table.hello'));
}

}

(new NextrasDbalTest($container))->run();