_filePath = $this->_parseFilePath($config); $this->_delimiter = isset($config['delimiter']) ? $config['delimiter'] : ','; $this->_enclosure = isset($config['enclosure']) ? $config['enclosure'] : '"'; } /** * Parses file path * * @param array|null $config * @return string */ protected function _parseFilePath(array $config = null) { $result = isset($config['filePath']) ? $config['filePath'] : self::DEFAULT_FILEPATH; if (isset($config['baseDir'])) { $result = rtrim($config['baseDir'], '/') . '/' . ltrim($result, '/'); } return $result; } /** * Write profiling results to CSV-file * * @param Stat $stat * @return void * @throws \RuntimeException if output file cannot be opened */ public function display(Stat $stat) { $fileHandle = fopen($this->_filePath, 'w'); if (!$fileHandle) { throw new \RuntimeException(sprintf('Can not open a file "%s".', $this->_filePath)); } $lockRequired = strpos($this->_filePath, 'php://') !== 0; $isLocked = false; while ($lockRequired && !$isLocked) { $isLocked = flock($fileHandle, LOCK_EX); } $this->_writeFileContent($fileHandle, $stat); if ($isLocked) { flock($fileHandle, LOCK_UN); } fclose($fileHandle); } /** * Write content into an opened file handle * * @param resource $fileHandle * @param Stat $stat * @return void */ protected function _writeFileContent($fileHandle, Stat $stat) { foreach ($this->_getTimerIds($stat) as $timerName) { $row = []; foreach ($this->_columns as $column) { $row[] = $this->_renderColumnValue($stat->fetch($timerName, $column), $column); } fputcsv($fileHandle, $row, $this->_delimiter, $this->_enclosure); } } }