-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmessenger.php
74 lines (66 loc) · 1.86 KB
/
messenger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Pantheon;
class Messenger {
public $messages = array();
static $instance;
public function __construct() {
self::$instance = $this;
return $this;
}
public static function instance() {
if (self::$instance)
return self::$instance;
return new Messenger();
}
public static function queue($message) {
$messenger = self::instance();
// @todo this changes the check object to an array, better would be to
// create a message object that could behave as an array when necessary
$messenger->addMessage($message);
}
public function addMessage($message) {
return array_push($this->messages, $message);
}
/**
* Emit the message in specified format
*
* @params $format string optional - options are "raw","json"
*/
public static function emit($format='raw') {
$messenger = self::instance();
switch($format) {
case 'pantheon':
case 'json':
$formatted = array();
foreach($messenger->messages as $message) {
$formatted[$message['name']] = $message;
}
\WP_CLI::print_value($formatted,array('format'=>'json'));
break;
case 'raw':
case 'default':
foreach ($messenger->messages as $message) {
// colorize
if ( $message['score'] == 2 ) {
$color = "%G";
} elseif ( $message['score'] == 0 ) {
$color = "%C";
} else {
$color = "%R";
}
$recommendation = isset( $message['action'] ) ? sprintf( "Recommendation: %s", $message['action'] ) : '';
// @todo might be a better way to do this
echo \cli\Colors::colorize( sprintf(str_repeat('-',80).PHP_EOL."%s: (%s) \n%s\nResult:%s %s\n%s\n\n".PHP_EOL,
strtoupper($message['label']),
$message['description'],
str_repeat('-',80),
$color,
$message['result'].'%n', // ugly
// Check for a recommended action before printing something.
$recommendation )
);
}
break;
}
}
}