3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Capture the output from the console, copy it to a file, and pass it on.
16 * $tee = CRM_Utils_ConsoleTee::create()->start();
19 * assertEquals("hello world", file_get_contents($tee->getFileName()));
22 * Loosely speaking, it serves a similar purpose to Unix `tee`.
24 * @link https://en.wikipedia.org/wiki/Tee_(command)
26 class CRM_Utils_ConsoleTee
{
29 * Capture console output and copy to a temp file.
31 * @param string $prefix
32 * @return CRM_Utils_ConsoleTee
34 public static function create($prefix = 'ConsoleTee-') {
35 return new static(tempnam(sys_get_temp_dir(), $prefix));
49 * CRM_Utils_ConsoleTee constructor.
51 * @param string $fileName
52 * The full path of the file to write to.
54 public function __construct($fileName) {
55 $this->fileName
= $fileName;
59 * Start capturing console output and copying it to a file.
62 * The file output mode, e.g. `w` or `w+`.
63 * @return CRM_Utils_ConsoleTee
66 public function start($mode = 'w') {
67 $this->fh
= fopen($this->fileName
, $mode);
68 ob_start([$this, 'onOutput']);
73 * Process a snippet of data from the output buffer.
79 public function onOutput($buf) {
80 fwrite($this->fh
, $buf);
85 * Stop capturing console output.
87 * @return CRM_Utils_ConsoleTee
89 public function stop() {
98 public function getFileName() {
99 return $this->fileName
;