16
16
*/
17
17
18
18
use Composer \Script \Event ;
19
+ use Helhum \Typo3Console \Error \ExceptionRenderer ;
19
20
use Helhum \Typo3Console \Mvc \Cli \CommandDispatcher ;
20
21
use Helhum \Typo3Console \Mvc \Cli \FailedSubProcessCommandException ;
21
22
use TYPO3 \CMS \Composer \Plugin \Core \InstallerScript ;
23
+ use Typo3Console \ComposerAutoCommands \Composer \ConsoleIo ;
22
24
23
25
class ConsoleCommand implements InstallerScript
24
26
{
@@ -42,26 +44,35 @@ class ConsoleCommand implements InstallerScript
42
44
*/
43
45
private $ shouldRun ;
44
46
47
+ /**
48
+ * @var bool
49
+ */
50
+ private $ allowFailure ;
51
+
52
+ private static $ verbosityHint = true ;
53
+
45
54
public function __construct (
46
55
string $ command ,
47
56
array $ arguments = [],
48
57
string $ message = '' ,
49
- callable $ shouldRun = null
58
+ callable $ shouldRun = null ,
59
+ bool $ allowFailure = true
50
60
) {
51
61
$ this ->command = $ command ;
52
62
$ this ->arguments = $ arguments ;
53
63
$ this ->message = $ message ;
54
- $ this ->shouldRun = $ shouldRun ?: function () {
64
+ $ this ->shouldRun = $ shouldRun ?? function () {
55
65
return true ;
56
66
};
67
+ $ this ->allowFailure = $ allowFailure ;
57
68
}
58
69
59
70
public function run (Event $ event ): bool
60
71
{
61
72
if (!($ this ->shouldRun )()) {
62
73
return true ;
63
74
}
64
- $ io = $ event ->getIO ();
75
+ $ io = new ConsoleIo ( $ event ->getIO () );
65
76
if ($ this ->message ) {
66
77
$ io ->writeError (sprintf ('<info>%s</info> ' , $ this ->message ));
67
78
}
@@ -71,34 +82,23 @@ public function run(Event $event): bool
71
82
$ output = $ commandDispatcher ->executeCommand ($ this ->command , $ this ->arguments );
72
83
$ io ->writeError ($ output , true , $ io ::VERBOSE );
73
84
} 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 ;
92
87
}
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 ()
101
94
);
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 );
102
102
}
103
103
}
104
104
0 commit comments