Skip to content

Commit

Permalink
Allow event listener to mess up with executors and steps
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Nov 14, 2016
1 parent b85343a commit fa3295a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
18 changes: 18 additions & 0 deletions API/Event/BeforeStepExecutionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,22 @@ public function getExecutor()
{
return $this->executor;
}

/**
* Here be dragons
* @param ExecutorInterface $executor
*/
public function replaceExecutor(ExecutorInterface $executor)
{
$this->executor = $executor;
}

/**
* Lasciate ogni speranza, voi ch'entrate
* @param MigrationStep $step
*/
public function replaceStep(MigrationStep $step)
{
$this->step = $step;
}
}
4 changes: 2 additions & 2 deletions Core/Executor/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,11 @@ protected function setFields($createOrUpdateStruct, array $fields, ContentType $
$fieldValue = $field;
}

$fieldType = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier];
if ($fieldType == null) {
if (!isset($contentType->fieldDefinitionsByIdentifier[$fieldIdentifier])) {
throw new \Exception("Field '$fieldIdentifier' is not present in field type '{$contentType->identifier}'");
}

$fieldType = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier];
$fieldValue = $this->getFieldValue($fieldValue, $fieldType, $contentType->identifier, $this->context);

$createOrUpdateStruct->setField($fieldIdentifier, $fieldValue, $this->getLanguageCode());
Expand Down
6 changes: 5 additions & 1 deletion Core/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ public function executeMigration(MigrationDefinition $migrationDefinition, $useT
// we validated the fact that we have a good executor at parsing time
$executor = $this->executors[$step->type];

$this->dispatcher->dispatch('ez_migration.before_execution', new BeforeStepExecutionEvent($step, $executor));
$beforeStepExecutionEvent = new BeforeStepExecutionEvent($step, $executor);
$this->dispatcher->dispatch('ez_migration.before_execution', $beforeStepExecutionEvent);
// allow some sneaky trickery here: event listeners can manipulate 'live' the step definition and the executor
$executor = $beforeStepExecutionEvent->getExecutor();
$step = $beforeStepExecutionEvent->getStep();

$result = $executor->execute($step);

Expand Down
6 changes: 6 additions & 0 deletions WHATSNEW.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 3.0.0-beta4
===================

* New: It is now possible to replace the executor and step definition in a BeforeStepExecutionEvent


Version 3.0.0-beta3
===================

Expand Down

0 comments on commit fa3295a

Please sign in to comment.