Skip to content

Commit dd83c3a

Browse files
committed
feat: import export bundle
1 parent 8bbffde commit dd83c3a

39 files changed

+507
-112
lines changed

components/ImportExportBundle/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
}
3030
},
3131
"suggest": {
32+
"matthiasnoback/symfony-console-form": "Used to execute workflow from CLI",
3233
"phpoffice/phpspreadsheet": "Used for the XlsReader",
3334
"symfony/messenger": "Used to run job asynchronously"
3435
},
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlmaviaCX\Bundle\IbexaImportExportBundle\Command;
6+
7+
use AlmaviaCX\Bundle\IbexaImportExport\Monolog\WorkflowConsoleLogger;
8+
use AlmaviaCX\Bundle\IbexaImportExport\Workflow\Form\Type\WorkflowProcessConfigurationFormType;
9+
use AlmaviaCX\Bundle\IbexaImportExport\Workflow\WorkflowConfiguration;
10+
use AlmaviaCX\Bundle\IbexaImportExport\Workflow\WorkflowEvent;
11+
use AlmaviaCX\Bundle\IbexaImportExport\Workflow\WorkflowExecutor;
12+
use AlmaviaCX\Bundle\IbexaImportExport\Workflow\WorkflowRegistry;
13+
use InvalidArgumentException;
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Helper\ProgressBar;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
21+
class ExecuteWorkflowCommand extends Command
22+
{
23+
protected WorkflowRegistry $workflowRegistry;
24+
protected WorkflowExecutor $workflowExecutor;
25+
26+
protected static $defaultName = 'import_export:workflow:execute';
27+
28+
public function __construct(WorkflowRegistry $workflowRegistry, WorkflowExecutor $workflowExecutor)
29+
{
30+
$this->workflowExecutor = $workflowExecutor;
31+
$this->workflowRegistry = $workflowRegistry;
32+
parent::__construct();
33+
}
34+
35+
protected function configure()
36+
{
37+
parent::configure();
38+
$this->addArgument('identifier', InputArgument::REQUIRED, 'Workflow identifier');
39+
$this->addOption('debug', null, InputOption::VALUE_NONE, 'Enable debug mode');
40+
}
41+
42+
protected function execute(InputInterface $input, OutputInterface $output): int
43+
{
44+
$workflowIdentifier = $input->getArgument('identifier');
45+
$workflow = $this->workflowRegistry->getWorkflow($workflowIdentifier);
46+
$baseConfiguration = $workflow::getDefaultConfig();
47+
if (!$baseConfiguration->isAvailable(WorkflowConfiguration::AVAILABILITY_CLI)) {
48+
throw new InvalidArgumentException(sprintf('Workflow %s is not available', $workflowIdentifier));
49+
}
50+
51+
/** @var \Matthias\SymfonyConsoleForm\Console\Helper\FormHelper $formHelper */
52+
$formHelper = $this->getHelper('form');
53+
$runtimeProcessConfiguration = $formHelper->interactUsingForm(
54+
WorkflowProcessConfigurationFormType::class,
55+
$input,
56+
$output,
57+
['default_configuration' => $baseConfiguration->getProcessConfiguration()]
58+
);
59+
60+
$progressBar = new ProgressBar($output);
61+
62+
$workflow->addEventListener(WorkflowEvent::START, function (WorkflowEvent $event) use ($progressBar) {
63+
$progressBar->start($event->getWorkflow()->getTotalItemsCount());
64+
});
65+
$workflow->addEventListener(WorkflowEvent::PROGRESS, function () use ($progressBar) {
66+
$progressBar->advance();
67+
});
68+
$logger = new WorkflowConsoleLogger($output);
69+
$workflow->setLogger($logger);
70+
$workflow->setDebug($input->getOption('debug'));
71+
($this->workflowExecutor)($workflow, $runtimeProcessConfiguration);
72+
$workflow->clean();
73+
74+
return Command::SUCCESS;
75+
}
76+
}

components/ImportExportBundle/src/bundle/Resources/config/forms.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ services:
1515
AlmaviaCX\Bundle\IbexaImportExport\Job\Form\JobCreateFlow:
1616
parent: craue.form.flow
1717

18+
AlmaviaCX\Bundle\IbexaImportExport\Reader\File\FileReaderOptionsFormType:
19+
tags:
20+
- { name: form.type, alias: FileReaderOptionsFormType }
21+
1822
AlmaviaCX\Bundle\IbexaImportExport\Reader\Csv\CsvReaderOptionsFormType:
23+
parent: AlmaviaCX\Bundle\IbexaImportExport\Reader\File\FileReaderOptionsFormType
1924
tags:
2025
- { name: form.type, alias: CsvReaderOptionsFormType }
2126

@@ -24,6 +29,7 @@ services:
2429
- { name: form.type, alias: IbexaContentListReaderOptionsFormType }
2530

