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: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459:
<?php
/*
* This file is part of the webmozart/console package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\Console\Api\IO;
use Webmozart\Console\Api\Formatter\Formatter;
use Webmozart\Console\Api\Formatter\Style;
use Webmozart\Console\UI\Rectangle;
/**
* Provides methods to access the console input and output.
*
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class IO implements Formatter
{
/**
* Flag: Always write data.
*/
const NORMAL = 0;
/**
* Flag: Only write if the verbosity is "verbose" or greater.
*/
const VERBOSE = 1;
/**
* Flag: Only write if the verbosity is "very verbose" or greater.
*/
const VERY_VERBOSE = 2;
/**
* Flag: Only write if the verbosity is "debug".
*/
const DEBUG = 4;
/**
* @var Input
*/
private $input;
/**
* @var Output
*/
private $output;
/**
* @var Output
*/
private $errorOutput;
/**
* @var Rectangle
*/
private $terminalDimensions;
/**
* Creates an I/O based on the given input and outputs.
*
* @param Input $input The standard input.
* @param Output $output The standard output.
* @param Output $errorOutput The error output.
*/
public function __construct(Input $input, Output $output, Output $errorOutput)
{
$this->input = $input;
$this->output = $output;
$this->errorOutput = $errorOutput;
}
/**
* Returns the standard input.
*
* @return Input The input.
*/
public function getInput()
{
return $this->input;
}
/**
* Returns the standard output.
*
* @return Output The output.
*/
public function getOutput()
{
return $this->output;
}
/**
* Returns the error output.
*
* @return Output The error output.
*/
public function getErrorOutput()
{
return $this->errorOutput;
}
/**
* Reads the given amount of characters from the standard input.
*
* @param int $length The number of characters to read.
* @param string $default The default to return if interaction is disabled.
*
* @return string The characters read from the input.
*
* @throws IOException If reading fails or if the standard input is closed.
*/
public function read($length, $default = null)
{
return $this->input->read($length, $default);
}
/**
* Reads a line from the standard input.
*
* @param string $default The default to return if interaction is disabled.
* @param int $length The maximum number of characters to read. If
* `null`, all characters up to the first newline are
* returned.
*
* @return string The characters read from the input.
*
* @throws IOException If reading fails or if the standard input is closed.
*/
public function readLine($default = null, $length = null)
{
return $this->input->readLine($default, $length);
}
/**
* Writes a string to the standard output.
*
* The string is formatted before it is written to the output.
*
* @param string $string The string to write.
* @param int $flags The flags. One of {@link VERBOSE},
* {@link VERY_VERBOSE} and {@link DEBUG}.
*
* @throws IOException If writing fails or if the standard output is closed.
*/
public function write($string, $flags = null)
{
$this->output->write($string, $flags);
}
/**
* Writes a line of text to the standard output.
*
* The string is formatted before it is written to the output.
*
* @param string $string The string to write. A newline is appended.
* @param int $flags The flags. One of {@link VERBOSE},
* {@link VERY_VERBOSE} and {@link DEBUG}.
*
* @throws IOException If writing fails or if the standard output is closed.
*/
public function writeLine($string, $flags = null)
{
$this->output->writeLine($string, $flags);
}
/**
* Writes a string to the standard output without formatting.
*
* @param string $string The string to write.
* @param int $flags The flags. One of {@link VERBOSE},
* {@link VERY_VERBOSE} and {@link DEBUG}.
*
* @throws IOException If writing fails or if the standard output is closed.
*/
public function writeRaw($string, $flags = null)
{
$this->output->writeRaw($string, $flags);
}
/**
* Writes a line of text to the standard output without formatting.
*
* @param string $string The string to write. A newline is appended.
* @param int $flags The flags. One of {@link VERBOSE},
* {@link VERY_VERBOSE} and {@link DEBUG}.
*
* @throws IOException If writing fails or if the standard output is closed.
*/
public function writeLineRaw($string, $flags = null)
{
$this->output->writeLineRaw($string, $flags);
}
/**
* Writes a string to the error output.
*
* The string is formatted before it is written to the output.
*
* @param string $string The string to write.
* @param int $flags The flags. One of {@link VERBOSE},
* {@link VERY_VERBOSE} and {@link DEBUG}.
*
* @throws IOException If writing fails or if the error output is closed.
*/
public function error($string, $flags = null)
{
$this->errorOutput->write($string, $flags);
}
/**
* Writes a line of text to the error output.
*
* The string is formatted before it is written to the output.
*
* @param string $string The string to write. A newline is appended.
* @param int $flags The flags. One of {@link VERBOSE},
* {@link VERY_VERBOSE} and {@link DEBUG}.
*
* @throws IOException If writing fails or if the error output is closed.
*/
public function errorLine($string, $flags = null)
{
$this->errorOutput->writeLine($string, $flags);
}
/**
* Writes a string to the error output without formatting.
*
* @param string $string The string to write.
* @param int $flags The flags. One of {@link VERBOSE},
* {@link VERY_VERBOSE} and {@link DEBUG}.
*
* @throws IOException If writing fails or if the error output is closed.
*/
public function errorRaw($string, $flags = null)
{
$this->errorOutput->writeRaw($string, $flags);
}
/**
* Writes a line of text to the error output without formatting.
*
* @param string $string The string to write. A newline is appended.
* @param int $flags The flags. One of {@link VERBOSE},
* {@link VERY_VERBOSE} and {@link DEBUG}.
*
* @throws IOException If writing fails or if the error output is closed.
*/
public function errorLineRaw($string, $flags = null)
{
$this->errorOutput->writeLineRaw($string, $flags);
}
/**
* Flushes the outputs and forces all pending text to be written out.
*
* @throws IOException If flushing fails or if the outputs are closed.
*/
public function flush()
{
$this->output->flush();
$this->errorOutput->flush();
}
/**
* Closes the input and the outputs.
*/
public function close()
{
$this->input->close();
$this->output->close();
$this->errorOutput->close();
}
/**
* Enables or disables interaction with the user.
*
* @param bool $interactive Whether the I/O may interact with the user. If
* set to `false`, all calls to {@link read()} and
* {@link readLine()} will immediately return the
* default value.
*/
public function setInteractive($interactive)
{
$this->input->setInteractive($interactive);
}
/**
* Returns whether the user may be asked for input.
*
* @return bool Returns `true` if the user may be asked for input and
* `false` otherwise.
*/
public function isInteractive()
{
return $this->input->isInteractive();
}
/**
* Sets the verbosity of the output.
*
* @param int $verbosity One of the constants {@link NORMAL}, {@link VERBOSE},
* {@link VERY_VERBOSE} or {@link DEBUG}. Only output
* with the given verbosity level or smaller will be
* passed through.
*/
public function setVerbosity($verbosity)
{
$this->output->setVerbosity($verbosity);
$this->errorOutput->setVerbosity($verbosity);
}
/**
* Returns whether the verbosity is {@link VERBOSE} or greater.
*
* @return bool Returns `true` if the verbosity is {@link VERBOSE} or
* greater and `false` otherwise.
*/
public function isVerbose()
{
return $this->output->isVerbose();
}
/**
* Returns whether the verbosity is {@link VERY_VERBOSE} or greater.
*
* @return bool Returns `true` if the verbosity is {@link VERY_VERBOSE} or
* greater and `false` otherwise.
*/
public function isVeryVerbose()
{
return $this->output->isVeryVerbose();
}
/**
* Returns whether the verbosity is {@link DEBUG}.
*
* @return bool Returns `true` if the verbosity is {@link DEBUG} and `false`
* otherwise.
*/
public function isDebug()
{
return $this->output->isDebug();
}
/**
* Returns the current verbosity level.
*
* @return int One of the verbosity constants.
*/
public function getVerbosity()
{
return $this->output->getVerbosity();
}
/**
* Sets whether all output should be suppressed.
*
* @param bool $quiet Pass `true` to suppress all output and `false`
* otherwise.
*/
public function setQuiet($quiet)
{
$this->output->setQuiet($quiet);
$this->errorOutput->setQuiet($quiet);
}
/**
* Returns whether all output is suppressed.
*
* @return bool Returns `true` if all output is suppressed and `false`
* otherwise.
*/
public function isQuiet()
{
return $this->output->isQuiet();
}
/**
* Sets the dimensions of the terminal.
*
* @param Rectangle $dimensions The terminal dimensions.
*/
public function setTerminalDimensions(Rectangle $dimensions)
{
$this->terminalDimensions = $dimensions;
}
/**
* Returns the dimensions of the terminal.
*
* @return Rectangle The terminal dimensions.
*/
public function getTerminalDimensions()
{
if (!$this->terminalDimensions) {
$this->terminalDimensions = $this->getDefaultTerminalDimensions();
}
return $this->terminalDimensions;
}
/**
* Sets the output formatter.
*
* @param Formatter $formatter The output formatter.
*/
public function setFormatter(Formatter $formatter)
{
$this->output->setFormatter($formatter);
$this->errorOutput->setFormatter($formatter);
}
/**
* Returns the output formatter.
*
* @return Formatter The output formatter.
*/
public function getFormatter()
{
return $this->output->getFormatter();
}
/**
* {@inheritdoc}
*/
public function format($string, Style $style = null)
{
return $this->output->format($string, $style);
}
/**
* {@inheritdoc}
*/
public function removeFormat($string)
{
return $this->output->removeFormat($string);
}
/**
* Returns the default terminal dimensions.
*
* @return Rectangle The terminal dimensions.
*/
protected function getDefaultTerminalDimensions()
{
return new Rectangle(80, 20);
}
}