Skip to content

Instantly share code, notes, and snippets.

@inri13666
Last active October 7, 2016 15:48
Show Gist options
  • Save inri13666/c909d47a031b85da59026625113244cf to your computer and use it in GitHub Desktop.
Save inri13666/c909d47a031b85da59026625113244cf to your computer and use it in GitHub Desktop.
Workflow extended commands
<?php
namespace Oro\Bundle\WorkflowBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
use Oro\Bundle\WorkflowBundle\Entity\WorkflowDefinition;
class DumpWorkflowDefinitionCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('oro:workflow:definition:dump')
->setDescription('Dumps workflow definition from database to YML format')
->addArgument(
'workflow_name',
null,
'Name of the workflow definition that should be dumped'
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('workflow_name')) {
$output->writeln('Please specify workflow definition name');
return 1;
}
/** @var WorkflowDefinition $workflow */
$workflow = $this->getContainer()->get('doctrine')->getRepository(WorkflowDefinition::class)->findOneBy(
['name' => $input->getArgument('workflow_name')]
);
if ($workflow) {
$general = [
'label' => $workflow->getLabel(),
'entity' => $workflow->getRelatedEntity(),
'entity_attribute' => $workflow->getEntityAttributeName(),
'priority' => $workflow->getPriority() ?: 0,
'defaults' => ['active' => $workflow->isActive()],
'steps_display_ordered' => $workflow->isStepsDisplayOrdered(),
];
if ($workflow->getStartStep()) {
$general['start_step'] = $workflow->getStartStep()->getName();
}
if ($workflow->getExclusiveRecordGroups()) {
$general['exclusive_record_groups'] = $workflow->getExclusiveRecordGroups();
}
if ($workflow->getExclusiveActiveGroups()) {
$general['exclusive_active_groups'] = $workflow->getExclusiveActiveGroups();
}
$definition = [
'workflows' => [
$workflow->getName() =>
array_merge(
$general,
$workflow->getConfiguration()
)
]
];
$output->write(Yaml::dump($definition, 6), true);
} else {
$output->writeln('No workflow definitions found.');
}
}
}
<?php
namespace Oro\Bundle\WorkflowBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
use Oro\Bundle\WorkflowBundle\Entity\WorkflowDefinition;
class ListWorkflowDefinitionCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('oro:workflow:definition:list')
->setDescription('Outputs list of registered workflows');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$workflows = $this->getContainer()->get('doctrine')->getRepository(WorkflowDefinition::class)->findAll();
if (count($workflows)) {
$table = new Table($output);
$table->setHeaders(['System Name', 'Label', 'Related Entity', 'Type', 'Status', 'Exclusive Groups'])
->setRows([]);
/** @var WorkflowDefinition $workflow */
foreach ($workflows as $workflow) {
$row = [
$workflow->getName(),
$workflow->getLabel(),
$workflow->getRelatedEntity(),
$workflow->isSystem() ? 'System' : 'Custom',
$workflow->isActive() ? 'Active' : 'Inactive',
$workflow->getExclusiveRecordGroups()
?
implode(', ', $workflow->getExclusiveRecordGroups())
:
'N/A',
];
$table->addRow($row);
}
$table->render();
} else {
$output->writeln('No workflow definitions found.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment