Skip to content

Commit

Permalink
Make classes more flexible to support workflowbundle; allow disabling…
Browse files Browse the repository at this point in the history
… tracing listener
  • Loading branch information
gggeek committed Jun 2, 2017
1 parent b3d0ce5 commit 0e6cbd2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 34 deletions.
25 changes: 15 additions & 10 deletions API/Collection/AbstractCollection.php
Expand Up @@ -18,7 +18,7 @@ class AbstractCollection extends \ArrayObject
public function __construct($input = array(), $flags = 0, $iterator_class = "ArrayIterator")
{
foreach ($input as $value) {
if (!is_a($value, $this->allowedClass)) {
if (!$this->isValidElement($value)) {
$this->throwInvalid($value);
}
}
Expand All @@ -31,7 +31,7 @@ public function __construct($input = array(), $flags = 0, $iterator_class = "Arr
*/
public function append($value)
{
if (!is_a($value, $this->allowedClass)) {
if (!$this->isValidElement($value)) {
$this->throwInvalid($value);
}

Expand All @@ -45,7 +45,7 @@ public function append($value)
public function exchangeArray($input)
{
foreach ($input as $value) {
if (!is_a($value, $this->allowedClass)) {
if (!$this->isValidElement($value)) {
$this->throwInvalid($value);
}
}
Expand All @@ -55,20 +55,25 @@ public function exchangeArray($input)

/**
* @param mixed $index
* @param mixed $newval
* @param mixed $value
*/
public function offsetSet($index, $newval)
public function offsetSet($index, $value)
{
if (!is_a($newval, $this->allowedClass)) {
$this->throwInvalid($newval);
if (!$this->isValidElement($value)) {
$this->throwInvalid($value);
}

parent::offsetSet($index, $newval);
parent::offsetSet($index, $value);
}

protected function isValidElement($value)
{
return is_a($value, $this->allowedClass);
}

protected function throwInvalid($newval)
protected function throwInvalid($value)
{
throw new \InvalidArgumentException("Can not add element of type '" . (is_object($newval) ? get_class($newval) : gettype($newval)) . "' to Collection of type '" . get_class($this) . "'");
throw new \InvalidArgumentException("Can not add element of type '" . (is_object($value) ? get_class($value) : gettype($value)) . "' to Collection of type '" . get_class($this) . "'");
}

/**
Expand Down
64 changes: 45 additions & 19 deletions Core/EventListener/TracingStepExecutedListener.php
Expand Up @@ -12,12 +12,20 @@
/**
* A listener designed to give feedback on the execution of migration steps
*
* @todo qdd proper support for plural forms, as well as for proper sentences when dealing with empty collections
* @todo add proper support for plural forms, as well as for proper sentences when dealing with empty collections
*/
class TracingStepExecutedListener
{
/** @var OutputInterface $output */
protected $output;
protected $minVerbosityLevel = OutputInterface::VERBOSITY_VERBOSE;
protected $entity = 'migration';
protected $enabled = true;

public function __construct($enabled = true)
{
$this->enabled = $enabled;
}

public function setOutput(OutputInterface $output)
{
Expand All @@ -33,12 +41,25 @@ public function setMinVerbosity($level)
$this->minVerbosityLevel = $level;
}

public function enable()
{
$this->enabled = true;
}

public function disable()
{
$this->enabled = false;
}

public function onStepExecuted(StepExecutedEvent $event)
{
if ($this->enabled) {
return;
}

$obj = $event->getResult();
$type = $event->getStep()->type;
$dsl = $event->getStep()->dsl;
$action = isset($dsl['mode']) ? ($dsl['mode'] . 'd') : 'acted upon';

switch ($type) {
case 'content':
Expand All @@ -52,6 +73,7 @@ public function onStepExecuted(StepExecutedEvent $event)
case 'tag':
case 'user':
case 'user_group':
$action = isset($dsl['mode']) ? ($dsl['mode'] . 'd') : 'acted upon';
$out = $type . ' ' . $this->getObjectIdentifierAsString($obj) . ' has been ' . $action;
break;
case 'sql':
Expand All @@ -62,47 +84,51 @@ public function onStepExecuted(StepExecutedEvent $event)
break;
default:
// custom migration step types...
$out = "migration step '$type' has been executed";
if (isset($dsl['mode'])) {
$type .= ' / ' . $dsl['mode'];
}
$out = $this->entity . " step '$type' has been executed";
}

if ($this->output) {
if ($this->output->getVerbosity() >= $this->minVerbosityLevel) {
$this->output->writeln($out);
}
} else {
echo $out . "\n";
}
$this->echoMessage($out);
}

public function onMigrationAborted(MigrationAbortedEvent $event)
{
if ($this->enabled) {
return;
}

$type = $event->getStep()->type;
$dsl = $event->getStep()->dsl;
if (isset($dsl['mode'])) {
$type .= '/' . $dsl['mode'];
}

$out = "migration aborted with status " . $event->getException()->getCode() . " during execution of step '$type'. Message: " . $event->getException()->getMessage();
$out = $this->entity . " aborted with status " . $event->getException()->getCode() . " during execution of step '$type'. Message: " . $event->getException()->getMessage();

if ($this->output) {
if ($this->output->getVerbosity() >= $this->minVerbosityLevel) {
$this->output->writeln($out);
}
} else {
echo $out . "\n";
}
$this->echoMessage($out);
}

public function onMigrationSuspended(MigrationSuspendedEvent $event)
{
if ($this->enabled) {
return;
}

$type = $event->getStep()->type;
$dsl = $event->getStep()->dsl;
if (isset($dsl['mode'])) {
$type .= '/' . $dsl['mode'];
}

$out = "migration suspended during execution of step '$type'. Message: " . $event->getException()->getMessage();
$out = $this->entity . " suspended during execution of step '$type'. Message: " . $event->getException()->getMessage();

$this->echoMessage($out);
}

protected function echoMessage($out)
{
if ($this->output) {
if ($this->output->getVerbosity() >= $this->minVerbosityLevel) {
$this->output->writeln($out);
Expand Down
11 changes: 6 additions & 5 deletions Core/MigrationService.php
Expand Up @@ -56,6 +56,8 @@ class MigrationService implements ContextProviderInterface

protected $eventPrefix = 'ez_migration.';

protected $eventEntity = 'migration';

protected $migrationContext = array();

public function __construct(LoaderInterface $loader, StorageHandlerInterface $storageHandler, Repository $repository,
Expand Down Expand Up @@ -252,8 +254,7 @@ public function executeMigration(MigrationDefinition $migrationDefinition, $useT
}

if ($migrationDefinition->status == MigrationDefinition::STATUS_INVALID) {
/// @todo !important name of entity should be gotten dynamically (migration vs. workflow)
throw new \Exception("Can not execute migration '{$migrationDefinition->name}': {$migrationDefinition->parsingError}");
throw new \Exception("Can not execute " . $this->getEntityName($migrationDefinition). " '{$migrationDefinition->name}': {$migrationDefinition->parsingError}");
}

/// @todo add support for setting in $migrationContext a userContentType ?
Expand Down Expand Up @@ -318,14 +319,14 @@ protected function executeMigrationInner(Migration $migration, MigrationDefiniti
} catch (MigrationAbortedException $e) {
// allow a migration step (or events) to abort the migration via a specific exception

$this->dispatcher->dispatch($this->eventPrefix . 'migration_aborted', new MigrationAbortedEvent($step, $e));
$this->dispatcher->dispatch($this->eventPrefix . $this->eventEntity . '_aborted', new MigrationAbortedEvent($step, $e));

$finalStatus = $e->getCode();
$finalMessage = "Abort in execution of step $i: " . $e->getMessage();
} catch (MigrationSuspendedException $e) {
// allow a migration step (or events) to suspend the migration via a specific exception

$this->dispatcher->dispatch($this->eventPrefix . 'migration_suspended', new MigrationSuspendedEvent($step, $e));
$this->dispatcher->dispatch($this->eventPrefix . $this->eventEntity . '_suspended', new MigrationSuspendedEvent($step, $e));

// let the context handler store our context, along with context data from any other (tagged) service which has some
$this->contextHandler->storeCurrentContext($migration->name);
Expand Down Expand Up @@ -568,6 +569,6 @@ public function restoreContext($migrationName, array $context)

protected function getEntityName($migration)
{
return strtolower(end(explode('\\', get_class($migration))));
return strtolower(preg_replace('/Definition$/', '', end(explode('\\', get_class($migration)))));
}
}
4 changes: 4 additions & 0 deletions Resources/config/services.yml
Expand Up @@ -16,6 +16,8 @@ parameters:
- 'drizzle'
- 'sqlserver'
- 'sqlanywhere'
# Set to false to disable completely the debug output of executed migration steps in verbose mode
ez_migration_bundle.enable_debug_output: true

# Deprecated parameter. Left in for the v1-to-v2 migration to work. Will be removed in v3
kaliop_bundle_migration.table_name: 'kaliop_versions'
Expand Down Expand Up @@ -626,6 +628,8 @@ services:
# This service is used for the verbose mode when executing migrations on the command line
ez_migration_bundle.step_executed_listener.tracing:
class: '%ez_migration_bundle.step_executed_listener.tracing.class%'
arguments:
- '%ez_migration_bundle.enable_debug_output%'
tags:
- { name: kernel.event_listener, event: ez_migration.step_executed, method: onStepExecuted }
- { name: kernel.event_listener, event: ez_migration.migration_aborted, method: onMigrationAborted }
Expand Down

0 comments on commit 0e6cbd2

Please sign in to comment.