@@ -45,29 +45,58 @@ protected function configure(): void
4545 ->addOption ('fail-on-warning ' , description: 'Fail command if any checks have a warning result ' )
4646 ->addOption ('fail-on-skip ' , description: 'Fail command if any checks are skipped ' )
4747 ->addOption ('fail-on-unknown ' , description: 'Fail command if any checks have an unknown result ' )
48+ ->addOption ('json ' , description: 'Output in JSON format ' )
4849 ;
4950 }
5051
52+ /**
53+ * @throws \JsonException
54+ */
5155 protected function execute (InputInterface $ input , OutputInterface $ output ): int
5256 {
57+ $ isJsonOutput = $ input ->getOption ('json ' );
5358 $ io = new SymfonyStyle ($ input , $ output );
54- $ subscriber = $ io ->isVerbose () ? new ConsoleCheckVerboseSubscriber ($ io ) : new ConsoleCheckListSubscriber ($ input , $ output );
5559
56- $ this ->eventDispatcher ->addSubscriber ($ subscriber );
60+ if (!$ isJsonOutput ) {
61+ $ subscriber = $ io ->isVerbose () ? new ConsoleCheckVerboseSubscriber ($ io ) : new ConsoleCheckListSubscriber ($ input , $ output );
62+ $ this ->eventDispatcher ->addSubscriber ($ subscriber );
63+ }
5764
5865 $ suite = $ this ->checkRegistry ->suite ($ input ->getOption ('suite ' ));
5966
6067 if (!$ suite ->count ()) {
6168 throw new \RuntimeException (\sprintf ('No checks found for suite "%s" ' , $ suite ));
6269 }
6370
71+ $ results = $ suite ->run (!$ input ->getOption ('no-cache ' ));
72+ $ failureStatuses = [];
73+
74+ if ($ input ->getOption ('fail-on-warning ' )) {
75+ $ failureStatuses [] = Status::WARNING ;
76+ }
77+
78+ if ($ input ->getOption ('fail-on-skip ' )) {
79+ $ failureStatuses [] = Status::SKIP ;
80+ }
81+
82+ if ($ input ->getOption ('fail-on-unknown ' )) {
83+ $ failureStatuses [] = Status::UNKNOWN ;
84+ }
85+
86+ $ isFail = $ results ->defects (...$ failureStatuses )->count () > 0 ;
87+
88+ if ($ isJsonOutput ) {
89+ $ output ->write (\json_encode ($ results , \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT , 512 ));
90+
91+ return $ isFail ? self ::FAILURE : self ::SUCCESS ;
92+ }
93+
6494 $ io ->section ($ input ->getOption ('suite ' ) ? \sprintf ('Running Check Suite "%s" ' , $ suite ) : 'Running All Checks ' );
6595
6696 if ($ input ->getOption ('no-cache ' )) {
6797 $ io ->note ('Running with cache disabled ' );
6898 }
6999
70- $ results = $ suite ->run (!$ input ->getOption ('no-cache ' ));
71100 $ warnings = $ results ->warnings ();
72101 $ unknowns = $ results ->unknowns ();
73102 $ summary = \array_filter ([
@@ -79,21 +108,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
79108 $ unknowns ->count () ? \sprintf ('%d unknown%s ' , $ unknowns ->count (), $ unknowns ->count () > 2 ? 's ' : '' ) : null ,
80109 ]);
81110 $ message = \sprintf ('%d check executed (%s) ' , $ results ->count (), \implode (', ' , $ summary ));
82- $ failureStatuses = [];
83-
84- if ($ input ->getOption ('fail-on-warning ' )) {
85- $ failureStatuses [] = Status::WARNING ;
86- }
87-
88- if ($ input ->getOption ('fail-on-skip ' )) {
89- $ failureStatuses [] = Status::SKIP ;
90- }
91-
92- if ($ input ->getOption ('fail-on-unknown ' )) {
93- $ failureStatuses [] = Status::UNKNOWN ;
94- }
95-
96- $ isFail = $ results ->defects (...$ failureStatuses )->count () > 0 ;
97111
98112 $ io ->newLine ();
99113 $ io ->{$ isFail ? 'error ' : 'success ' }($ message );
0 commit comments