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 d5188bfe3c
commit 9eecad5658
4 changed files with 114 additions and 18 deletions

View File

@@ -144,10 +144,28 @@
<argument>%kernel.environment%</argument> <argument>%kernel.environment%</argument>
</service> </service>
<!-- zip -->
<service id="thelia.manager.zip_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder"> <service id="thelia.manager.zip_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder">
<tag name="thelia.manager.archive_builder" /> <tag name="thelia.manager.archive_builder" />
</service> </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" /> <service id="thelia.manager.formatter_manager" class="Thelia\Core\FileFormat\Formatter\FormatterManager" />
</services> </services>
>>>>>>> Add archive builder manager and formatter manager services >>>>>>> Add archive builder manager and formatter manager services

View File

@@ -13,12 +13,17 @@
namespace Thelia\Core\FileFormat\Archive; namespace Thelia\Core\FileFormat\Archive;
use Thelia\Core\FileFormat\FormatInterface; use Thelia\Core\FileFormat\FormatInterface;
<<<<<<< HEAD <<<<<<< HEAD
<<<<<<< HEAD
use Thelia\Core\Translation\Translator; use Thelia\Core\Translation\Translator;
use Thelia\Exception\FileNotFoundException; use Thelia\Exception\FileNotFoundException;
use Thelia\Exception\FileNotReadableException; use Thelia\Exception\FileNotReadableException;
use Thelia\Log\Tlog; use Thelia\Log\Tlog;
======= =======
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
use Thelia\Core\Translation\Translator;
use Thelia\Log\Tlog;
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
use Thelia\Tools\FileDownload\FileDownloaderAwareTrait; use Thelia\Tools\FileDownload\FileDownloaderAwareTrait;
/** /**
@@ -29,6 +34,7 @@ use Thelia\Tools\FileDownload\FileDownloaderAwareTrait;
abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilderInterface abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilderInterface
{ {
use FileDownloaderAwareTrait; use FileDownloaderAwareTrait;
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
const TEMP_DIRECTORY_NAME = "archive_builder"; const TEMP_DIRECTORY_NAME = "archive_builder";
@@ -36,6 +42,11 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
/** @var string */ /** @var string */
protected $cacheFile; protected $cacheFile;
=======
const TEMP_DIRECTORY_NAME = "archive_builder";
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
/** @var \Thelia\Core\Translation\Translator */ /** @var \Thelia\Core\Translation\Translator */
protected $translator; protected $translator;
@@ -45,9 +56,12 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
/** @var string */ /** @var string */
protected $cacheDir; protected $cacheDir;
<<<<<<< HEAD
/** @var string */ /** @var string */
protected $environment; protected $environment;
=======
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
public function __construct() public function __construct()
{ {
$this->translator = Translator::getInstance(); $this->translator = Translator::getInstance();
@@ -70,7 +84,11 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
); );
} }
<<<<<<< HEAD
$archiveBuilderCacheDir = $this->cacheDir = $theliaCacheDir . static::TEMP_DIRECTORY_NAME; $archiveBuilderCacheDir = $this->cacheDir = $theliaCacheDir . static::TEMP_DIRECTORY_NAME;
=======
$archiveBuilderCacheDir = $this->cache_dir = $theliaCacheDir . static::TEMP_DIRECTORY_NAME;
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
if (!is_dir($archiveBuilderCacheDir) && !mkdir($archiveBuilderCacheDir, 0755)) { if (!is_dir($archiveBuilderCacheDir) && !mkdir($archiveBuilderCacheDir, 0755)) {
throw new \ErrorException( throw new \ErrorException(
@@ -86,6 +104,7 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
return $archiveBuilderCacheDir; return $archiveBuilderCacheDir;
} }
<<<<<<< HEAD
/** /**
* @param $pathToFile * @param $pathToFile
* @param $destination * @param $destination
@@ -177,6 +196,8 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
return $this; return $this;
} }
=======
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
public function getCacheDir() public function getCacheDir()
{ {
@@ -198,6 +219,7 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
{ {
return $this->translator; return $this->translator;
} }
<<<<<<< HEAD
public function getCacheFile() public function getCacheFile()
{ {
@@ -219,3 +241,6 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
======= =======
} }
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
}
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources

View File

@@ -41,10 +41,13 @@ use Thelia\Tools\FileDownload\FileDownloaderInterface;
class ZipArchiveBuilder extends AbstractArchiveBuilder class ZipArchiveBuilder extends AbstractArchiveBuilder
{ {
<<<<<<< HEAD <<<<<<< HEAD
<<<<<<< HEAD
======= =======
const TEMP_DIRECTORY_NAME = "archive_builder"; const TEMP_DIRECTORY_NAME = "archive_builder";
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
/** /**
* @var \ZipArchive * @var \ZipArchive
*/ */
@@ -60,31 +63,23 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
/** /**
* @var string This is the absolute path to the zip file in cache * @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 * @var string This is the path of the cache
*/ */
protected $cache_dir; protected $cacheDir;
/**
* @var \Thelia\Log\Tlog
*/
protected $logger;
/**
* @var Translator
*/
protected $translator;
public function __construct() public function __construct()
{ {
$this->zip = new \ZipArchive(); parent::__construct();
$this->logger = Tlog::getNewInstance();
<<<<<<< HEAD
$this->translator = Translator::getInstance(); $this->translator = Translator::getInstance();
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
$this->zip = new \ZipArchive();
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
} }
/** /**
@@ -100,6 +95,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
if ($this->zip instanceof \ZipArchive) { if ($this->zip instanceof \ZipArchive) {
@$this->zip->close(); @$this->zip->close();
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
if (file_exists($this->cacheFile)) { if (file_exists($this->cacheFile)) {
unlink($this->cacheFile); unlink($this->cacheFile);
@@ -107,6 +103,10 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
if (file_exists($this->zip_cache_file)) { if (file_exists($this->zip_cache_file)) {
unlink($this->zip_cache_file); unlink($this->zip_cache_file);
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
if (file_exists($this->zipCacheFile)) {
unlink($this->zipCacheFile);
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
} }
} }
} }
@@ -250,7 +250,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$directoryInArchive = ""; $directoryInArchive = "";
} }
if(!empty($directoryInArchive) && $directoryInArchive != "/") { if(!empty($directoryInArchive)) {
$directoryInArchive = $this->getDirectoryPath($directoryInArchive); $directoryInArchive = $this->getDirectoryPath($directoryInArchive);
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
@@ -266,6 +266,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
} }
} }
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
return $this; return $this;
} }
@@ -341,8 +342,14 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
return $initialString . "/"; return $initialString . "/";
======= =======
=======
/**
* Download the file if it is online
* If it's local check if the file exists and if it is redable
*/
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
if ($isOnline) { if ($isOnline) {
$fileDownloadCache = $this->cache_dir . DS . "download"; $fileDownloadCache = $this->cacheDir . DS . "download";
$this->getFileDownloader() $this->getFileDownloader()
->download($filePath, $fileDownloadCache) ->download($filePath, $fileDownloadCache)
@@ -369,6 +376,10 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$name = basename($filePath); $name = basename($filePath);
} }
/**
* 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($filePath,$destination)) {
@@ -441,6 +452,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$this->commit(); $this->commit();
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
if (!file_exists($this->cacheFile)) { if (!file_exists($this->cacheFile)) {
$this->throwFileNotFound($this->cacheFile); $this->throwFileNotFound($this->cacheFile);
@@ -454,25 +466,40 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
if (!is_readable($this->zip_cache_file)) { if (!is_readable($this->zip_cache_file)) {
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
if (!file_exists($this->zipCacheFile)) {
$this->throwFileNotFound($this->zipCacheFile);
}
if (!is_readable($this->zipCacheFile)) {
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
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",
[ [
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
"%file" => $this->cacheFile "%file" => $this->cacheFile
======= =======
"%file" => $this->zip_cache_file "%file" => $this->zip_cache_file
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
"%file" => $this->zipCacheFile
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
] ]
) )
); );
} }
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
$content = file_get_contents($this->cacheFile); $content = file_get_contents($this->cacheFile);
======= =======
$content = file_get_contents($this->zip_cache_file); $content = file_get_contents($this->zip_cache_file);
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
$content = file_get_contents($this->zipCacheFile);
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
$this->zip->close(); $this->zip->close();
@@ -642,6 +669,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
*/ */
public function setEnvironment($environment) public function setEnvironment($environment)
{ {
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
parent::setEnvironment($environment); parent::setEnvironment($environment);
@@ -678,6 +706,13 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$cacheFile = $archiveBuilderCacheDir . DS . $cacheFileName; $cacheFile = $archiveBuilderCacheDir . DS . $cacheFileName;
$cacheFile .= "." . $this->getExtension(); $cacheFile .= "." . $this->getExtension();
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
$cacheFileName = md5 (uniqid());
$cacheFile = $this->getArchiveBuilderCacheDirectory($environment) . DS;
$cacheFile .= $cacheFileName . "." . $this->getExtension();
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
if (file_exists($cacheFile)) { if (file_exists($cacheFile)) {
unlink($cacheFile); unlink($cacheFile);
@@ -697,17 +732,25 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
if($opening !== true) { if($opening !== true) {
throw new \ErrorException( throw new \ErrorException(
$this->translator->trans( $this->translator->trans(
<<<<<<< HEAD
"Unknown" "Unknown"
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
"An unknown error append"
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
) )
); );
} }
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
$this->cacheFile = $cacheFile; $this->cacheFile = $cacheFile;
======= =======
$this->zip_cache_file = $cacheFile; $this->zip_cache_file = $cacheFile;
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
$this->zipCacheFile = $cacheFile;
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
return $this; return $this;
} }
@@ -920,6 +963,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
/** /**
<<<<<<< HEAD <<<<<<< HEAD
<<<<<<< HEAD
======= =======
* @return Tlog * @return Tlog
*/ */
@@ -938,6 +982,8 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
/** /**
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
* @return \ZipArchive * @return \ZipArchive
*/ */
public function getRawZipArchive() public function getRawZipArchive()
@@ -950,12 +996,16 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
public function getZipCacheFile() public function getZipCacheFile()
{ {
return $this->zip_cache_file; return $this->zipCacheFile;
} }
<<<<<<< HEAD
public function getCacheDir() public function getCacheDir()
{ {
return $this->cache_dir; return $this->cache_dir;
} }
} }
>>>>>>> Define archive builders and formatters >>>>>>> Define archive builders and formatters
=======
}
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources

View File

@@ -55,6 +55,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
/** /**
* This method formats a path to be compatible with \ZipArchive * This method formats a path to be compatible with \ZipArchive
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
*/ */
public function testFormatFilePath() public function testFormatFilePath()
@@ -65,6 +66,8 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
======= =======
* *
* *
=======
>>>>>>> Begin tar, tar.bz2 and tar.gz formatter, fix zip test resources
*/ */
public function testGetFilePath() public function testGetFilePath()
{ {