Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources

modifié:         core/lib/Thelia/Config/Resources/config.xml
	modifié:         core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php
	modifié:         core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php
	modifié:         core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.zip
	modifié:         core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.zip
	modifié:         core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php
This commit is contained in:
Benjamin Perche
2014-07-02 15:14:52 +02:00
parent ce3089e106
commit 50fb4b0dd8
6 changed files with 119 additions and 79 deletions

View File

@@ -142,10 +142,28 @@
<argument>%kernel.environment%</argument>
</service>
<!-- zip -->
<service id="thelia.manager.zip_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder">
<tag name="thelia.manager.archive_builder" />
</service>
<!-- tar -->
<service id="thelia.manager.tar_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder">
<tag name="thelia.manager.archive_builder" />
</service>
<!-- tar.gz -->
<service id="thelia.manager.tar_gz_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder">
<argument id="compressionType" type="string">gz</argument>
<tag name="thelia.manager.archive_builder" />
</service>
<!-- tar.bz2 -->
<service id="thelia.manager.tar_bz2_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder">
<argument id="compressionType" type="string">bz2</argument>
<tag name="thelia.manager.archive_builder" />
</service>
<service id="thelia.manager.formatter_manager" class="Thelia\Core\FileFormat\Formatter\FormatterManager" />
</services>

View File

@@ -12,6 +12,8 @@
namespace Thelia\Core\FileFormat\Archive;
use Thelia\Core\FileFormat\FormatInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Log\Tlog;
use Thelia\Tools\FileDownload\FileDownloaderAwareTrait;
/**
@@ -22,4 +24,75 @@ use Thelia\Tools\FileDownload\FileDownloaderAwareTrait;
abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilderInterface
{
use FileDownloaderAwareTrait;
const TEMP_DIRECTORY_NAME = "archive_builder";
/** @var \Thelia\Core\Translation\Translator */
protected $translator;
/** @var \Thelia\Log\Tlog */
protected $logger;
/** @var string */
protected $cacheDir;
public function __construct()
{
$this->translator = Translator::getInstance();
$this->logger = Tlog::getNewInstance();
}
public function getArchiveBuilderCacheDirectory($environment)
{
$theliaCacheDir = THELIA_CACHE_DIR . $environment . DS;
if (!is_writable($theliaCacheDir)) {
throw new \ErrorException(
$this->translator->trans(
"The cache directory \"%env\" is not writable",
[
"%env" => $environment
]
)
);
}
$archiveBuilderCacheDir = $this->cache_dir = $theliaCacheDir . static::TEMP_DIRECTORY_NAME;
if (!is_dir($archiveBuilderCacheDir) && !mkdir($archiveBuilderCacheDir, 0755)) {
throw new \ErrorException(
$this->translator->trans(
"Error while creating the directory \"%directory\"",
[
"%directory" => static::TEMP_DIRECTORY_NAME
]
)
);
}
return $archiveBuilderCacheDir;
}
public function getCacheDir()
{
return $this->cacheDir;
}
/**
* @return Tlog
*/
public function getLogger()
{
return $this->logger;
}
/**
* @return Translator
*/
public function getTranslator()
{
return $this->translator;
}
}

View File

