Skip to content

Commit f2fc858

Browse files
authored
Merge pull request zaantar#3 from displaynone/feature/json-response
Add a new JSON format response so it can be used as an API.
2 parents b111b42 + 1baa4d6 commit f2fc858

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

bin/phpcs-diff

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const OUTPUT_UNDERLINED = "\033[4m";
2929
const OUTPUT_UNUNDERLINED = "\033[24m";
3030

3131
$logo = file_get_contents( PHPCS_DIFF_PLUGIN_DIR . '/logo' );
32-
echo "\n$logo\n" . OUTPUT_UNDERLINED . COLOUR_SECONDARY . 'https://github.com/zaantar/phpcs-diff' . OUTPUT_RESET . "\n\n";
3332

3433
require_once PHPCS_DIFF_PLUGIN_DIR . '/vendor/autoload.php';
3534

@@ -48,6 +47,7 @@ $getopt = new GetOpt(
4847
[ 'excluded_exts', GetOpt::OPTIONAL_ARGUMENT ],
4948
[ 'standards_location', GetOpt::OPTIONAL_ARGUMENT ],
5049
[ 'default_standards_location', GetOpt::NO_ARGUMENT ],
50+
[ 'format', GetOpt::OPTIONAL_ARGUMENT ],
5151
]
5252
);
5353

@@ -83,6 +83,8 @@ $no_colours = $getopt->offsetExists( 'no_colours' );
8383
$excluded_exts = $getopt->getOption( 'excluded_exts' );
8484
$standards_location = $getopt->getOption( 'standards_location' );
8585
$default_standards_location = $getopt->getOption( 'default_standards_location' );
86+
$format = $getopt->getOption( 'format' );
87+
$do_echo = $format !== 'json';
8688

8789
$sniff_unstaged = $getopt->offsetExists( 'sniff_unstaged' );
8890
if ( $sniff_unstaged ) {
@@ -137,11 +139,15 @@ if( $default_standards_location ) {
137139
}
138140

139141
$logger = new Log\ShellLogger( (int) $log_level );
140-
$options = [ 'ignore-space-change' => $ignore_space_changes, 'standards_location' => $standards_location ];
142+
$options = [ 'ignore-space-change' => $ignore_space_changes, 'standards_location' => $standards_location, 'format' => $format ];
141143
$version_control = new Backends\Git( '', $logger, $options );
142144
$controller = new Main( $version_control, $logger, $options );
143145
$controller->set_phpcs_standard( $phpcs_standard );
144146

147+
if ( $do_echo ) {
148+
echo "\n$logo\n" . OUTPUT_UNDERLINED . COLOUR_SECONDARY . 'https://github.com/zaantar/phpcs-diff' . OUTPUT_RESET . "\n\n";
149+
}
150+
145151
try {
146152
$found_issues = $controller->run( $start_revision, $end_revision, '', $excluded_exts );
147153
} catch ( \Exception $e ) {
@@ -201,6 +207,15 @@ function echo_chapter( $title, $items ) {
201207
echo PHP_EOL;
202208
}
203209

210+
if ( $format === 'json' ) {
211+
echo json_encode( array(
212+
'blockers' => $blockers,
213+
'warnings' => $warnings,
214+
'notes' => $notes,
215+
) );
216+
exit();
217+
}
218+
204219
echo PHP_EOL;
205220
echo_chapter( 'Blockers', $blockers );
206221
echo_chapter( 'Warnings', $warnings );

inc/Main.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Main {
2626

2727
private $no_diff_to_big = false;
2828

29+
private $format = false;
30+
2931
/** @var LoggerInterface */
3032
private $log;
3133

@@ -50,6 +52,7 @@ public function __construct( $version_control, LoggerInterface $log, $options =
5052
$this->allowed_extensions = array( 'php', 'js' );
5153

5254
$this->log = $log;
55+
$this->log->set_enable( $this->format !== 'json' );
5356
}
5457

5558
public function set_nocache( $nocache = false ) {

inc/log/LoggerInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ interface LoggerInterface {
2323
*/
2424
public function log( $severity, $message );
2525

26+
/**
27+
* @param $enable boolea
28+
*
29+
* @return void
30+
*/
31+
public function set_enable( $enable );
32+
2633
}

inc/log/ShellLogger.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class ShellLogger implements LoggerInterface {
77

88
private $log_level;
9-
9+
private $enable = true;
1010

1111
public function __construct( $log_level = LoggerInterface::WARNING ) {
1212
$this->log_level = $log_level;
@@ -20,7 +20,7 @@ public function __construct( $log_level = LoggerInterface::WARNING ) {
2020
*/
2121
public function log( $severity, $message ) {
2222

23-
if( $this->log_level > $severity ) {
23+
if( $this->log_level > $severity || ! $this->enable ) {
2424
return;
2525
}
2626

@@ -36,4 +36,14 @@ public function log( $severity, $message ) {
3636
break;
3737
}
3838
}
39+
40+
/**
41+
* @param $enable boolea
42+
*
43+
* @return void
44+
*/
45+
public function set_enable( $enable ) {
46+
$this->enable = $enable;
47+
}
48+
3949
}

inc/log/WpcliLogger.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @package PHPCSDiff\Log
1111
*/
1212
class WpcliLogger implements LoggerInterface {
13+
private $enable = true;
1314

1415
/** @noinspection PhpDocMissingThrowsInspection */
1516
/**
@@ -19,6 +20,10 @@ class WpcliLogger implements LoggerInterface {
1920
* @return void
2021
*/
2122
public function log( $severity, $message ) {
23+
if ( ! $this->enable ) {
24+
return;
25+
}
26+
2227
switch( $severity ) {
2328
case LoggerInterface::ERROR:
2429
/** @noinspection PhpUnhandledExceptionInspection */
@@ -34,4 +39,13 @@ public function log( $severity, $message ) {
3439
}
3540
}
3641

42+
/**
43+
* @param $enable boolea
44+
*
45+
* @return void
46+
*/
47+
public function set_enable( $enable ) {
48+
$this->enable = $enable;
49+
}
50+
3751
}

0 commit comments

Comments
 (0)