2631
AlmaviaCX\Bundle\IbexaImportExport\Reader\Xls\XlsReaderOptionsFormType:
32+
parent: AlmaviaCX\Bundle\IbexaImportExport\Reader\File\FileReaderOptionsFormType
2733
tags:
2834
- { name: form.type, alias: XlsReaderOptionsFormType }
2935

components/ImportExportBundle/src/bundle/Resources/config/item_value_transformer/transformers.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,19 @@ services:
2020
AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\Utils\SprintfTransformer:
2121
tags:
2222
- { name: almaviacx.import_export.item.value_transformer, alias: almaviacx.import_export.item.value_transformer.sprintf}
23+
24+
AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\Utils\ToStringTransformer:
25+
tags:
26+
- { name: almaviacx.import_export.item.value_transformer, alias: almaviacx.import_export.item.value_transformer.to_string}
27+
28+
AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\Ibexa\HtmlToRichtextTransformer:
29+
arguments:
30+
$richtextInputHandler: '@Ibexa\FieldTypeRichText\RichText\InputHandler'
31+
tags:
32+
- { name: almaviacx.import_export.item.value_transformer, alias: almaviacx.import_export.item.value_transformer.html_to_richtext}
33+
34+
AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\Ibexa\TextToRichtextTransformer:
35+
arguments:
36+
$htmlToRichtextTransformer: '@AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\Ibexa\HtmlToRichtextTransformer'
37+
tags:
38+
- { name: almaviacx.import_export.item.value_transformer, alias: almaviacx.import_export.item.value_transformer.text_to_richtext}

components/ImportExportBundle/src/bundle/Resources/config/services.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ services:
2424
$router: '@router'
2525
tags:
2626
- { name: ibexa.notification.renderer, alias: import_export:notification:default }
27+
28+
AlmaviaCX\Bundle\IbexaImportExportBundle\Command\ExecuteWorkflowCommand:
29+
arguments:
30+
$workflowExecutor: '@AlmaviaCX\Bundle\IbexaImportExport\Workflow\WorkflowExecutor'
31+
$workflowRegistry: '@AlmaviaCX\Bundle\IbexaImportExport\Workflow\WorkflowRegistry'
32+
tags:
33+
- console.command

components/ImportExportBundle/src/bundle/Resources/config/workflow/component/reader.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
AlmaviaCX\Bundle\IbexaImportExport\Reader\Csv\CsvReader:
1010
arguments:
1111
$fileHandler: '@AlmaviaCX\Bundle\IbexaImportExport\File\FileHandler'
12+
$slugConverter: '@Ibexa\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter'
1213
tags:
1314
- { name: almaviacx.import_export.reader, alias: reader.csv}
1415

components/ImportExportBundle/src/lib/Accessor/ArrayAccessor.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use AlmaviaCX\Bundle\IbexaImportExport\Item\ItemAccessorInterface;
88
use ArrayAccess;
9+
use Exception;
910

1011
class ArrayAccessor implements ItemAccessorInterface, ArrayAccess
1112
{
@@ -25,14 +26,27 @@ public function __construct(array $array)
2526
*/
2627
public function offsetExists($offset): bool
2728
{
28-
return isset($this->array, $offset);
29+
return isset($this->array[$offset]);
2930
}
3031

3132
/**
3233
* @param $offset
3334
*/
3435
public function offsetGet($offset)
3536
{
37+
if (!$this->offsetExists($offset)) {
38+
throw new Exception(
39+
sprintf(
40+
'Undefined offset: %s.
41+
Available offsets are %s',
42+
$offset,
43+
implode(' / ', array_map(function ($value) {
44+
return "'$value'";
45+
}, array_keys($this->array)))
46+
)
47+
);
48+
}
49+
3650
return $this->array[$offset];
3751
}
3852

components/ImportExportBundle/src/lib/Component/AbstractComponent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ public function getOptions(): ComponentOptions
3939
{
4040
return $this->options;
4141
}
42+
43+
public function clean(): void
44+
{
45+
}
4246
}

components/ImportExportBundle/src/lib/Component/ComponentInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public static function getOptionsType(): ?string;
2323
public function setOptions(ComponentOptions $options): void;
2424

2525
public function getOptions(): ComponentOptions;
26+
27+
public function clean(): void;
2628
}

components/ImportExportBundle/src/lib/Item/ItemMapper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ protected function getPropertyValue($objectOrArray, $source)
8080
return $this->getPropertyMultipleValue($objectOrArray, $source);
8181
}
8282

83-
return $this->propertyAccessor->getValue($objectOrArray, $source);
83+
$value = $this->propertyAccessor->getValue($objectOrArray, $source);
84+
85+
return is_string($value) ? trim($value) : $value;
8486
}
8587
} catch (NoSuchIndexException|NoSuchPropertyException $exception) {
8688
return null;

0 commit comments

Comments
 (0)