Commit | Line | Data |
---|---|---|
38faa439 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
38faa439 | 5 | | | |
bc77d7c0 TO |
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 | | |
38faa439 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * Capture the output from the console, copy it to a file, and pass it on. | |
14 | * | |
0b882a86 | 15 | * ``` |
38faa439 TO |
16 | * $tee = CRM_Utils_ConsoleTee::create()->start(); |
17 | * echo "hello world"; | |
18 | * $tee->stop(); | |
19 | * assertEquals("hello world", file_get_contents($tee->getFileName())); | |
0b882a86 | 20 | * ``` |
38faa439 TO |
21 | * |
22 | * Loosely speaking, it serves a similar purpose to Unix `tee`. | |
23 | * | |
24 | * @link https://en.wikipedia.org/wiki/Tee_(command) | |
25 | */ | |
26 | class CRM_Utils_ConsoleTee { | |
27 | ||
28 | /** | |
29 | * Capture console output and copy to a temp file. | |
30 | * | |
31 | * @param string $prefix | |
32 | * @return CRM_Utils_ConsoleTee | |
33 | */ | |
34 | public static function create($prefix = 'ConsoleTee-') { | |
35 | return new static(tempnam(sys_get_temp_dir(), $prefix)); | |
36 | } | |
37 | ||
38 | /** | |
39 | * @var string | |
40 | */ | |
41 | protected $fileName; | |
42 | ||
43 | /** | |
44 | * @var resource | |
45 | */ | |
46 | protected $fh; | |
47 | ||
48 | /** | |
49 | * CRM_Utils_ConsoleTee constructor. | |
50 | * | |
51 | * @param string $fileName | |
52 | * The full path of the file to write to. | |
53 | */ | |
54 | public function __construct($fileName) { | |
55 | $this->fileName = $fileName; | |
56 | } | |
57 | ||
58 | /** | |
59 | * Start capturing console output and copying it to a file. | |
60 | * | |
61 | * @param string $mode | |
62 | * The file output mode, e.g. `w` or `w+`. | |
63 | * @return CRM_Utils_ConsoleTee | |
64 | * @see fopen | |
65 | */ | |
66 | public function start($mode = 'w') { | |
67 | $this->fh = fopen($this->fileName, $mode); | |
be2fb01f | 68 | ob_start([$this, 'onOutput']); |
38faa439 TO |
69 | return $this; |
70 | } | |
71 | ||
72 | /** | |
73 | * Process a snippet of data from the output buffer. | |
74 | * | |
75 | * @param string $buf | |
76 | * @return bool | |
77 | * @see ob_start | |
78 | */ | |
79 | public function onOutput($buf) { | |
80 | fwrite($this->fh, $buf); | |
81 | return FALSE; | |
82 | } | |
83 | ||
84 | /** | |
85 | * Stop capturing console output. | |
86 | * | |
87 | * @return CRM_Utils_ConsoleTee | |
88 | */ | |
89 | public function stop() { | |
90 | ob_end_flush(); | |
91 | fclose($this->fh); | |
92 | return $this; | |
93 | } | |
94 | ||
95 | /** | |
96 | * @return string | |
97 | */ | |
98 | public function getFileName() { | |
99 | return $this->fileName; | |
100 | } | |
101 | ||
102 | } |