From 38faa43962685d8263dfe4eefc02825dc4aed02a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 13 Apr 2017 11:19:26 -0700 Subject: [PATCH] CRM-20420 - Extract utility class, CRM_Utils_ConsoleTee --- CRM/Contact/Form/Task/PDFLetterCommon.php | 24 ++--- CRM/Utils/ConsoleTee.php | 118 ++++++++++++++++++++++ 2 files changed, 125 insertions(+), 17 deletions(-) create mode 100644 CRM/Utils/ConsoleTee.php diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index 979488a615..fe69fd100c 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -410,21 +410,12 @@ class CRM_Contact_Form_Task_PDFLetterCommon { $html[] = $tokenHtml; } - $saveToFile = Civi::settings()->get('recordGeneratedLetters') === 'combined-attached'; - if ($saveToFile) { + $tee = NULL; + if (Civi::settings()->get('recordGeneratedLetters') === 'combined-attached') { if (count($activityIds) !== 1) { throw new CRM_Core_Exception("When recordGeneratedLetters=combined-attached, there should only be one activity."); } - - // We need to capture data from various output-generators whose only - // consistent output format is writing to the console (for download). - $tmpFile = tempnam(sys_get_temp_dir(), 'PDFLetterCommon-'); - $tmpFh = fopen($tmpFile, 'w'); - ob_start(function ($buf) use ($tmpFh) { - // Pass to file, then continue to stdout. - fwrite($tmpFh, $buf); - return FALSE; - }); + $tee = CRM_Utils_ConsoleTee::create()->start(); } $type = $formValues['document_type']; @@ -444,10 +435,9 @@ class CRM_Contact_Form_Task_PDFLetterCommon { CRM_Utils_PDF_Document::html2doc($html, $fileName, $formValues); } - if ($saveToFile) { - ob_end_flush(); - fclose($tmpFh); - $content = file_get_contents($tmpFile, NULL, NULL, NULL, 5); + if ($tee) { + $tee->stop(); + $content = file_get_contents($tee->getFileName(), NULL, NULL, NULL, 5); if (empty($content)) { throw new \CRM_Core_Exception("Failed to capture document content (type=$type)!"); } @@ -458,7 +448,7 @@ class CRM_Contact_Form_Task_PDFLetterCommon { 'name' => $fileName, 'mime_type' => $mimeType, 'options' => array( - 'move-file' => $tmpFile, + 'move-file' => $tee->getFileName(), ), )); } diff --git a/CRM/Utils/ConsoleTee.php b/CRM/Utils/ConsoleTee.php new file mode 100644 index 0000000000..9659ddbeba --- /dev/null +++ b/CRM/Utils/ConsoleTee.php @@ -0,0 +1,118 @@ +start(); + * echo "hello world"; + * $tee->stop(); + * assertEquals("hello world", file_get_contents($tee->getFileName())); + * @endCode + * + * Loosely speaking, it serves a similar purpose to Unix `tee`. + * + * @link https://en.wikipedia.org/wiki/Tee_(command) + */ +class CRM_Utils_ConsoleTee { + + /** + * Capture console output and copy to a temp file. + * + * @param string $prefix + * @return CRM_Utils_ConsoleTee + */ + public static function create($prefix = 'ConsoleTee-') { + return new static(tempnam(sys_get_temp_dir(), $prefix)); + } + + /** + * @var string + */ + protected $fileName; + + /** + * @var resource + */ + protected $fh; + + /** + * CRM_Utils_ConsoleTee constructor. + * + * @param string $fileName + * The full path of the file to write to. + */ + public function __construct($fileName) { + $this->fileName = $fileName; + } + + /** + * Start capturing console output and copying it to a file. + * + * @param string $mode + * The file output mode, e.g. `w` or `w+`. + * @return CRM_Utils_ConsoleTee + * @see fopen + */ + public function start($mode = 'w') { + $this->fh = fopen($this->fileName, $mode); + ob_start(array($this, 'onOutput')); + return $this; + } + + /** + * Process a snippet of data from the output buffer. + * + * @param string $buf + * @return bool + * @see ob_start + */ + public function onOutput($buf) { + fwrite($this->fh, $buf); + return FALSE; + } + + /** + * Stop capturing console output. + * + * @return CRM_Utils_ConsoleTee + */ + public function stop() { + ob_end_flush(); + fclose($this->fh); + return $this; + } + + /** + * @return string + */ + public function getFileName() { + return $this->fileName; + } + +} -- 2.25.1