Output class added
This commit is contained in:
parent
722b1ea521
commit
d691a47731
75
server/src/Output.php
Normal file
75
server/src/Output.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* class Output.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class Output
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isVerbose;
|
||||
|
||||
/*
|
||||
* Constructor.
|
||||
*
|
||||
* @param bool $isVerbose
|
||||
*/
|
||||
public function __construct(bool $isVerbose)
|
||||
{
|
||||
$this->isVerbose = $isVerbose;
|
||||
}
|
||||
/*
|
||||
* Writes a message.
|
||||
*
|
||||
* @param string $message
|
||||
* @param bool $isError
|
||||
*
|
||||
* @return Output
|
||||
*/
|
||||
public function write(string $message, $isError = false)
|
||||
{
|
||||
return $this->doWrite($message, false, $isError);
|
||||
}
|
||||
|
||||
/*
|
||||
* Writes a message with new line.
|
||||
*
|
||||
* @param string $message
|
||||
* @param bool $isError
|
||||
*
|
||||
* @return Output
|
||||
*/
|
||||
public function writeln(string $message, $isError = false)
|
||||
{
|
||||
return $this->doWrite($message, true, $isError);
|
||||
}
|
||||
|
||||
/*
|
||||
* Writes a message.
|
||||
*
|
||||
* @param string $message
|
||||
* @param bool $newLine
|
||||
* @param bool $isError
|
||||
*
|
||||
* @return Output
|
||||
*/
|
||||
public function doWrite(string $message, bool $newLine, bool $isError)
|
||||
{
|
||||
if (!$this->isVerbose) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$output = $isError ? 'php://stdin' : 'php://stderr';
|
||||
|
||||
if ($newLine) {
|
||||
$message .= "\n";
|
||||
}
|
||||
|
||||
file_put_contents($output, $message);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue