forked from ioannis-ampatzis/dmt_structure_export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDmtStructureExportCommands.php
146 lines (133 loc) · 4.55 KB
/
DmtStructureExportCommands.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Drush\Commands\dmt_structure_export;
use Composer\Autoload\ClassLoader;
use Consolidation\OutputFormatters\FormatterManager;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drush\Commands\DrushCommands;
use Drush\dmt_structure_export\TableBuilderManager;
use Drush\Log\LogLevel;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
/**
* Drush commands for DMT Structure Export.
*/
class DmtStructureExportCommands extends DrushCommands {
/**
* Default directory for the exported files.
*/
const DMT_STRUCTURE_EXPORT_DEFAULT_DIR = 'dmt_structure_export';
/**
* Autoload our files if they are not already loaded.
*/
public function init() {
if (!class_exists(TableBuilderManager::class)) {
$loader = new ClassLoader();
$loader->addPsr4('Drush\\dmt_structure_export\\', __DIR__ . '/src');
$loader->register();
}
}
/**
* Exports website structure/data information to CSV or table of fields.
*
* The default formatter is CSV.
*
* @param string $export_type
* An export to generate. See \Drush\dmt_structure_export\TableBuilder\
* TableBuilderManager::getTableBuilderTypes().
* @param array $options
* An array of options.
*
* @option format Use the specified output format.
*
* @command dmt-se:export
*
* @bootstrap DRUSH_BOOTSTRAP_DRUPAL_FULL
*
* @usage dmt-se:export
* @usage dmt-se:export entity_bundles --format=table
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
* The data as RowsOfFields.
*/
public function export($export_type, array $options = ['format' => 'csv']) {
try {
$this->init();
$manager = new TableBuilderManager();
$table_builder = $manager->getTableBuilder($export_type);
$table_builder->build();
$table = $table_builder->getTable();
return new RowsOfFields($table);
}
catch (\Exception $e) {
drush_log($e->getMessage(), LogLevel::ERROR);
}
}
/**
* Exports all.
*
* @param array $options
* An array of options.
*
* @option destination Relative or absolute path to the folder where CSVs will be generated.
*
* @command dmt-se:export-all
*
* @bootstrap DRUSH_BOOTSTRAP_DRUPAL_FULL
*/
public function exportAll(array $options = ['destination' => '', 'format' => 'csv']) {
$this->init();
$dst_dir = $this->getDestinationDirectory($options['destination']);
$manager = new TableBuilderManager();
$exports = $manager->getTableBuilders();
foreach ($exports as $export_type => $table_builder) {
try {
// Build table.
$table_builder->build();
$data = new RowsOfFields($table_builder->getTable());
// Write to CSV.
$file_path = $dst_dir . '/' . $export_type . '.csv';
$output = new StreamOutput(fopen($file_path, 'w'));
$this->formatData($output, 'csv', $data, new FormatterOptions());
drush_log(dt('Exported CSV file to @path', ['@path' => $file_path]), LogLevel::SUCCESS);
}
catch (\Exception $e) {
drush_log($e->getMessage(), LogLevel::ERROR);
}
}
}
/**
* Formats and writes RowsOfFields using Consolidation\OutputFormatters.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* Output stream to write to.
* @param string $format
* Data format to output in.
* @param \Consolidation\OutputFormatters\StructuredData\RowsOfFields $data
* The RowsOfFields object to format.
* @param \Consolidation\OutputFormatters\Options\FormatterOptions $options
* Formatter options.
*
* @throws \Consolidation\OutputFormatters\Exception\InvalidFormatException
*/
protected function formatData(OutputInterface $output, $format, RowsOfFields $data, FormatterOptions $options) {
$formatterManager = new FormatterManager();
$formatterManager->write($output, $format, $data, $options);
}
/**
* Returns the destination directory.
*/
protected function getDestinationDirectory($destination) {
$dst_dir = !empty($destination) ? $destination : self::DMT_STRUCTURE_EXPORT_DEFAULT_DIR;
// Handle relative or absolute paths.
if (strpos($dst_dir, '/') !== 0) {
$dst_dir = drush_cwd() . '/' . $dst_dir;
}
// Create the destination dir if needed.
if (!is_dir($dst_dir)) {
drush_mkdir($dst_dir);
drush_log(dt('Directory @path was created', ['@path' => $dst_dir]), LogLevel::INFO);
}
return $dst_dir;
}
}