1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Cli\Plugins\DebugCreatePlugin |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 7 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import-cli |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cli\Plugins; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
24
|
|
|
use TechDivision\Import\ApplicationInterface; |
25
|
|
|
use TechDivision\Import\Utils\DebugUtilInterface; |
26
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
27
|
|
|
use TechDivision\Import\Utils\InputOptionKeysInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Plugin that creates and sends a debug report via email. |
31
|
|
|
* |
32
|
|
|
* @author Tim Wagner <[email protected]> |
33
|
|
|
* @copyright 2020 TechDivision GmbH <[email protected]> |
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
35
|
|
|
* @link https://github.com/techdivision/import |
36
|
|
|
* @link http://www.techdivision.com |
37
|
|
|
*/ |
38
|
|
|
class DebugCreatePlugin extends AbstractConsolePlugin |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The debug util instance. |
43
|
|
|
* |
44
|
|
|
* @var \TechDivision\Import\Utils\DebugUtilInterface |
45
|
|
|
*/ |
46
|
|
|
private $debugUtil; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The constructor to initialize the plugin with. |
50
|
|
|
* |
51
|
|
|
* @param \TechDivision\Import\ApplicationInterface $application The application instance |
52
|
|
|
* @param \TechDivision\Import\Utils\DebugUtilInterface $debugUtil The debug util instance |
53
|
|
|
*/ |
54
|
|
|
public function __construct(ApplicationInterface $application, DebugUtilInterface $debugUtil) |
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
// set the debug utility |
58
|
|
|
$this->debugUtil = $debugUtil; |
59
|
|
|
|
60
|
|
|
// pass the application to the parent constructor |
61
|
|
|
parent::__construct($application); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return's the debug util instance. |
67
|
|
|
* |
68
|
|
|
* @return \TechDivision\Import\Utils\DebugUtilInterface The debug util instance |
69
|
|
|
*/ |
70
|
|
|
private function getDebugUtil() : DebugUtilInterface |
71
|
|
|
{ |
72
|
|
|
return $this->debugUtil; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Process the plugin functionality. |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
* @throws \InvalidArgumentException Is thrown if either the directory nor a artefact for the given serial is available |
80
|
|
|
*/ |
81
|
|
|
public function process() |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
// load the actual status |
85
|
|
|
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
86
|
|
|
|
87
|
|
|
// query whether or not the configured source directory is available |
88
|
|
|
if (isset($status[RegistryKeys::SOURCE_DIRECTORY])) { |
89
|
|
|
$sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY]; |
90
|
|
|
} else { |
91
|
|
|
throw new \Exception('Source directory is not available!'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// try to load the archive directory |
95
|
|
|
$archiveDir = $this->getConfiguration()->getArchiveDir(); |
96
|
|
|
|
97
|
|
|
// try to initialize a default archive directory by concatenating 'archive' to the target directory |
98
|
|
|
if ($archiveDir === null) { |
|
|
|
|
99
|
|
|
$archiveDir = sprintf('var/import_history'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// retrieve the question helper |
103
|
|
|
$questionHelper = $this->getHelper('question'); |
104
|
|
|
|
105
|
|
|
// initialize the array for the available serials |
106
|
|
|
$availableSerials = array(); |
107
|
|
|
|
108
|
|
|
// load the directories from the actual working directory (broken imports, etc.) |
109
|
|
|
foreach (glob(sprintf('%s/*', $sourceDir), GLOB_ONLYDIR) as $possibleImportDir) { |
110
|
|
|
$availableSerials[basename($possibleImportDir)] = filemtime($possibleImportDir); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// load the ZIP artefacts from the archive directory |
114
|
|
|
foreach (glob(sprintf('%s/*.zip', $archiveDir)) as $possibleArtefact) { |
115
|
|
|
$availableSerials[ basename($possibleArtefact, '.zip')] = filemtime($possibleArtefact); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// sort the available serials by modification time |
119
|
|
|
uasort($availableSerials, function ($a, $b) { |
120
|
|
|
// return zero, if the passed values are equal |
121
|
|
|
if ($a == $b) { |
122
|
|
|
return 0; |
123
|
|
|
} |
124
|
|
|
// otherwise return -1 or 1 |
125
|
|
|
return ($a > $b) ? -1 : 1; |
126
|
|
|
}); |
127
|
|
|
|
128
|
|
|
// finally create the array with the available serials to render on the console |
129
|
|
|
$availableSerials = array_slice(array_keys($availableSerials), 0, $this->getInput()->getOption(InputOptionKeysInterface::RENDER_DEBUG_SERIALS)); |
130
|
|
|
|
131
|
|
|
// this is, when the import:debug send command has been invoked |
132
|
|
|
// WITHOUT the --serial=<UUID> parameter or an invalid serial |
133
|
|
|
if (!in_array($serial = $this->getSerial(), $availableSerials, true)) { |
134
|
|
|
// create the question instance to choose the serial |
135
|
|
|
$chooseSerialQuestion = new ChoiceQuestion('Please select the serial to create the debug artefact for', $availableSerials); |
136
|
|
|
$chooseSerialQuestion->setErrorMessage('Selected serial "%s" is invalid.'); |
137
|
|
|
|
138
|
|
|
// abort the operation if the user does not confirm with 'y' or enter |
139
|
|
|
if (!$serial = $questionHelper->ask($this->getInput(), $this->getOutput(), $chooseSerialQuestion)) { |
|
|
|
|
140
|
|
|
$this->getOutput()->writeln('<info>Aborting operation - debug report has NOT been sent.</info>'); |
141
|
|
|
return; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// update the registry with the (probably new) serial and the source directory |
146
|
|
|
$this->getRegistryProcessor()->mergeAttributesRecursive( |
147
|
|
|
RegistryKeys::STATUS, |
148
|
|
|
array( |
149
|
|
|
RegistryKeys::DEBUG_SERIAL => $serial, |
150
|
|
|
RegistryKeys::SOURCE_DIRECTORY => sprintf('%s/%s', $sourceDir, $serial) |
151
|
|
|
) |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
// prepare the debug dump |
155
|
|
|
$this->getDebugUtil()->extractArchive($serial); |
156
|
|
|
$this->getDebugUtil()->prepareDump($serial); |
157
|
|
|
$dumpFilename = $this->getDebugUtil()->createDump($serial); |
158
|
|
|
|
159
|
|
|
// write a success message to the console |
160
|
|
|
$this->getOutput()->writeln(sprintf('<info>Successfully created debug dump "%s"</info>', $dumpFilename)); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|