Finish implementing and testing zip
modifié: core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php nouveau fichier: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/Exception/TarArchiveException.php renommé: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveException.php -> core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/Exception/ZipArchiveException.php nouveau fichier: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php modifié: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php modifié: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderInterface.php nouveau fichier: core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php modifié: core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php
This commit is contained in:
@@ -13,6 +13,8 @@
|
|||||||
namespace Thelia\Core\FileFormat\Archive;
|
namespace Thelia\Core\FileFormat\Archive;
|
||||||
use Thelia\Core\FileFormat\FormatInterface;
|
use Thelia\Core\FileFormat\FormatInterface;
|
||||||
use Thelia\Core\Translation\Translator;
|
use Thelia\Core\Translation\Translator;
|
||||||
|
use Thelia\Exception\FileNotFoundException;
|
||||||
|
use Thelia\Exception\FileNotReadableException;
|
||||||
use Thelia\Log\Tlog;
|
use Thelia\Log\Tlog;
|
||||||
use Thelia\Tools\FileDownload\FileDownloaderAwareTrait;
|
use Thelia\Tools\FileDownload\FileDownloaderAwareTrait;
|
||||||
|
|
||||||
@@ -27,6 +29,9 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
|
|||||||
|
|
||||||
const TEMP_DIRECTORY_NAME = "archive_builder";
|
const TEMP_DIRECTORY_NAME = "archive_builder";
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $cacheFile;
|
||||||
|
|
||||||
/** @var \Thelia\Core\Translation\Translator */
|
/** @var \Thelia\Core\Translation\Translator */
|
||||||
protected $translator;
|
protected $translator;
|
||||||
|
|
||||||
@@ -58,7 +63,7 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$archiveBuilderCacheDir = $this->cache_dir = $theliaCacheDir . static::TEMP_DIRECTORY_NAME;
|
$archiveBuilderCacheDir = $this->cacheDir = $theliaCacheDir . static::TEMP_DIRECTORY_NAME;
|
||||||
|
|
||||||
if (!is_dir($archiveBuilderCacheDir) && !mkdir($archiveBuilderCacheDir, 0755)) {
|
if (!is_dir($archiveBuilderCacheDir) && !mkdir($archiveBuilderCacheDir, 0755)) {
|
||||||
throw new \ErrorException(
|
throw new \ErrorException(
|
||||||
@@ -74,6 +79,97 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
|
|||||||
return $archiveBuilderCacheDir;
|
return $archiveBuilderCacheDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $pathToFile
|
||||||
|
* @param $destination
|
||||||
|
* @param $isOnline
|
||||||
|
* @return $this
|
||||||
|
* @throws \ErrorException
|
||||||
|
*/
|
||||||
|
public function copyFile($pathToFile, $destination, $isOnline)
|
||||||
|
{
|
||||||
|
if ($isOnline) {
|
||||||
|
/**
|
||||||
|
* It's an online file
|
||||||
|
*/
|
||||||
|
$this->getFileDownloader()
|
||||||
|
->download($pathToFile, $destination)
|
||||||
|
;
|
||||||
|
} else {
|
||||||
|
/**
|
||||||
|
* It's a local file
|
||||||
|
*/
|
||||||
|
if (!is_file($pathToFile)) {
|
||||||
|
$this->throwFileNotFound($pathToFile);
|
||||||
|
} elseif (!is_readable($pathToFile)) {
|
||||||
|
throw new FileNotReadableException(
|
||||||
|
$this->translator
|
||||||
|
->trans(
|
||||||
|
"The file %file is not readable",
|
||||||
|
[
|
||||||
|
"%file" => $pathToFile,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!copy($pathToFile, $destination)) {
|
||||||
|
$translatedErrorMessage = $this->translator->trans(
|
||||||
|
"An error happend while copying %prev to %dest",
|
||||||
|
[
|
||||||
|
"%prev" => $pathToFile,
|
||||||
|
"%dest" => $destination,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->logger
|
||||||
|
->error($translatedErrorMessage)
|
||||||
|
;
|
||||||
|
|
||||||
|
throw new \ErrorException($translatedErrorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function generateCacheFile($environment)
|
||||||
|
{
|
||||||
|
$cacheFileName = md5(uniqid());
|
||||||
|
|
||||||
|
$cacheFile = $this->getArchiveBuilderCacheDirectory($environment) . DS;
|
||||||
|
$cacheFile .= $cacheFileName . "." . $this->getExtension();
|
||||||
|
|
||||||
|
return $cacheFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function throwFileNotFound($file)
|
||||||
|
{
|
||||||
|
|
||||||
|
throw new FileNotFoundException(
|
||||||
|
$this->translator
|
||||||
|
->trans(
|
||||||
|
"The file %file is missing or is not readable",
|
||||||
|
[
|
||||||
|
"%file" => $file,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setCacheFile($path)
|
||||||
|
{
|
||||||
|
$this->cacheFile = $path;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getCacheDir()
|
public function getCacheDir()
|
||||||
{
|
{
|
||||||
@@ -95,4 +191,9 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
|
|||||||
{
|
{
|
||||||
return $this->translator;
|
return $this->translator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getCacheFile()
|
||||||
|
{
|
||||||
|
return $this->cacheFile;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* This file is part of the Thelia package. */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : dev@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||||
|
/* file that was distributed with this source code. */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
namespace Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class TarArchiveException
|
||||||
|
* @package Thelia\Core\FileFormat\Archive\ArchiveBuilder
|
||||||
|
* @author Benjamin Perche <bperche@openstudio.fr>
|
||||||
|
*/
|
||||||
|
class TarArchiveException extends \Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
/* file that was distributed with this source code. */
|
/* file that was distributed with this source code. */
|
||||||
/*************************************************************************************/
|
/*************************************************************************************/
|
||||||
|
|
||||||
namespace Thelia\Core\FileFormat\Archive\ArchiveBuilder;
|
namespace Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ZipArchiveException
|
* Class ZipArchiveException
|
||||||
@@ -0,0 +1,479 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* This file is part of the Thelia package. */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : dev@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||||
|
/* file that was distributed with this source code. */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
namespace Thelia\Core\FileFormat\Archive\ArchiveBuilder;
|
||||||
|
use Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder;
|
||||||
|
use Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\TarArchiveException;
|
||||||
|
use Thelia\Core\FileFormat\Archive\ArchiveBuilderInterface;
|
||||||
|
use Thelia\Core\Translation\Translator;
|
||||||
|
use Thelia\Log\Tlog;
|
||||||
|
use Thelia\Tools\FileDownload\FileDownloaderInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class TarArchiveBuilder
|
||||||
|
* @package Thelia\Core\FileFormat\Archive\ArchiveBuilder
|
||||||
|
* @author Benjamin Perche <bperche@openstudio.fr>
|
||||||
|
*/
|
||||||
|
class TarArchiveBuilder extends AbstractArchiveBuilder
|
||||||
|
{
|
||||||
|
const PHAR_FORMAT = \Phar::TAR;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $environment;
|
||||||
|
|
||||||
|
/** @var null|string */
|
||||||
|
protected $compression;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $tarCacheFile;
|
||||||
|
|
||||||
|
/** @var \PharData */
|
||||||
|
protected $tar;
|
||||||
|
|
||||||
|
/** @var \Thelia\Core\Translation\Translator */
|
||||||
|
protected $translator;
|
||||||
|
|
||||||
|
/** @var \Thelia\Log\Tlog */
|
||||||
|
protected $logger;
|
||||||
|
|
||||||
|
function __construct($compressionType = null)
|
||||||
|
{
|
||||||
|
$this->translator = Translator::getInstance();
|
||||||
|
$this->logger = Tlog::getNewInstance();
|
||||||
|
|
||||||
|
$supportedCompression = [
|
||||||
|
"gz",
|
||||||
|
"bz2",
|
||||||
|
null
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!in_array($compressionType, $supportedCompression)) {
|
||||||
|
throw new TarArchiveException(
|
||||||
|
$this->translator->trans(
|
||||||
|
"The compression %type is not supported"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->compression = $compressionType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
if ($this->tar instanceof \PharData) {
|
||||||
|
if (file_exists($this->cacheFile)) {
|
||||||
|
unlink($this->cacheFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $filePath It is the path to access the file.
|
||||||
|
* @param string $directoryInArchive This is the directory where it will be stored in the archive
|
||||||
|
* @param null|string $name The name of the file in the archive. if it null or empty, it keeps the same name
|
||||||
|
* @param bool $isOnline
|
||||||
|
* @return $this
|
||||||
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
|
* @throws \Thelia\Exception\FileNotReadableException
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This methods adds a file in the archive.
|
||||||
|
* If the file is local, $isOnline must be false,
|
||||||
|
* If the file online, $filePath must be an URL.
|
||||||
|
*/
|
||||||
|
public function addFile($filePath, $directoryInArchive = "/", $name = null, $isOnline = false)
|
||||||
|
{
|
||||||
|
if (empty($name) || !is_scalar($name)) {
|
||||||
|
$name = basename($filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download the file if it is online
|
||||||
|
* If it's local check if the file exists and if it is redable
|
||||||
|
*/
|
||||||
|
$fileDownloadCache = $this->cacheDir . DS . "download.tmp";
|
||||||
|
$this->copyFile($filePath, $fileDownloadCache, $isOnline);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Then write the file in the archive
|
||||||
|
*/
|
||||||
|
$directoryInArchive = $this->formatDirectoryPath($directoryInArchive);
|
||||||
|
|
||||||
|
if (!empty($directoryInArchive)) {
|
||||||
|
$name = $this->formatFilePath(
|
||||||
|
$directoryInArchive . $name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->tar->addFile($filePath, $name);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $content
|
||||||
|
* @param $name
|
||||||
|
* @param string $directoryInArchive
|
||||||
|
* @return mixed
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method creates a file in the archive with its content
|
||||||
|
*/
|
||||||
|
public function addFileFromString($content, $name, $directoryInArchive = "/")
|
||||||
|
{
|
||||||
|
if (empty($name) || !is_scalar($name)) {
|
||||||
|
throw new \ErrorException(
|
||||||
|
$this->translator->trans(
|
||||||
|
"The file name must be valid"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$directoryInArchive = $this->formatDirectoryPath($directoryInArchive);
|
||||||
|
|
||||||
|
if (!empty($directoryInArchive)) {
|
||||||
|
$name = $this->formatFilePath(
|
||||||
|
$directoryInArchive . $name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$this->tar->addFromString($name, $content);
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
throw new \ErrorException(
|
||||||
|
$this->translator->trans(
|
||||||
|
"Error while writing the file into the archive, error message: %errmes",
|
||||||
|
[
|
||||||
|
"%errmes" => $e->getMessage()
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $directoryPath
|
||||||
|
* @return $this
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method creates an empty directory
|
||||||
|
*/
|
||||||
|
public function addDirectory($directoryPath)
|
||||||
|
{
|
||||||
|
$directoryInArchive = $this->formatDirectoryPath($directoryPath);
|
||||||
|
|
||||||
|
if (!empty($directoryInArchive)) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->tar->addEmptyDir($directoryInArchive);
|
||||||
|
} catch(\Exception $e) {
|
||||||
|
throw new \ErrorException(
|
||||||
|
$this->translator->trans(
|
||||||
|
"The directory %dir has not been created in the archive",
|
||||||
|
[
|
||||||
|
"%dir" => $directoryInArchive
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $pathToFile
|
||||||
|
* @return null|string
|
||||||
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
|
* @throws \Thelia\Exception\FileNotReadableException
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method returns a file content
|
||||||
|
*/
|
||||||
|
public function getFileContent($pathToFile)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $pathInArchive
|
||||||
|
* @return $this
|
||||||
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method deletes a file in the archive
|
||||||
|
*/
|
||||||
|
public function deleteFile($pathInArchive)
|
||||||
|
{
|
||||||
|
if (!$this->hasFile($pathInArchive)) {
|
||||||
|
$this->throwFileNotFound($pathInArchive);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false === $this->tar->delete($pathInArchive)) {
|
||||||
|
throw new \ErrorException(
|
||||||
|
$this->translator->trans(
|
||||||
|
"Unknown error while deleting the file %file",
|
||||||
|
[
|
||||||
|
"%file" => $pathInArchive
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Thelia\Core\HttpFoundation\Response
|
||||||
|
*
|
||||||
|
* This method return an instance of a Response with the archive as content.
|
||||||
|
*/
|
||||||
|
public function buildArchiveResponse()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $pathToArchive
|
||||||
|
* @param string $environment
|
||||||
|
* @param bool $isOnline
|
||||||
|
* @param FileDownloaderInterface $fileDownloader
|
||||||
|
* @return $this
|
||||||
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
|
* @throws \Thelia\Exception\HttpUrlException
|
||||||
|
* @throws TarArchiveException
|
||||||
|
*
|
||||||
|
* Loads an archive
|
||||||
|
*/
|
||||||
|
public static function loadArchive(
|
||||||
|
$pathToArchive,
|
||||||
|
$environment,
|
||||||
|
$isOnline = false,
|
||||||
|
FileDownloaderInterface $fileDownloader = null
|
||||||
|
) {
|
||||||
|
/** @var TarArchiveBuilder $instance */
|
||||||
|
$instance = new static();
|
||||||
|
|
||||||
|
if ($fileDownloader !== null) {
|
||||||
|
$instance->setFileDownloader($fileDownloader);
|
||||||
|
}
|
||||||
|
|
||||||
|
$instance->setCacheFile($instance->getCacheFile())
|
||||||
|
->copyFile($pathToArchive, $isOnline);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This throws TarArchiveBuilderException if
|
||||||
|
* the archive is not valid.
|
||||||
|
*/
|
||||||
|
$instance->setEnvironment($environment);
|
||||||
|
|
||||||
|
return $instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $pathToFile
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* Checks if the archive has a file.
|
||||||
|
* In \PharData, if you call it as a array,
|
||||||
|
* the keys are the files in the archive.
|
||||||
|
*/
|
||||||
|
public function hasFile($pathToFile)
|
||||||
|
{
|
||||||
|
$isFile = false;
|
||||||
|
|
||||||
|
$pathToFile = $this->formatFilePath($pathToFile);
|
||||||
|
try {
|
||||||
|
/** @var \PharFileInfo $fileInfo */
|
||||||
|
$fileInfo = $this->tar[$pathToFile];
|
||||||
|
|
||||||
|
if($fileInfo->isFile()) {
|
||||||
|
$isFile = true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Catch the exception to avoid its displaying.
|
||||||
|
*/
|
||||||
|
} catch(\BadMethodCallException $e) {}
|
||||||
|
|
||||||
|
return $isFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $directory
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* Check if the archive has a directory
|
||||||
|
*/
|
||||||
|
public function hasDirectory($directory)
|
||||||
|
{
|
||||||
|
$isDir = false;
|
||||||
|
|
||||||
|
$pathToDir = $this->formatDirectoryPath($directory);
|
||||||
|
try {
|
||||||
|
/** @var \PharFileInfo $fileInfo */
|
||||||
|
$fileInfo = $this->tar[$pathToDir];
|
||||||
|
|
||||||
|
if($fileInfo->isDir()) {
|
||||||
|
$isDir = true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Catch the exception to avoid its displaying.
|
||||||
|
*/
|
||||||
|
} catch(\BadMethodCallException $e) {}
|
||||||
|
|
||||||
|
return $isDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $environment
|
||||||
|
* @return $this
|
||||||
|
*
|
||||||
|
* Sets the execution environment of the Kernel,
|
||||||
|
* used to know which cache is used.
|
||||||
|
*/
|
||||||
|
public function setEnvironment($environment)
|
||||||
|
{
|
||||||
|
if ($this->cacheFile === null) {
|
||||||
|
$cacheFile = $this->generateCacheFile($environment);
|
||||||
|
|
||||||
|
if (file_exists($cacheFile)) {
|
||||||
|
unlink($cacheFile);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$cacheFile = $this->cacheFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
$errorMessage = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->tar = new \PharData($cacheFile, null, null, static::PHAR_FORMAT);
|
||||||
|
|
||||||
|
switch ($this->compression) {
|
||||||
|
case "gz":
|
||||||
|
$this->tar = $this->tar->compress(\Phar::GZ);
|
||||||
|
$cacheFile .= ".gz";
|
||||||
|
break;
|
||||||
|
case "bz2":
|
||||||
|
$this->tar = $this->tar->compress(\Phar::BZ2);
|
||||||
|
$cacheFile .= ".bz2";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch(\BadMethodCallException $e) {
|
||||||
|
/**
|
||||||
|
* This should not happen
|
||||||
|
*/
|
||||||
|
$errorMessage = "You have badly called the method setEnvironment twice for %file";
|
||||||
|
} catch(\UnexpectedValueException $e) {
|
||||||
|
$errorMessage = "The file %file is corrupted";
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($errorMessage !== null) {
|
||||||
|
throw new TarArchiveException(
|
||||||
|
$this->translator->trans(
|
||||||
|
$errorMessage,
|
||||||
|
[
|
||||||
|
"%file" => $cacheFile
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->cacheFile = $cacheFile;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $initialString
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* Gives a valid file path for \ZipArchive
|
||||||
|
*/
|
||||||
|
public function formatFilePath($initialString)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Remove the / at the beginning and the end.
|
||||||
|
*/
|
||||||
|
$initialString = trim($initialString, "/");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the double, triple, ... slashes
|
||||||
|
*/
|
||||||
|
$initialString = preg_replace("#\/{2,}#", "/", $initialString);
|
||||||
|
|
||||||
|
return $initialString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $initialString
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* Gives a valid directory path for \ZipArchive
|
||||||
|
*/
|
||||||
|
public function formatDirectoryPath($initialString)
|
||||||
|
{
|
||||||
|
$initialString = $this->formatFilePath($initialString);
|
||||||
|
|
||||||
|
return $initialString . "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* This method must return a string, the name of the format.
|
||||||
|
*
|
||||||
|
* example:
|
||||||
|
* return "XML";
|
||||||
|
*/
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
$name = "tar";
|
||||||
|
|
||||||
|
if ($this->compression !== null) {
|
||||||
|
$name .= "." . $this->compression;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* This method must return a string, the extension of the file format, without the ".".
|
||||||
|
* The string should be lowercase.
|
||||||
|
*
|
||||||
|
* example:
|
||||||
|
* return "xml";
|
||||||
|
*/
|
||||||
|
public function getExtension()
|
||||||
|
{
|
||||||
|
return $this->getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* This method must return a string, the mime type of the file format.
|
||||||
|
*
|
||||||
|
* example:
|
||||||
|
* return "application/json";
|
||||||
|
*/
|
||||||
|
public function getMimeType()
|
||||||
|
{
|
||||||
|
return $this->compression === null ?
|
||||||
|
"application/x-tar" :
|
||||||
|
"application/x-gtar"
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -12,12 +12,10 @@
|
|||||||
|
|
||||||
namespace Thelia\Core\FileFormat\Archive\ArchiveBuilder;
|
namespace Thelia\Core\FileFormat\Archive\ArchiveBuilder;
|
||||||
use Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder;
|
use Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder;
|
||||||
|
use Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\ZipArchiveException;
|
||||||
use Thelia\Core\HttpFoundation\Response;
|
use Thelia\Core\HttpFoundation\Response;
|
||||||
use Thelia\Core\Thelia;
|
use Thelia\Core\Thelia;
|
||||||
use Thelia\Core\Translation\Translator;
|
|
||||||
use Thelia\Exception\FileNotFoundException;
|
|
||||||
use Thelia\Exception\FileNotReadableException;
|
use Thelia\Exception\FileNotReadableException;
|
||||||
use Thelia\Log\Tlog;
|
|
||||||
use Thelia\Tools\FileDownload\FileDownloaderInterface;
|
use Thelia\Tools\FileDownload\FileDownloaderInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,16 +36,6 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
*/
|
*/
|
||||||
protected $zip;
|
protected $zip;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string This is the absolute path to the zip file in cache
|
|
||||||
*/
|
|
||||||
protected $zipCacheFile;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string This is the path of the cache
|
|
||||||
*/
|
|
||||||
protected $cacheDir;
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@@ -64,8 +52,8 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
if ($this->zip instanceof \ZipArchive) {
|
if ($this->zip instanceof \ZipArchive) {
|
||||||
@$this->zip->close();
|
@$this->zip->close();
|
||||||
|
|
||||||
if (file_exists($this->zipCacheFile)) {
|
if (file_exists($this->cacheFile)) {
|
||||||
unlink($this->zipCacheFile);
|
unlink($this->cacheFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,6 +66,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
* @return $this
|
* @return $this
|
||||||
* @throws \Thelia\Exception\FileNotFoundException
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
* @throws \Thelia\Exception\FileNotReadableException
|
* @throws \Thelia\Exception\FileNotReadableException
|
||||||
|
* @throws \ErrorException
|
||||||
*
|
*
|
||||||
* This methods adds a file in the archive.
|
* This methods adds a file in the archive.
|
||||||
* If the file is local, $isOnline must be false,
|
* If the file is local, $isOnline must be false,
|
||||||
@@ -85,71 +74,37 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
*/
|
*/
|
||||||
public function addFile($filePath, $directoryInArchive = null, $name = null, $isOnline = false)
|
public function addFile($filePath, $directoryInArchive = null, $name = null, $isOnline = false)
|
||||||
{
|
{
|
||||||
|
$directoryInArchive = $this->formatDirectoryPath($directoryInArchive);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add empty directory if it doesn't exist
|
* Add empty directory if it doesn't exist
|
||||||
*/
|
*/
|
||||||
if (empty($directoryInArchive) || preg_match("#^\/+$#", $directoryInArchive)) {
|
|
||||||
$directoryInArchive = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!empty($directoryInArchive)) {
|
if(!empty($directoryInArchive)) {
|
||||||
$directoryInArchive = $this->getDirectoryPath($directoryInArchive);
|
$this->addDirectory($directoryInArchive);
|
||||||
|
}
|
||||||
|
|
||||||
if (!$this->zip->addEmptyDir($directoryInArchive)) {
|
if (empty($name) || !is_scalar($name)) {
|
||||||
throw new \ErrorException(
|
$name = basename($filePath);
|
||||||
$this->translator->trans(
|
|
||||||
"The directory %dir has not been created in the archive",
|
|
||||||
[
|
|
||||||
"%dir" => $directoryInArchive
|
|
||||||
]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download the file if it is online
|
* Download the file if it is online
|
||||||
* If it's local check if the file exists and if it is redable
|
* If it's local check if the file exists and if it is redable
|
||||||
*/
|
*/
|
||||||
if ($isOnline) {
|
$fileDownloadCache = $this->cacheDir . DS . "download.tmp";
|
||||||
$fileDownloadCache = $this->cacheDir . DS . "download";
|
$this->copyFile($filePath, $fileDownloadCache, $isOnline);
|
||||||
|
|
||||||
$this->getFileDownloader()
|
|
||||||
->download($filePath, $fileDownloadCache)
|
|
||||||
;
|
|
||||||
|
|
||||||
$filePath = $fileDownloadCache;
|
|
||||||
} else {
|
|
||||||
if (!file_exists($filePath)) {
|
|
||||||
$this->throwFileNotFound($filePath);
|
|
||||||
} else if (!is_readable($filePath)) {
|
|
||||||
throw new FileNotReadableException(
|
|
||||||
$this->translator
|
|
||||||
->trans(
|
|
||||||
"The file %file is not readable",
|
|
||||||
[
|
|
||||||
"%file" => $filePath,
|
|
||||||
]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($name)) {
|
|
||||||
$name = basename($filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Then write the file in the archive and commit the changes
|
* Then write the file in the archive and commit the changes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$destination = $directoryInArchive . $name;
|
$destination = $directoryInArchive . $name;
|
||||||
|
|
||||||
if (!$this->zip->addFile($filePath,$destination)) {
|
if (!$this->zip->addFile($fileDownloadCache, $destination)) {
|
||||||
$translatedErrorMessage = $this->translator->trans(
|
$translatedErrorMessage = $this->translator->trans(
|
||||||
"An error occurred while adding this file to the archive: %file",
|
"An error occurred while adding this file to the archive: %file",
|
||||||
[
|
[
|
||||||
"%file" => $filePath
|
"%file" => $fileDownloadCache
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -163,6 +118,146 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $content
|
||||||
|
* @param $name
|
||||||
|
* @param string $directoryInArchive
|
||||||
|
* @return mixed
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method creates a file in the archive with its content
|
||||||
|
*/
|
||||||
|
public function addFileFromString($content, $name, $directoryInArchive = "/")
|
||||||
|
{
|
||||||
|
$directoryInArchive = $this->formatDirectoryPath($directoryInArchive);
|
||||||
|
|
||||||
|
if (!empty($directoryInArchive) && $directoryInArchive !== "/") {
|
||||||
|
$this->addDirectory($directoryInArchive);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($name) || !is_scalar($name)) {
|
||||||
|
throw new \ErrorException(
|
||||||
|
$this->translator->trans(
|
||||||
|
"The filename is not correct"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$filePath = $this->getFilePath($directoryInArchive . DS . $name);
|
||||||
|
|
||||||
|
if (!$this->zip->addFromString($filePath, $content)) {
|
||||||
|
throw new \ErrorException(
|
||||||
|
$this->translator->trans(
|
||||||
|
"Unable to write the file %file into the archive",
|
||||||
|
[
|
||||||
|
"%file" => $filePath,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $directoryPath
|
||||||
|
* @return $this
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method creates an empty directory
|
||||||
|
*/
|
||||||
|
public function addDirectory($directoryPath)
|
||||||
|
{
|
||||||
|
$directoryInArchive = $this->formatDirectoryPath($directoryPath);
|
||||||
|
|
||||||
|
if (!empty($directoryInArchive)) {
|
||||||
|
if (!$this->zip->addEmptyDir($directoryInArchive)) {
|
||||||
|
throw new \ErrorException(
|
||||||
|
$this->translator->trans(
|
||||||
|
"The directory %dir has not been created in the archive",
|
||||||
|
[
|
||||||
|
"%dir" => $directoryInArchive
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $pathToFile
|
||||||
|
* @return null|string
|
||||||
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
|
* @throws \Thelia\Exception\FileNotReadableException
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method returns a file content
|
||||||
|
*/
|
||||||
|
public function getFileContent($pathToFile)
|
||||||
|
{
|
||||||
|
$pathToFile = $this->formatFilePath($pathToFile);
|
||||||
|
|
||||||
|
if (!$this->hasFile($pathToFile)) {
|
||||||
|
$this->throwFileNotFound($pathToFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
$stream = $this->zip->getStream($pathToFile);
|
||||||
|
$content = "";
|
||||||
|
|
||||||
|
while (!feof($stream)) {
|
||||||
|
$content .= fread($stream, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($stream);
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $initialString
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* Gives a valid file path for \ZipArchive
|
||||||
|
*/
|
||||||
|
public function getFilePath($initialString)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Remove the / at the beginning and the end.
|
||||||
|
*/
|
||||||
|
$initialString = trim($initialString, "/");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the double, triple, ... slashes
|
||||||
|
*/
|
||||||
|
$initialString = preg_replace("#\/{2,}#", "/", $initialString);
|
||||||
|
|
||||||
|
if (preg_match("#\/?[^\/]+\/[^/]+\/?#", $initialString)) {
|
||||||
|
$initialString = "/" . $initialString;
|
||||||
|
}
|
||||||
|
return $initialString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $initialString
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* Gives a valid directory path for \ZipArchive
|
||||||
|
*/
|
||||||
|
public function getDirectoryPath($initialString)
|
||||||
|
{
|
||||||
|
$initialString = $this->getFilePath($initialString);
|
||||||
|
|
||||||
|
if ($initialString[0] !== "/") {
|
||||||
|
$initialString = "/" . $initialString;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $initialString . "/";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $pathInArchive
|
* @param $pathInArchive
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -173,7 +268,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
*/
|
*/
|
||||||
public function deleteFile($pathInArchive)
|
public function deleteFile($pathInArchive)
|
||||||
{
|
{
|
||||||
$pathInArchive = $this->getFilePath($pathInArchive);
|
$pathInArchive = $this->formatFilePath($pathInArchive);
|
||||||
|
|
||||||
if (!$this->hasFile($pathInArchive)) {
|
if (!$this->hasFile($pathInArchive)) {
|
||||||
$this->throwFileNotFound($pathInArchive);
|
$this->throwFileNotFound($pathInArchive);
|
||||||
@@ -206,22 +301,22 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
|
|
||||||
$this->commit();
|
$this->commit();
|
||||||
|
|
||||||
if (!file_exists($this->zipCacheFile)) {
|
if (!file_exists($this->cacheFile)) {
|
||||||
$this->throwFileNotFound($this->zipCacheFile);
|
$this->throwFileNotFound($this->cacheFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_readable($this->zipCacheFile)) {
|
if (!is_readable($this->cacheFile)) {
|
||||||
throw new FileNotReadableException(
|
throw new FileNotReadableException(
|
||||||
$this->translator->trans(
|
$this->translator->trans(
|
||||||
"The cache file %file is not readable",
|
"The cache file %file is not readable",
|
||||||
[
|
[
|
||||||
"%file" => $this->zipCacheFile
|
"%file" => $this->cacheFile
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$content = file_get_contents($this->zipCacheFile);
|
$content = file_get_contents($this->cacheFile);
|
||||||
|
|
||||||
$this->zip->close();
|
$this->zip->close();
|
||||||
|
|
||||||
@@ -262,39 +357,9 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
$instance->setFileDownloader($fileDownloader);
|
$instance->setFileDownloader($fileDownloader);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($isOnline) {
|
$instance->copyFile($pathToArchive, $instance->getCacheFile(), $isOnline);
|
||||||
/**
|
|
||||||
* It's an online file
|
|
||||||
*/
|
|
||||||
$instance->getFileDownloader()
|
|
||||||
->download($pathToArchive, $instance->getZipCacheFile())
|
|
||||||
;
|
|
||||||
} else {
|
|
||||||
/**
|
|
||||||
* It's a local file
|
|
||||||
*/
|
|
||||||
if (!is_file($pathToArchive) || !is_readable($pathToArchive)) {
|
|
||||||
$instance->throwFileNotFound($pathToArchive);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!copy($pathToArchive, $instance->getZipCacheFile())) {
|
if (true !== $return = $zip->open($instance->getCacheFile())) {
|
||||||
$translatedErrorMessage = $instance->getTranslator()->trans(
|
|
||||||
"An unknown error happend while copying %prev to %dest",
|
|
||||||
[
|
|
||||||
"%prev" => $pathToArchive,
|
|
||||||
"%dest" => $instance->getZipCacheFile(),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
$instance->getLogger()
|
|
||||||
->error($translatedErrorMessage)
|
|
||||||
;
|
|
||||||
|
|
||||||
throw new \ErrorException($translatedErrorMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (true !== $return = $zip->open($instance->getZipCacheFile())) {
|
|
||||||
throw new ZipArchiveException(
|
throw new ZipArchiveException(
|
||||||
$instance->getZipErrorMessage($return)
|
$instance->getZipErrorMessage($return)
|
||||||
);
|
);
|
||||||
@@ -312,7 +377,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
public function hasFile($pathToFile)
|
public function hasFile($pathToFile)
|
||||||
{
|
{
|
||||||
return $this->zip
|
return $this->zip
|
||||||
->locateName($this->getFilePath($pathToFile)) !== false
|
->locateName($this->formatFilePath($pathToFile)) !== false
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,7 +389,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
*/
|
*/
|
||||||
public function hasDirectory($directory)
|
public function hasDirectory($directory)
|
||||||
{
|
{
|
||||||
$link = $this->zip->locateName($this->getDirectoryPath($directory));
|
$link = $this->zip->locateName($this->formatDirectoryPath($directory));
|
||||||
|
|
||||||
return $link !== false;
|
return $link !== false;
|
||||||
}
|
}
|
||||||
@@ -339,10 +404,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
public function setEnvironment($environment)
|
public function setEnvironment($environment)
|
||||||
{
|
{
|
||||||
|
|
||||||
$cacheFileName = md5 (uniqid());
|
$cacheFile = $this->generateCacheFile($environment);
|
||||||
|
|
||||||
$cacheFile = $this->getArchiveBuilderCacheDirectory($environment) . DS;
|
|
||||||
$cacheFile .= $cacheFileName . "." . $this->getExtension();
|
|
||||||
|
|
||||||
if (file_exists($cacheFile)) {
|
if (file_exists($cacheFile)) {
|
||||||
unlink($cacheFile);
|
unlink($cacheFile);
|
||||||
@@ -361,7 +423,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->zipCacheFile = $cacheFile;
|
$this->cacheFile = $cacheFile;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -433,7 +495,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
public function commit()
|
public function commit()
|
||||||
{
|
{
|
||||||
$this->zip->close();
|
$this->zip->close();
|
||||||
$result = $this->zip->open($this->getZipCacheFile());
|
$result = $this->zip->open($this->getCacheFile());
|
||||||
|
|
||||||
if ($result !== true) {
|
if ($result !== true) {
|
||||||
throw new \ErrorException(
|
throw new \ErrorException(
|
||||||
@@ -452,7 +514,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
*
|
*
|
||||||
* Gives a valid file path for \ZipArchive
|
* Gives a valid file path for \ZipArchive
|
||||||
*/
|
*/
|
||||||
public function getFilePath($initialString)
|
public function formatFilePath($initialString)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Remove the / at the beginning and the end.
|
* Remove the / at the beginning and the end.
|
||||||
@@ -476,31 +538,17 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
*
|
*
|
||||||
* Gives a valid directory path for \ZipArchive
|
* Gives a valid directory path for \ZipArchive
|
||||||
*/
|
*/
|
||||||
public function getDirectoryPath($initialString)
|
public function formatDirectoryPath($initialString)
|
||||||
{
|
{
|
||||||
$initialString = $this->getFilePath($initialString);
|
$initialString = $this->formatFilePath($initialString);
|
||||||
|
|
||||||
if ($initialString[0] !== "/") {
|
if ($initialString !== "" && $initialString[0] !== "/") {
|
||||||
$initialString = "/" . $initialString;
|
$initialString = "/" . $initialString;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $initialString . "/";
|
return $initialString . "/";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function throwFileNotFound($file)
|
|
||||||
{
|
|
||||||
|
|
||||||
throw new FileNotFoundException(
|
|
||||||
$this->getTranslator()
|
|
||||||
->trans(
|
|
||||||
"The file %file is missing or is not readable",
|
|
||||||
[
|
|
||||||
"%file" => $file,
|
|
||||||
]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*
|
*
|
||||||
@@ -548,10 +596,4 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
|
|||||||
{
|
{
|
||||||
return $this->zip;
|
return $this->zip;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getZipCacheFile()
|
|
||||||
{
|
|
||||||
return $this->zipCacheFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
/*************************************************************************************/
|
/*************************************************************************************/
|
||||||
|
|
||||||
namespace Thelia\Core\FileFormat\Archive;
|
namespace Thelia\Core\FileFormat\Archive;
|
||||||
|
use Thelia\Tools\FileDownload\FileDownloaderInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface ArchiveBuilderInterface
|
* Interface ArchiveBuilderInterface
|
||||||
@@ -29,6 +30,7 @@ interface ArchiveBuilderInterface
|
|||||||
* @return $this
|
* @return $this
|
||||||
* @throws \Thelia\Exception\FileNotFoundException
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
* @throws \Thelia\Exception\FileNotReadableException
|
* @throws \Thelia\Exception\FileNotReadableException
|
||||||
|
* @throws \ErrorException
|
||||||
*
|
*
|
||||||
* This methods adds a file in the archive.
|
* This methods adds a file in the archive.
|
||||||
* If the file is local, $isOnline must be false,
|
* If the file is local, $isOnline must be false,
|
||||||
@@ -36,15 +38,45 @@ interface ArchiveBuilderInterface
|
|||||||
*/
|
*/
|
||||||
public function addFile($filePath, $directoryInArchive = "/", $name = null, $isOnline = false);
|
public function addFile($filePath, $directoryInArchive = "/", $name = null, $isOnline = false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $content
|
||||||
|
* @param $name
|
||||||
|
* @param string $directoryInArchive
|
||||||
|
* @return mixed
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method creates a file in the archive with its content
|
||||||
|
*/
|
||||||
|
public function addFileFromString($content, $name, $directoryInArchive = "/");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $pathToFile
|
||||||
|
* @return null|string
|
||||||
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
|
* @throws \Thelia\Exception\FileNotReadableException
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method returns a file content
|
||||||
|
*/
|
||||||
|
public function getFileContent($pathToFile);
|
||||||
/**
|
/**
|
||||||
* @param $pathInArchive
|
* @param $pathInArchive
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws \Thelia\Exception\FileNotFoundException
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
|
* @throws \ErrorException
|
||||||
*
|
*
|
||||||
* This method deletes a file in the archive
|
* This method deletes a file in the archive
|
||||||
*/
|
*/
|
||||||
public function deleteFile($pathInArchive);
|
public function deleteFile($pathInArchive);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $directoryPath
|
||||||
|
* @return $this
|
||||||
|
* @throws \ErrorException
|
||||||
|
*
|
||||||
|
* This method creates an empty directory
|
||||||
|
*/
|
||||||
|
public function addDirectory($directoryPath);
|
||||||
/**
|
/**
|
||||||
* @return \Thelia\Core\HttpFoundation\Response
|
* @return \Thelia\Core\HttpFoundation\Response
|
||||||
*
|
*
|
||||||
@@ -54,14 +86,16 @@ interface ArchiveBuilderInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $pathToArchive
|
* @param string $pathToArchive
|
||||||
|
* @param string $environment
|
||||||
* @param bool $isOnline
|
* @param bool $isOnline
|
||||||
|
* @param FileDownloaderInterface $fileDownloader
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws \Thelia\Exception\FileNotFoundException
|
* @throws \Thelia\Exception\FileNotFoundException
|
||||||
* @throws \Thelia\Exception\HttpUrlException
|
* @throws \Thelia\Exception\HttpUrlException
|
||||||
*
|
*
|
||||||
* Loads an archive
|
* Loads an archive
|
||||||
*/
|
*/
|
||||||
public static function loadArchive($pathToArchive, $environment, $isOnline = false);
|
public static function loadArchive($pathToArchive, $environment, $isOnline = false, FileDownloaderInterface $fileDownloader = null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $pathToFile
|
* @param $pathToFile
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* This file is part of the Thelia package. */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : dev@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||||
|
/* file that was distributed with this source code. */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
namespace Thelia\Tests\FileFormat\Archive\ArchiveBuilder;
|
||||||
|
use Symfony\Component\DependencyInjection\Container;
|
||||||
|
use Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarArchiveBuilder;
|
||||||
|
use Thelia\Core\Translation\Translator;
|
||||||
|
use Thelia\Log\Tlog;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class TarArchiveBuilderTest
|
||||||
|
* @package Thelia\Tests\FileFormat\Archive\ArchiveBuilder
|
||||||
|
* @author Benjamin Perche <bperche@openstudio.fr>
|
||||||
|
*/
|
||||||
|
class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/** @var TarArchiveBuilder */
|
||||||
|
protected $tar;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
new Translator(new Container());
|
||||||
|
|
||||||
|
Tlog::getNewInstance();
|
||||||
|
|
||||||
|
$this->tar = new TarArchiveBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddFileAndDirectory()
|
||||||
|
{
|
||||||
|
$this->tar->setEnvironment("dev");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File
|
||||||
|
*/
|
||||||
|
$tar = $this->tar->addFile(
|
||||||
|
__DIR__ . DS . "TestResources/test_file"
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($tar->hasFile("test_file"));
|
||||||
|
|
||||||
|
$this->assertFalse($tar->hasDirectory("test_file"));
|
||||||
|
|
||||||
|
$tar = $this->tar->addFile(
|
||||||
|
__DIR__ . DS . "TestResources/test_file",
|
||||||
|
null,
|
||||||
|
"TEST.txt"
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($tar->hasFile("TEST.txt"));
|
||||||
|
|
||||||
|
$this->assertFalse($tar->hasDirectory("TEST.txt"));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Directory
|
||||||
|
*/
|
||||||
|
$this->tar->addDirectory("foo");
|
||||||
|
|
||||||
|
$this->assertTrue($tar->hasDirectory("foo"));
|
||||||
|
|
||||||
|
$this->assertFalse($tar->hasFile("foo"));
|
||||||
|
|
||||||
|
/**s
|
||||||
|
* File in a directory
|
||||||
|
*/
|
||||||
|
$this->tar->addFile(
|
||||||
|
__DIR__ . DS . "TestResources/test_file",
|
||||||
|
"bar",
|
||||||
|
"baz"
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue($this->tar->hasFile("bar/baz"));
|
||||||
|
|
||||||
|
$this->assertTrue($this->tar->hasDirectory("bar"));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -53,52 +53,52 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"foo",
|
"foo",
|
||||||
$this->zip->getFilePath("foo")
|
$this->zip->formatFilePath("foo")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"foo",
|
"foo",
|
||||||
$this->zip->getFilePath("/foo")
|
$this->zip->formatFilePath("/foo")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"foo",
|
"foo",
|
||||||
$this->zip->getFilePath("foo/")
|
$this->zip->formatFilePath("foo/")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"foo",
|
"foo",
|
||||||
$this->zip->getFilePath("/foo/")
|
$this->zip->formatFilePath("/foo/")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar",
|
"/foo/bar",
|
||||||
$this->zip->getFilePath("foo/bar")
|
$this->zip->formatFilePath("foo/bar")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar",
|
"/foo/bar",
|
||||||
$this->zip->getFilePath("/foo/bar")
|
$this->zip->formatFilePath("/foo/bar")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar",
|
"/foo/bar",
|
||||||
$this->zip->getFilePath("/foo//bar/")
|
$this->zip->formatFilePath("/foo//bar/")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar",
|
"/foo/bar",
|
||||||
$this->zip->getFilePath("/foo/bar/")
|
$this->zip->formatFilePath("/foo/bar/")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar/baz",
|
"/foo/bar/baz",
|
||||||
$this->zip->getFilePath("foo/bar/baz")
|
$this->zip->formatFilePath("foo/bar/baz")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar/baz",
|
"/foo/bar/baz",
|
||||||
$this->zip->getFilePath("//foo/bar///baz/")
|
$this->zip->formatFilePath("//foo/bar///baz/")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,52 +106,52 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/",
|
"/foo/",
|
||||||
$this->zip->getDirectoryPath("foo")
|
$this->zip->formatDirectoryPath("foo")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/",
|
"/foo/",
|
||||||
$this->zip->getDirectoryPath("/foo")
|
$this->zip->formatDirectoryPath("/foo")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/",
|
"/foo/",
|
||||||
$this->zip->getDirectoryPath("foo/")
|
$this->zip->formatDirectoryPath("foo/")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/",
|
"/foo/",
|
||||||
$this->zip->getDirectoryPath("/foo/")
|
$this->zip->formatDirectoryPath("/foo/")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar/",
|
"/foo/bar/",
|
||||||
$this->zip->getDirectoryPath("foo/bar")
|
$this->zip->formatDirectoryPath("foo/bar")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar/",
|
"/foo/bar/",
|
||||||
$this->zip->getDirectoryPath("/foo/bar")
|
$this->zip->formatDirectoryPath("/foo/bar")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar/",
|
"/foo/bar/",
|
||||||
$this->zip->getDirectoryPath("/foo//bar/")
|
$this->zip->formatDirectoryPath("/foo//bar/")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar/",
|
"/foo/bar/",
|
||||||
$this->zip->getDirectoryPath("/foo/bar/")
|
$this->zip->formatDirectoryPath("/foo/bar/")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar/baz/",
|
"/foo/bar/baz/",
|
||||||
$this->zip->getDirectoryPath("foo/bar/baz")
|
$this->zip->formatDirectoryPath("foo/bar/baz")
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"/foo/bar/baz/",
|
"/foo/bar/baz/",
|
||||||
$this->zip->getDirectoryPath("//foo/bar///baz/")
|
$this->zip->formatDirectoryPath("//foo/bar///baz/")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveException
|
* @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\ZipArchiveException
|
||||||
* @expectedExceptionMessage [Zip Error] The file is not a zip archive
|
* @expectedExceptionMessage [Zip Error] The file is not a zip archive
|
||||||
*/
|
*/
|
||||||
public function testLoadNotValidZip()
|
public function testLoadNotValidZip()
|
||||||
@@ -202,7 +202,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveException
|
* @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\ZipArchiveException
|
||||||
* @expectedExceptionMessage [Zip Error] The file is not a zip archive
|
* @expectedExceptionMessage [Zip Error] The file is not a zip archive
|
||||||
*/
|
*/
|
||||||
public function testLoadOnlineAvailableAndNotValidFile()
|
public function testLoadOnlineAvailableAndNotValidFile()
|
||||||
@@ -339,4 +339,49 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$loadedArchiveResponseContent
|
$loadedArchiveResponseContent
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAddValidFileFromString()
|
||||||
|
{
|
||||||
|
$this->loadedZip->addFileFromString(
|
||||||
|
"foo", "bar"
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertTrue(
|
||||||
|
$this->loadedZip->hasFile("bar")
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
"foo",
|
||||||
|
$this->loadedZip->getFileContent("bar")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Thelia\Exception\FileNotFoundException
|
||||||
|
*/
|
||||||
|
public function testGetFileContentFileNotFound()
|
||||||
|
{
|
||||||
|
$this->loadedZip->getFileContent("bar");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \ErrorException
|
||||||
|
*/
|
||||||
|
public function testAddNotValidFileFromString()
|
||||||
|
{
|
||||||
|
$this->loadedZip->addFileFromString(
|
||||||
|
"foo", $this
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \ErrorException
|
||||||
|
*/
|
||||||
|
public function testAddNotValidFileValueFromString()
|
||||||
|
{
|
||||||
|
$this->loadedZip->addFileFromString(
|
||||||
|
$this, "bar"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user