| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Capture the output from the console, copy it to a file, and pass it on. |
| 30 | * |
| 31 | * @code |
| 32 | * $tee = CRM_Utils_ConsoleTee::create()->start(); |
| 33 | * echo "hello world"; |
| 34 | * $tee->stop(); |
| 35 | * assertEquals("hello world", file_get_contents($tee->getFileName())); |
| 36 | * @endCode |
| 37 | * |
| 38 | * Loosely speaking, it serves a similar purpose to Unix `tee`. |
| 39 | * |
| 40 | * @link https://en.wikipedia.org/wiki/Tee_(command) |
| 41 | */ |
| 42 | class CRM_Utils_ConsoleTee { |
| 43 | |
| 44 | /** |
| 45 | * Capture console output and copy to a temp file. |
| 46 | * |
| 47 | * @param string $prefix |
| 48 | * @return CRM_Utils_ConsoleTee |
| 49 | */ |
| 50 | public static function create($prefix = 'ConsoleTee-') { |
| 51 | return new static(tempnam(sys_get_temp_dir(), $prefix)); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @var string |
| 56 | */ |
| 57 | protected $fileName; |
| 58 | |
| 59 | /** |
| 60 | * @var resource |
| 61 | */ |
| 62 | protected $fh; |
| 63 | |
| 64 | /** |
| 65 | * CRM_Utils_ConsoleTee constructor. |
| 66 | * |
| 67 | * @param string $fileName |
| 68 | * The full path of the file to write to. |
| 69 | */ |
| 70 | public function __construct($fileName) { |
| 71 | $this->fileName = $fileName; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Start capturing console output and copying it to a file. |
| 76 | * |
| 77 | * @param string $mode |
| 78 | * The file output mode, e.g. `w` or `w+`. |
| 79 | * @return CRM_Utils_ConsoleTee |
| 80 | * @see fopen |
| 81 | */ |
| 82 | public function start($mode = 'w') { |
| 83 | $this->fh = fopen($this->fileName, $mode); |
| 84 | ob_start(array($this, 'onOutput')); |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Process a snippet of data from the output buffer. |
| 90 | * |
| 91 | * @param string $buf |
| 92 | * @return bool |
| 93 | * @see ob_start |
| 94 | */ |
| 95 | public function onOutput($buf) { |
| 96 | fwrite($this->fh, $buf); |
| 97 | return FALSE; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Stop capturing console output. |
| 102 | * |
| 103 | * @return CRM_Utils_ConsoleTee |
| 104 | */ |
| 105 | public function stop() { |
| 106 | ob_end_flush(); |
| 107 | fclose($this->fh); |
| 108 | return $this; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return string |
| 113 | */ |
| 114 | public function getFileName() { |
| 115 | return $this->fileName; |
| 116 | } |
| 117 | |
| 118 | } |