/** * Compiles the history-file. This is to be done when the writing to the history-file is complete. * This essentialy puts the file in a closed state, gzipping it while also optionally adding extra data. * @param string|SplFileObject $historyInput Either a string which is a path to a file that is to be compiled, * or a SplFileObject pointing to that file. This file will be deleted by this function, leaving you only * with its compiled state. * @param string|null $historyOutput A filepath-string to the file to be created. This may be omitted in which case * the output-file will be generated in the same directory as the input-file, with the same name but with * ".gz" added at the end. * @param mixed $customData Anything that is json-friendly can be passed here. It will be assigned to the root of * the final, compiled json-object with the same name("customData") * @param bool $keepInputFile If this is set to true then the uncompiled input-file will not be deleted. An example * of a useful case for this would be if daily history-files are generated, and one would like to view the * history so far of today. For this to work $historyOutput needs to be set. * @return bool Returns true for success*/ public static function compileHistoryStatic($historyInput, $historyOutput = null, $customData = null, $keepInputFile = false) { //At this point the history should be formatted as: //{"pages":[page1{},page2{}+tempData+tempDataSize //i.e. //{"pages":[{"exchanges":[{"content":"foobar","headers":{"http_code":200}}]}{"numPages":1,"idIndices":[]}29 //(the outer bracket&brace aren't closed) if ($keepInputFile) { //can't use tmpfile() or SplTempFileObject because we can't get a path from those //to feed to exec in compressHistoryFile() so tempnam() is used instead. $tempFile = tempnam(dirname($historyInput), 'hicurlHistory'); copy($historyInput, $tempFile); $historyInput = $tempFile; } $historyInput = new SplFileObject($historyInput, 'r+'); Hicurl::seekHistoryFileTempData($historyInput); $historyInput->fwrite('],"customData":' . json_encode($customData) . '}'); $historyInput->ftruncate($historyInput->ftell()); $historyInputPath = $historyInput->getRealPath(); $historyInput = null; //remove the reference to the file so that gzip can delete it as supposed to Hicurl::compressHistoryFile($historyInputPath, $historyOutput); }