@@ -33,8 +33,6 @@ use Thelia\Tools\FileDownload\FileDownloaderInterface;
*/
class ZipArchiveBuilder extends AbstractArchiveBuilder
{
const TEMP_DIRECTORY_NAME = "archive_builder";
/**
* @var \ZipArchive
*/
@@ -43,30 +41,18 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
/**
* @var string This is the absolute path to the zip file in cache
*/
protected $zip_cache_file;
protected $zipCacheFile;
/**
* @var string This is the path of the cache
*/
protected $cache_dir;
/**
* @var \Thelia\Log\Tlog
*/
protected $logger;
/**
* @var Translator
*/
protected $translator;
protected $cacheDir;
public function __construct()
{
parent::__construct();
$this->zip = new \ZipArchive();
$this->logger = Tlog::getNewInstance();
$this->translator = Translator::getInstance();
}
/**
@@ -78,8 +64,8 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
if ($this->zip instanceof \ZipArchive) {
@$this->zip->close();
if (file_exists($this->zip_cache_file)) {
unlink($this->zip_cache_file);
if (file_exists($this->zipCacheFile)) {
unlink($this->zipCacheFile);
}
}
}
@@ -106,7 +92,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$directoryInArchive = "";
}
if(!empty($directoryInArchive) && $directoryInArchive != "/") {
if(!empty($directoryInArchive)) {
$directoryInArchive = $this->getDirectoryPath($directoryInArchive);
if (!$this->zip->addEmptyDir($directoryInArchive)) {
@@ -121,8 +107,12 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
}
}
/**
* Download the file if it is online
* If it's local check if the file exists and if it is redable
*/
if ($isOnline) {
$fileDownloadCache = $this->cache_dir . DS . "download";
$fileDownloadCache = $this->cacheDir . DS . "download";
$this->getFileDownloader()
->download($filePath, $fileDownloadCache)
@@ -149,6 +139,10 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$name = basename($filePath);
}
/**
* Then write the file in the archive and commit the changes
*/
$destination = $directoryInArchive . $name;
if (!$this->zip->addFile($filePath,$destination)) {
@@ -212,22 +206,22 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$this->commit();
if (!file_exists($this->zip_cache_file)) {
$this->throwFileNotFound($this->zip_cache_file);
if (!file_exists($this->zipCacheFile)) {
$this->throwFileNotFound($this->zipCacheFile);
}
if (!is_readable($this->zip_cache_file)) {
if (!is_readable($this->zipCacheFile)) {
throw new FileNotReadableException(
$this->translator->trans(
"The cache file %file is not readable",
[
"%file" => $this->zip_cache_file
"%file" => $this->zipCacheFile
]
)
);
}
$content = file_get_contents($this->zip_cache_file);
$content = file_get_contents($this->zipCacheFile);
$this->zip->close();
@@ -344,36 +338,11 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
*/
public function setEnvironment($environment)
{
$theliaCacheDir = THELIA_CACHE_DIR . $environment . DS;
if (!is_writable($theliaCacheDir)) {
throw new \ErrorException(
$this->translator->trans(
"The cache directory \"%env\" is not writable",
[
"%env" => $environment
]
)
);
}
$archiveBuilderCacheDir = $this->cache_dir = $theliaCacheDir . static::TEMP_DIRECTORY_NAME;
if (!is_dir($archiveBuilderCacheDir) && !mkdir($archiveBuilderCacheDir, 0755)) {
throw new \ErrorException(
$this->translator->trans(
"Error while creating the directory \"%directory\"",
[
"%directory" => static::TEMP_DIRECTORY_NAME
]
)
);
}
$cacheFileName = md5 (uniqid());
$cacheFile = $archiveBuilderCacheDir . DS . $cacheFileName;
$cacheFile .= "." . $this->getExtension();
$cacheFile = $this->getArchiveBuilderCacheDirectory($environment) . DS;
$cacheFile .= $cacheFileName . "." . $this->getExtension();
if (file_exists($cacheFile)) {
unlink($cacheFile);
@@ -387,12 +356,12 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
if($opening !== true) {
throw new \ErrorException(
$this->translator->trans(
"Unknown"
"An unknown error append"
)
);
}
$this->zip_cache_file = $cacheFile;
$this->zipCacheFile = $cacheFile;
return $this;
}
@@ -572,22 +541,6 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
return "application/zip";
}
/**
* @return Tlog
*/
public function getLogger()
{
return $this->logger;
}
/**
* @return Translator
*/
public function getTranslator()
{
return $this->translator;
}
/**
* @return \ZipArchive
*/
@@ -598,11 +551,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
public function getZipCacheFile()
{
return $this->zip_cache_file;
return $this->zipCacheFile;
}
public function getCacheDir()
{
return $this->cache_dir;
}
}

View File

@@ -48,8 +48,6 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
/**
* This method formats a path to be compatible with \ZipArchive
*
*
*/
public function testGetFilePath()
{