-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExportDataObjectsCommand.php
More file actions
108 lines (90 loc) · 3.7 KB
/
ExportDataObjectsCommand.php
File metadata and controls
108 lines (90 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php declare(strict_types=1);
namespace Neusta\Pimcore\ImportExportBundle\Command;
use Neusta\Pimcore\ImportExportBundle\Command\Base\AbstractExportBaseCommand;
use Neusta\Pimcore\ImportExportBundle\Export\Exporter;
use Neusta\Pimcore\ImportExportBundle\Toolbox\Repository\ExportRepositoryInterface;
use Pimcore\Model\DataObject\Concrete;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
/**
* @extends AbstractExportBaseCommand<Concrete>
*/
#[AsCommand(
name: 'neusta:pimcore:export:objects',
description: 'Export all Pimcore Data Objects in one single file'
)]
class ExportDataObjectsCommand extends AbstractExportBaseCommand
{
protected static $defaultName = 'neusta:pimcore:export:objects';
public function __construct(
ExportRepositoryInterface $repository,
Exporter $exporter,
) {
parent::__construct(
$repository,
$exporter,
['yaml', 'json'],
Concrete::class,
);
}
protected function configure(): void
{
parent::configure();
$formatsList = implode(', ', $this->supportedFormats);
$this
->setDescription('Exports data objects from the system.')
->setHelp(
<<<HELP
The <info>%command.name%</info> command exports objects.
Usage:
<info>php %command.full_name%</info>
Currently supported formats:
$formatsList
Options:
--format Specify the export format (e.g. --format=json)
--output full path filename where exported file will be stored
Example:
php %command.full_name% --format=json --output=/your/path
Example Result:
elements:
-
Pimcore\Model\DataObject:
className: SocialMediaItem
published: true
fields:
label: Instagram
relations:
icon:
Pimcore\Model\Asset: { filename: instagram.png, id: 7, parentId: 6, type: image, path: /Icons/, language: '', key: instagram.png }
id: 2
parentId: 3
type: object
path: '/Social Media Items/'
language: 'en'
key: Instagram
HELP
);
}
protected function exportInFile(array $allElements, InputInterface $input): bool
{
$yamlContent = $this->exporter->export($allElements, $input->getOption('format'), ['includeIds' => $input->getOption('includeIds')]);
$exportFilename = $input->getOption('output');
// Validate filename to prevent directory traversal
$safeFilename = basename($exportFilename);
if ($safeFilename !== $exportFilename) {
$this->io->warning(\sprintf(
'For security reasons, path traversal is not recommended. Using "%s" instead of "%s".',
$safeFilename,
$exportFilename
));
$exportFilename = $safeFilename;
}
$this->io->writeln('Write in file <' . $exportFilename . '>');
$this->io->newLine();
if (!file_put_contents($exportFilename, $yamlContent)) {
$this->io->error('An error occurred while writing the file');
return false;
}
return true;
}
}