Skip to content

Commit c3ff3a0

Browse files
committed
[FEATURE] Improve handling of error cases
Fixes: #2
1 parent bd12644 commit c3ff3a0

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "GPL-2.0-or-later",
66
"require": {
77
"php": "^7.0",
8-
"helhum/typo3-console": "^4.9.5 || ^5.0 || ^6.0",
8+
"helhum/typo3-console": "^5.8.3 || ^6.0",
99
"typo3/cms-composer-installers": "^1.4.2 || ^2.0"
1010
},
1111
"autoload": {

src/Composer/InstallerScript/ConsoleCommand.php

+29-29
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
*/
1717

1818
use Composer\Script\Event;
19+
use Helhum\Typo3Console\Error\ExceptionRenderer;
1920
use Helhum\Typo3Console\Mvc\Cli\CommandDispatcher;
2021
use Helhum\Typo3Console\Mvc\Cli\FailedSubProcessCommandException;
2122
use TYPO3\CMS\Composer\Plugin\Core\InstallerScript;
23+
use Typo3Console\ComposerAutoCommands\Composer\ConsoleIo;
2224

2325
class ConsoleCommand implements InstallerScript
2426
{
@@ -42,26 +44,35 @@ class ConsoleCommand implements InstallerScript
4244
*/
4345
private $shouldRun;
4446

47+
/**
48+
* @var bool
49+
*/
50+
private $allowFailure;
51+
52+
private static $verbosityHint = true;
53+
4554
public function __construct(
4655
string $command,
4756
array $arguments = [],
4857
string $message = '',
49-
callable $shouldRun = null
58+
callable $shouldRun = null,
59+
bool $allowFailure = true
5060
) {
5161
$this->command = $command;
5262
$this->arguments = $arguments;
5363
$this->message = $message;
54-
$this->shouldRun = $shouldRun ?: function () {
64+
$this->shouldRun = $shouldRun ?? function () {
5565
return true;
5666
};
67+
$this->allowFailure = $allowFailure;
5768
}
5869

5970
public function run(Event $event): bool
6071
{
6172
if (!($this->shouldRun)()) {
6273
return true;
6374
}
64-
$io = $event->getIO();
75+
$io = new ConsoleIo($event->getIO());
6576
if ($this->message) {
6677
$io->writeError(sprintf('<info>%s</info>', $this->message));
6778
}
@@ -71,34 +82,23 @@ public function run(Event $event): bool
7182
$output = $commandDispatcher->executeCommand($this->command, $this->arguments);
7283
$io->writeError($output, true, $io::VERBOSE);
7384
} catch (FailedSubProcessCommandException $e) {
74-
$io->writeError(sprintf('<error>Executing TYPO3 Console command "%s" returned with error code %d.</error>', $e->getCommand(), $e->getExitCode()));
75-
$io->writeError(
76-
sprintf(
77-
"Command line:\n%s\n",
78-
$e->getCommandLine()
79-
),
80-
true,
81-
$io::VERBOSE
82-
);
83-
if ($commandOutput = trim(strip_tags($e->getOutputMessage()))) {
84-
$io->writeError(
85-
sprintf(
86-
"Command output:\n%s\n",
87-
$commandOutput
88-
),
89-
true,
90-
$io::VERBOSE
91-
);
85+
if (!$this->allowFailure) {
86+
throw $e;
9287
}
93-
if ($commandErrorOutput = trim(strip_tags($e->getErrorMessage()))) {
94-
$io->writeError(
95-
sprintf(
96-
"Command error output:\n%s\n",
97-
$commandErrorOutput
98-
),
99-
true,
100-
$io::VERBOSE
88+
if (!$this->allowFailure || $io->getOutput()->isVerbose()) {
89+
(new ExceptionRenderer())->render($e, $io->getOutput());
90+
} else {
91+
$messages[] = sprintf(
92+
'<error>Executing TYPO3 Console command "%s" failed.</error>',
93+
$e->getCommand()
10194
);
95+
if (self::$verbosityHint) {
96+
$messages[] = sprintf(
97+
'<info>For details re-run Composer command with increased verbosity (-vvv).</info>'
98+
);
99+
self::$verbosityHint = false;
100+
}
101+
$io->writeError($messages);
102102
}
103103
}
104104

src/Composer/InstallerScripts.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class InstallerScripts implements InstallerScriptsRegistration
3232
public static function register(Event $event, ScriptDispatcher $scriptDispatcher)
3333
{
3434
$scriptDispatcher->addInstallerScript(
35-
new ConsoleCommand('install:generatepackagestates'),
35+
new ConsoleCommand('install:generatepackagestates', [], '', null, false),
3636
20
3737
);
3838
$scriptDispatcher->addInstallerScript(
39-
new ConsoleCommand('install:fixfolderstructure'),
39+
new ConsoleCommand('install:fixfolderstructure', [], '', null, false),
4040
20
4141
);
4242
$typo3IsSetUp = getenv('TYPO3_IS_SET_UP') || file_exists(getenv('TYPO3_PATH_ROOT') . '/typo3conf/LocalConfiguration.php');

0 commit comments

Comments
 (0)