| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPConsole\Core\Console\Psy\Output; |
| 4 |
|
| 5 |
use Psy\Output\OutputPager; |
| 6 |
use Psy\Output\PassthruPager; |
| 7 |
use Psy\Output\ProcOutputPager; |
| 8 |
use Psy\Output\ShellOutput as PsyShellOutput; |
| 9 |
use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
| 10 |
use Symfony\Component\Console\Output\ConsoleOutput; |
| 11 |
use WPConsole\Core\Console\Psy\Output\OutputFormatterStyle; |
| 12 |
|
| 13 |
class ShellOutput extends PsyShellOutput { |
| 14 |
|
| 15 |
/** |
| 16 |
* Output message |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $outputMessage = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Holds exceptions |
| 26 |
* |
| 27 |
* @since 1.1.0 |
| 28 |
* |
| 29 |
* @var null|\Exception |
| 30 |
*/ |
| 31 |
public $exception = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* The output pager. |
| 35 |
* |
| 36 |
* @since 2.4.0 |
| 37 |
* |
| 38 |
* @var null|string|OutputPager |
| 39 |
*/ |
| 40 |
private $pager; |
| 41 |
|
| 42 |
/** |
| 43 |
* Construct a ShellOutput instance. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* |
| 47 |
* @param mixed $verbosity (default: self::VERBOSITY_NORMAL) |
| 48 |
* @param bool $decorated (default: null) |
| 49 |
* @param OutputFormatterInterface $formatter (default: null) |
| 50 |
* @param null|string|OutputPager $pager (default: null) |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function __construct( $verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null, $pager = null ) { |
| 55 |
ConsoleOutput::__construct( $verbosity, $decorated, $formatter ); |
| 56 |
|
| 57 |
$this->initFormatters(); |
| 58 |
|
| 59 |
if ( $pager === null ) { |
| 60 |
$this->pager = new PassthruPager( $this ); |
| 61 |
} elseif (\is_string($pager)) { |
| 62 |
$this->pager = new ProcOutputPager( $this, $pager ); |
| 63 |
} elseif ( $pager instanceof OutputPager ) { |
| 64 |
$this->pager = $pager; |
| 65 |
} else { |
| 66 |
throw new \InvalidArgumentException( 'Unexpected pager parameter: ' . $pager ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Writes a message to the output. |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* |
| 75 |
* @param string $message A message to write to the output |
| 76 |
* @param bool $newline Whether to add a newline or not |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function doWrite( $message, $newline ) { |
| 81 |
$this->outputMessage .= $message; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Initialize output formatter styles. |
| 86 |
* |
| 87 |
* @since 1.0.0 |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
protected function initFormatters() { |
| 92 |
$formatter = $this->getFormatter(); |
| 93 |
|
| 94 |
$formatter->setStyle( 'warning', new OutputFormatterStyle( 'black', 'yellow' ) ); |
| 95 |
$formatter->setStyle( 'error', new OutputFormatterStyle( 'black', 'red', ['bold'] ) ); |
| 96 |
$formatter->setStyle( 'aside', new OutputFormatterStyle( 'blue' ) ); |
| 97 |
$formatter->setStyle( 'strong', new OutputFormatterStyle( null, null, ['bold'] ) ); |
| 98 |
$formatter->setStyle( 'return', new OutputFormatterStyle( 'cyan' ) ); |
| 99 |
$formatter->setStyle( 'urgent', new OutputFormatterStyle( 'red' ) ); |
| 100 |
$formatter->setStyle( 'hidden', new OutputFormatterStyle( 'black' ) ); |
| 101 |
|
| 102 |
// Visibility |
| 103 |
$formatter->setStyle( 'public', new OutputFormatterStyle( null, null, ['bold'] ) ); |
| 104 |
$formatter->setStyle( 'protected', new OutputFormatterStyle( 'yellow' ) ); |
| 105 |
$formatter->setStyle( 'private', new OutputFormatterStyle( 'red' ) ); |
| 106 |
$formatter->setStyle( 'global', new OutputFormatterStyle( 'cyan', null, ['bold'] ) ); |
| 107 |
$formatter->setStyle( 'const', new OutputFormatterStyle( 'cyan' ) ); |
| 108 |
$formatter->setStyle( 'class', new OutputFormatterStyle( 'blue', null, ['underscore'] ) ); |
| 109 |
$formatter->setStyle( 'function', new OutputFormatterStyle( null)); |
| 110 |
$formatter->setStyle( 'default', new OutputFormatterStyle( null)); |
| 111 |
|
| 112 |
// Types |
| 113 |
$formatter->setStyle( 'number', new OutputFormatterStyle( 'magenta' ) ); |
| 114 |
$formatter->setStyle( 'string', new OutputFormatterStyle( 'green' ) ); |
| 115 |
$formatter->setStyle( 'bool', new OutputFormatterStyle( 'cyan' ) ); |
| 116 |
$formatter->setStyle( 'keyword', new OutputFormatterStyle( 'yellow' ) ); |
| 117 |
$formatter->setStyle( 'comment', new OutputFormatterStyle( 'blue' ) ); |
| 118 |
$formatter->setStyle( 'object', new OutputFormatterStyle( 'blue' ) ); |
| 119 |
$formatter->setStyle( 'resource', new OutputFormatterStyle( 'yellow' ) ); |
| 120 |
} |
| 121 |
} |
| 122 |
|