From 33de6d8776d6b5d6c7b89e1ffe40c57ad1e9eec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81ro=CC=82me=20Vieilledent?= Date: Fri, 26 Aug 2016 11:37:56 +0200 Subject: [PATCH] Added support for default language code as console command option --- API/LanguageAwareInterface.php | 32 +++++++++++++++++++++++ Command/MigrateCommand.php | 7 ++++- Core/Executor/RepositoryExecutor.php | 38 ++++++++++++++++++++-------- Core/MigrationService.php | 12 ++++++++- 4 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 API/LanguageAwareInterface.php diff --git a/API/LanguageAwareInterface.php b/API/LanguageAwareInterface.php new file mode 100644 index 00000000..c9b6bec8 --- /dev/null +++ b/API/LanguageAwareInterface.php @@ -0,0 +1,32 @@ +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") @@ -147,7 +148,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln("Processing $name"); 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("\nMigration failed! Reason: " . $e->getMessage() . "\n"); diff --git a/Core/Executor/RepositoryExecutor.php b/Core/Executor/RepositoryExecutor.php index fa205e6a..3e312a19 100644 --- a/Core/Executor/RepositoryExecutor.php +++ b/Core/Executor/RepositoryExecutor.php @@ -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; @@ -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'; @@ -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. * @@ -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)) { @@ -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; } } diff --git a/Core/MigrationService.php b/Core/MigrationService.php index 2b5c7a62..09a751d4 100644 --- a/Core/MigrationService.php +++ b/Core/MigrationService.php @@ -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; @@ -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); @@ -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);