diff --git a/Utils/IO/Csv/CsvDatabaseMapper.php b/Utils/IO/Csv/CsvDatabaseMapper.php deleted file mode 100644 index 26489fe94..000000000 --- a/Utils/IO/Csv/CsvDatabaseMapper.php +++ /dev/null @@ -1,90 +0,0 @@ -db = $db; - } - - public function addSource(string $source) - { - $this->sources[] = $source; - - $this->sources = array_unique($this->sources); - } - - public function setSources(array $sources) : void - { - $this->sources = $sources; - } - - public function autoIdentifyCsvSettings(bool $identify) - { - $this->autoIdentifyCsvSettings = $identify; - } - - public function setLineBuffer(int $buffer) : void - { - $this->lineBuffer = $buffer; - } - - public function insert() - { - foreach ($this->sources as $source) { - $file = fopen($source, 'r'); - $header = []; - $delimiter = $this->autoIdentifyCsvSettings ? $this->getFileDelimiter($file, 100) : $this->delimiter; - - if (feof($file) && ($line = fgetcsv($file, 0, $delimiter)) !== false) { - $header = $line; - } - - $query = new Builder($this->db); - $query->insert(...$header)->into(\str_replace(' ', '', explode($source, '.'))); - - while (feof($file)) { - $c = 0; - - while (($line = fgetcsv($file)) !== false && $c < $this->lineBuffer && feof($file)) { - $query->values($line); - $c++; - } - - $this->db->con->prepare($query->toSql())->execute(); - } - - fclose($file); - } - } -} diff --git a/Utils/IO/Csv/CsvSettings.php b/Utils/IO/Csv/CsvSettings.php index 0523273e4..f001235a9 100644 --- a/Utils/IO/Csv/CsvSettings.php +++ b/Utils/IO/Csv/CsvSettings.php @@ -25,9 +25,9 @@ class CsvSettings /** * Get csv file delimiter. * - * @param mixed $file File resource - * @param int $checkLines Lines to check for evaluation - * @param array $delimiters Potential delimiters + * @param mixed $file File resource + * @param int $checkLines Lines to check for evaluation + * @param string[] $delimiters Potential delimiters * * @return string * @@ -36,14 +36,23 @@ class CsvSettings public static function getFileDelimiter($file, int $checkLines = 2, array $delimiters = [',', '\t', ';', '|', ':']) : string { $results = []; + $i = 0; + $line = \fgets($file); - $i = 0; - while (($line = fgets($file)) !== false && $i < $checkLines) { + if ($line === false) { + return ';'; + } + + while ($line !== false && $i < $checkLines) { $i++; foreach ($delimiters as $delimiter) { $regExp = '/[' . $delimiter . ']/'; - $fields = preg_split($regExp, $line); + $fields = \preg_split($regExp, $line); + + if ($fields === false) { + return ';'; + } if (count($fields) > 1) { if (!empty($results[$delimiter])) { @@ -55,7 +64,7 @@ class CsvSettings } } - $results = array_keys($results, max($results)); + $results = \array_keys($results, max($results)); return $results[0]; } diff --git a/Utils/IO/Zip/Tar.php b/Utils/IO/Zip/Tar.php index 498494215..71695911e 100644 --- a/Utils/IO/Zip/Tar.php +++ b/Utils/IO/Zip/Tar.php @@ -14,6 +14,8 @@ declare(strict_types=1); namespace phpOMS\Utils\IO\Zip; +use phpOMS\System\File\FileUtils; + /** * Zip class for handling zip files. * @@ -31,48 +33,57 @@ class Tar implements ArchiveInterface */ public static function pack($sources, string $destination, bool $overwrite = true) : bool { - $destination = \str_replace('\\', '/', realpath($destination)); + $destination = FileUtils::absolute(\str_replace('\\', '/', $destination)); if (!$overwrite && \file_exists($destination)) { return false; } + $tar = new \PharData($destination); + /** @var array $sources */ - foreach ($sources as $source) { - $source = \str_replace('\\', '/', realpath($source)); + foreach ($sources as $source => $relative) { + $source = \realpath($source); + + if ($source === false) { + continue; + } + + $source = \str_replace('\\', '/', $source); if (!\file_exists($source)) { continue; } - if (is_dir($source)) { - $files = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($source), - \RecursiveIteratorIterator::SELF_FIRST - ); + if (\is_dir($source)) { + $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { $file = \str_replace('\\', '/', $file); /* Ignore . and .. */ - if (\in_array(mb_substr($file, mb_strrpos($file, '/') + 1), ['.', '..'])) { + if (($pos = \mb_strrpos($file, '/')) === false + || \in_array(\mb_substr($file, $pos + 1), ['.', '..']) + ) { continue; } - $file = realpath($file); + $absolute = \realpath($file); + $absolute = \str_replace('\\', '/', (string) $absolute); + $dir = \str_replace($source . '/', '', $relative . '/' . $absolute); - if (is_dir($file)) { - // todo: do work here - } elseif (is_file($file)) { - // todo: do work here + if (\is_dir($absolute)) { + $tar->addEmptyDir($dir . '/'); + } elseif (\is_file($absolute)) { + $tar->addFile($absolute, $dir); } } - } elseif (is_file($source)) { - // todo: do work here + } elseif (\is_file($source)) { + $tar->addFile($source, $relative); } } - fwrite($tar, pack('a1024', '')); + return true; } /** @@ -80,6 +91,16 @@ class Tar implements ArchiveInterface */ public static function unpack(string $source, string $destination) : bool { + if (!\file_exists($source)) { + return false; + } + $destination = \str_replace('\\', '/', $destination); + $destination = \rtrim($destination, '/'); + $tar = new \PharData($destination); + + $tar->extractTo($destination . '/'); + + return true; } } diff --git a/Utils/IO/Zip/Zip.php b/Utils/IO/Zip/Zip.php index 02b24b3c8..a330e48cb 100644 --- a/Utils/IO/Zip/Zip.php +++ b/Utils/IO/Zip/Zip.php @@ -100,7 +100,7 @@ class Zip implements ArchiveInterface } $destination = \str_replace('\\', '/', $destination); - $destination = rtrim($destination, '/'); + $destination = \rtrim($destination, '/'); $zip = new \ZipArchive(); if (!$zip->open($source)) { diff --git a/tests/Utils/IO/Csv/CsvDatabaseMapperTest.php b/tests/Utils/IO/Csv/CsvDatabaseMapperTest.php deleted file mode 100644 index 48a9fedab..000000000 --- a/tests/Utils/IO/Csv/CsvDatabaseMapperTest.php +++ /dev/null @@ -1,24 +0,0 @@ -