Skip to content

Instantly share code, notes, and snippets.

@rskuipers
Created September 4, 2014 06:50
Show Gist options
  • Save rskuipers/924e8c65c17519cc5898 to your computer and use it in GitHub Desktop.
Save rskuipers/924e8c65c17519cc5898 to your computer and use it in GitHub Desktop.
<?php
namespace My\Traits;
use Symfony\Component\Console\Output\OutputInterface;
trait OutputTrait
{
/**
* @param string $message
* @param bool $rewrite
*/
protected function output($message, $rewrite = false)
{
$logFile = $this->getLogFile();
if (!is_null($logFile)) {
if ($rewrite === false) {
$timestamp = (new \DateTime())->format('Y-m-d H:i:s');
file_put_contents($logFile, "[{$timestamp}] {$message}\n", FILE_APPEND);
}
}
if (is_null($this->getOutput()) || $this->getOutput()->getVerbosity() <= 0) {
return;
}
echo $message . str_pad($rewrite ? "\r" : "\n", 25, ' ', STR_PAD_LEFT);
}
/**
* @return OutputInterface
*/
abstract protected function getOutput();
/**
* @return string
*/
abstract protected function getLogFile();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment