3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Capture the output from the console, copy it to a file, and pass it on.
32 * $tee = CRM_Utils_ConsoleTee::create()->start();
35 * assertEquals("hello world", file_get_contents($tee->getFileName()));
38 * Loosely speaking, it serves a similar purpose to Unix `tee`.
40 * @link https://en.wikipedia.org/wiki/Tee_(command)
42 class CRM_Utils_ConsoleTee
{
45 * Capture console output and copy to a temp file.
47 * @param string $prefix
48 * @return CRM_Utils_ConsoleTee
50 public static function create($prefix = 'ConsoleTee-') {
51 return new static(tempnam(sys_get_temp_dir(), $prefix));
65 * CRM_Utils_ConsoleTee constructor.
67 * @param string $fileName
68 * The full path of the file to write to.
70 public function __construct($fileName) {
71 $this->fileName
= $fileName;
75 * Start capturing console output and copying it to a file.
78 * The file output mode, e.g. `w` or `w+`.
79 * @return CRM_Utils_ConsoleTee
82 public function start($mode = 'w') {
83 $this->fh
= fopen($this->fileName
, $mode);
84 ob_start([$this, 'onOutput']);
89 * Process a snippet of data from the output buffer.
95 public function onOutput($buf) {
96 fwrite($this->fh
, $buf);
101 * Stop capturing console output.
103 * @return CRM_Utils_ConsoleTee
105 public function stop() {
114 public function getFileName() {
115 return $this->fileName
;