Skip to content

Commit

Permalink
Added support for default language code as console command option
Browse files Browse the repository at this point in the history
  • Loading branch information
lolautruche committed Aug 26, 2016
1 parent 65ac388 commit 33de6d8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 13 deletions.
32 changes: 32 additions & 0 deletions API/LanguageAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Kaliop\eZMigrationBundle\API;

interface LanguageAwareInterface
{
/**
* Injects language code, with xxx-YY format.
* e.g. fre-FR
*
* @param string $languageCode
*/
public function setLanguageCode($languageCode);

/**
* @return string
*/
public function getLanguageCode();

/**
* Sets default language code, with xxx-YY format.
* e.g. fre-FR
*
* @param string $languageCode
*/
public function setDefaultLanguageCode($languageCode);

/**
* @return string
*/
public function getDefaultLanguageCode();
}
7 changes: 6 additions & 1 deletion Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function configure()
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
"The directory or file to load the migration definitions from"
)
->addOption('default-language', null, InputOption::VALUE_REQUIRED, "Default language code that will be used if no language is provided in migration steps")
->addOption('ignore-failures', null, InputOption::VALUE_NONE, "Keep executing migrations even if one fails")
->addOption('clear-cache', null, InputOption::VALUE_NONE, "Clear the cache after the command finishes")
->addOption('no-interaction', 'n', InputOption::VALUE_NONE, "Do not ask any interactive question")
Expand Down Expand Up @@ -147,7 +148,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>Processing $name</info>");

try {
$migrationsService->executeMigration($migrationDefinition, !$input->getOption('no-transactions'));
$migrationsService->executeMigration(
$migrationDefinition,
!$input->getOption('no-transactions'),
$input->getOption('default-language')
);
} catch(\Exception $e) {
if ($input->getOption('ignore-failures')) {
$output->writeln("\n<error>Migration failed! Reason: " . $e->getMessage() . "</error>\n");
Expand Down
38 changes: 27 additions & 11 deletions Core/Executor/RepositoryExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kaliop\eZMigrationBundle\Core\Executor;

use Kaliop\eZMigrationBundle\API\LanguageAwareInterface;
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
Expand All @@ -11,12 +12,10 @@
/**
* The core manager class that all migration action managers inherit from.
*/
abstract class RepositoryExecutor extends AbstractExecutor
abstract class RepositoryExecutor extends AbstractExecutor implements LanguageAwareInterface
{
/**
* Constant defining the default language code
*
* @todo inject via config parameter
*/
const DEFAULT_LANGUAGE_CODE = 'eng-GB';

Expand Down Expand Up @@ -52,6 +51,11 @@ abstract class RepositoryExecutor extends AbstractExecutor
*/
private $languageCode;

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

/**
* The bundle object representing the bundle the currently processed migration is in.
*
Expand Down Expand Up @@ -94,7 +98,9 @@ public function execute(MigrationStep $step)

$this->dsl = $step->dsl;
$this->context = $step->context;
$this->languageCode = isset($this->dsl['lang']) ? $this->dsl['lang'] : self::DEFAULT_LANGUAGE_CODE;
if (isset($this->dsl['lang'])) {
$this->setLanguageCode($this->dsl['lang']);
}

if (method_exists($this, $action)) {

Expand Down Expand Up @@ -141,13 +147,23 @@ protected function loginUser($userId)
return $previousUser->id;
}

/**
* Returns selected language code.
*
* @return string
*/
protected function getLanguageCode()
public function setLanguageCode($languageCode)
{
$this->languageCode = $languageCode;
}

public function getLanguageCode()
{
return $this->languageCode ?: $this->getDefaultLanguageCode();
}

public function setDefaultLanguageCode($languageCode)
{
$this->defaultLanguageCode = $languageCode;
}

public function getDefaultLanguageCode()
{
return $this->languageCode;
return $this->defaultLanguageCode ?: self::DEFAULT_LANGUAGE_CODE;
}
}
12 changes: 11 additions & 1 deletion Core/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kaliop\eZMigrationBundle\Core;

use Kaliop\eZMigrationBundle\API\Collection\MigrationDefinitionCollection;
use Kaliop\eZMigrationBundle\API\LanguageAwareInterface;
use Kaliop\eZMigrationBundle\API\StorageHandlerInterface;
use Kaliop\eZMigrationBundle\API\LoaderInterface;
use Kaliop\eZMigrationBundle\API\DefinitionParserInterface;
Expand Down Expand Up @@ -155,7 +156,7 @@ public function parseMigrationDefinition(MigrationDefinition $migrationDefinitio
*
* @todo add support for skipped migrations, partially executed migrations
*/
public function executeMigration(MigrationDefinition $migrationDefinition, $useTransaction=true)
public function executeMigration(MigrationDefinition $migrationDefinition, $useTransaction = true, $defaultLanguageCode = null)
{
if ($migrationDefinition->status == MigrationDefinition::STATUS_TO_PARSE) {
$migrationDefinition = $this->parseMigrationDefinition($migrationDefinition);
Expand All @@ -165,6 +166,15 @@ public function executeMigration(MigrationDefinition $migrationDefinition, $useT
throw new \Exception("Can not execute migration '{$migrationDefinition->name}': {$migrationDefinition->parsingError}");
}

// Inject default language code in executors that support it.
if ($defaultLanguageCode) {
foreach ($this->executors as $executor) {
if ($executor instanceof LanguageAwareInterface) {
$executor->setDefaultLanguageCode($defaultLanguageCode);
}
}
}

// set migration as begun - has to be in own db transaction
$migration = $this->storageHandler->startMigration($migrationDefinition);

Expand Down

0 comments on commit 33de6d8

Please sign in to comment.