/** * @param array $contents - ['san.txt' => '@san.txt', 'string.txt' => 'anything can be here'] * @param string $zipFile * * @return string */ public function create(array $contents = [], string $zipFile = '') { $zip = new ZipArchive(); $file = sprintf('%s/%s.zip', $this->tmpDir->getTempDir('zip'), $this->utils->filename($zipFile) ?: Str::random(6)); $res = $zip->open($file, ZipArchive::CREATE | ZipArchive::OVERWRITE); foreach ($contents as $filename => $data) { if (strpos($data, '@') === 0) { $zip->addFile(substr($data, 1), $filename); } else { $zip->addFromString($filename, preg_replace("/\r\n/", "\n", $data)); } } $zip->close(); return filesize($file) > 0 ? $file : false; }
protected function startQueryLog() { $this->logFile = $this->tmpDir->getTempDir('logs') . '/query.log'; @unlink($this->logFile); $this->connection->enableQueryLog(); $this->logger->pushHandler(new StreamHandler($this->logFile, LoggerEx::INFO)); $this->dispatcher->listen(QueryExecuted::class, function (QueryExecuted $query) { $sql = preg_replace_callback('/\\?/', function () use($query, &$index) { return sprintf("'%s'", $query->bindings[$index++ ?? 0]); }, $query->sql); $this->logger->info($sql); }); }
public function downloadCached(string $url) { $path = sprintf('%s/%s.%s', $this->tmpDir->getTempDir('downloads'), md5($url), $this->utils->extension($url)); return file_exists($path) ? $path : $this->download($url, $path); }