Skip to content

Commit

Permalink
fixup! Added loaders and dumpers for nette database and Doctrine DBAL.
Browse files Browse the repository at this point in the history
  • Loading branch information
fprochazka committed Oct 17, 2015
1 parent 8094b40 commit 628179d
Show file tree
Hide file tree
Showing 25 changed files with 1,188 additions and 597 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@
"nette/utils": "~2.3@dev",
"latte/latte": "~2.3@dev",
"tracy/tracy": "~2.3@dev",

"kdyby/doctrine": "~2.3",
"kdyby/events": "~2.3.1",
"symfony/console": "~2.3",

"nette/tester": "~1.4",
"mockery/mockery": "~0.9",
"kdyby/doctrine": "~2.3",
"kdyby/events": "~2.3.1"
"mockery/mockery": "~0.9"
},
"autoload": {
"psr-0": {
Expand Down
2 changes: 1 addition & 1 deletion docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ You can also use your own Loader, then you put to your config.neon full name wit
```yml
translation:
database:
loader: \Namespace\CustomLoader
loader: Namespace\CustomLoader
```

The loader needs to implement the [Symfony\Component\Translation\Loader\LoaderInterface](https://github.com/symfony/Translation/blob/3ba54181c8386b180999d942bf33681a726bb70f/Loader/LoaderInterface.php)
Expand Down
91 changes: 31 additions & 60 deletions src/Kdyby/Translation/Console/CreateTableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,103 +10,74 @@

namespace Kdyby\Translation\Console;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Types\Type;
use Kdyby;
use Kdyby\Console\ContainerHelper;
use Nette;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;



/**
* @author Filip Procházka <[email protected]>
* @author Azathoth <[email protected]>
*
* @method ContainerHelper|Helper getHelper(string $name)
*/
class CreateTableCommand extends Command
{

/** @var Nette\DI\Container */
private $serviceLocator;

/** @var Connection */
private $connection;

/** @var AbstractSchemaManager */
private $schemaManager;

/** @var string */
public $table;

/** @var string */
public $key;

/** @var string */
public $locale;
/**
* @var Kdyby\Translation\Loader\IDatabaseLoader
*/
private $databaseLoader;

/** @var string */
public $message;

/** @var string */
public $updatedAt;

protected function configure()
{
$this->setName('kdyby:translation-create-table')
$this->setName('kdyby:translation:create-table')
->setDescription('Builds query for creating of database table.')
->setDefinition(array(
new InputOption(
'dump-sql', NULL, InputOption::VALUE_NONE,
'Dumps the generated SQL statement to the screen (does not execute it).'
),
new InputOption(
'force', NULL, InputOption::VALUE_NONE,
'Causes the generated SQL statement to be physically executed against your database.'
)
));
->addOption('dump-sql', NULL, InputOption::VALUE_NONE, 'Dumps the generated SQL statement to the screen (does not execute it).')
->addOption('force', NULL, InputOption::VALUE_NONE, 'Causes the generated SQL statement to be physically executed against your database.');
}



protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->serviceLocator = $this->getHelper('container')->getContainer();
$this->connection = $this->serviceLocator->getByType('Doctrine\DBAL\Connection');
$this->schemaManager = $this->serviceLocator->getByType('Doctrine\DBAL\Schema\AbstractSchemaManager');
$serviceLocator = $this->getHelper('container')->getContainer();
$this->databaseLoader = $serviceLocator->getByType('Kdyby\Translation\Loader\IDatabaseLoader');
}



protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$input->getOption('dump-sql') && !$input->getOption('force')) {
$output->writeln('<error>You must run the command either with --dump-sql or --force.</error>');
return;
return 1;
}

if ($this->schemaManager->tablesExist($this->table)) {
$output->writeln('Table already exists.');
return;
}
try {
$queries = $this->databaseLoader->setupDatabase($input->getOption('force'));

$table = $this->schemaManager->createSchema()
->createTable($this->table);
$table->addColumn($this->key, Type::STRING);
$table->addColumn($this->locale, Type::STRING);
$table->addColumn($this->message, Type::TEXT);
$table->addColumn($this->updatedAt, Type::DATETIME);
$table->setPrimaryKey(array($this->key, $this->locale));
$table->addIndex(array($this->updatedAt));

if ($input->getOption('dump-sql')) {
list($sql) = $this->connection->getDatabasePlatform()->getCreateTableSQL($table);
$output->writeln('Create table SQL:');
$output->writeln($sql);
}
if ($input->getOption('force')) {
$output->writeln(sprintf('Database schema updated successfully! Translation table created.'));
}

if ($input->getOption('dump-sql')) {
$output->writeln(implode(";\n", $queries));
}

return 0;

if ($input->getOption('force')) {
$this->schemaManager->createTable($table);
$output->writeln(sprintf('Database schema updated successfully! Translation table created.'));
} catch (Kdyby\Translation\DatabaseException $e) {
$this->getApplication()->renderException($e, $output);
return 1;
}
}

Expand Down
125 changes: 125 additions & 0 deletions src/Kdyby/Translation/DI/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka ([email protected])
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Translation\DI;

use Kdyby;
use Nette;



/**
* @author Filip Procházka <[email protected]>
*/
class Configuration
{

/**
* @var string
*/
private $table = 'translations';

/**
* @var string
*/
private $key = 'key';

/**
* @var string
*/
private $locale = 'locale';

/**
* @var string
*/
private $message = 'message';

/**
* @var string
*/
private $updatedAt = 'updated_at';



/**
* @param string $table
*/
public function setTableName($table)
{
$this->table = $table;
}



/**
* @return string
*/
public function getTableName()
{
return $this->table;
}



/**
* @param string $key
* @param string $locale
* @param string $message
* @param string $updatedAt
*/
public function setColumnNames($key, $locale, $message, $updatedAt)
{
$this->key = $key;
$this->locale = $locale;
$this->message = $message;
$this->updatedAt = $updatedAt;
}



/**
* @return string
*/
public function getKeyColumn()
{
return $this->key;
}



/**
* @return string
*/
public function getLocaleColumn()
{
return $this->locale;
}



/**
* @return string
*/
public function getMessageColumn()
{
return $this->message;
}



/**
* @return string
*/
public function getUpdatedAtColumn()
{
return $this->updatedAt;
}

}
Loading

0 comments on commit 628179d

Please sign in to comment.