diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 74392256a..60f1eb0e6 100644 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -148,18 +148,18 @@ - + - + gz - + bz2 diff --git a/core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php b/core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php index b444860b6..aa6e2ccad 100644 --- a/core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php +++ b/core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php @@ -41,6 +41,9 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder /** @var string */ protected $cacheDir; + /** @var string */ + protected $environment; + public function __construct() { $this->translator = Translator::getInstance(); @@ -196,4 +199,16 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder { return $this->cacheFile; } + + /** + * @param string $environment + * @return $this + * + * Sets the execution environment of the Kernel, + * used to know which cache is used. + */ + public function setEnvironment($environment) + { + $this->environment = $environment; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php index 08cd483e9..6d9db55fd 100644 --- a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php +++ b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilder.php @@ -47,29 +47,6 @@ class TarArchiveBuilder extends AbstractArchiveBuilder /** @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) { @@ -303,29 +280,19 @@ class TarArchiveBuilder extends AbstractArchiveBuilder * * Loads an archive */ - public static function loadArchive( - $pathToArchive, - $environment, - $isOnline = false, - FileDownloaderInterface $fileDownloader = null - ) { - /** @var TarArchiveBuilder $instance */ - $instance = new static(); + public function loadArchive($pathToArchive, $isOnline = false) + { + $tar = clone $this; - if ($fileDownloader !== null) { - $instance->setFileDownloader($fileDownloader); - } - - $instance->setCacheFile($instance->generateCacheFile($environment)) - ->copyFile($pathToArchive, $instance->getCacheFile(), $isOnline); + $tar + ->setCacheFile($tar->generateCacheFile($this->environment)) + ->copyFile($pathToArchive, $tar->getCacheFile(), $isOnline); /** * This throws TarArchiveBuilderException if * the archive is not valid. */ - $instance->setEnvironment($environment); - - return $instance; + return $tar->setEnvironment($tar->environment); } /** @@ -391,6 +358,14 @@ class TarArchiveBuilder extends AbstractArchiveBuilder */ public function setEnvironment($environment) { + if (empty($environment)) { + throw new \ErrorException( + $this->translator->trans( + "You must define an environment when you use an archive builder" + ) + ); + } + if ($this->cacheFile === null) { $cacheFile = $this->generateCacheFile($environment); @@ -406,16 +381,8 @@ class TarArchiveBuilder extends AbstractArchiveBuilder 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; - } + $this->compressionEntryPoint(); + } catch(\BadMethodCallException $e) { /** @@ -438,6 +405,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder } $this->cacheFile = $cacheFile; + $this->environment = $environment; return $this; } @@ -486,13 +454,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder */ public function getName() { - $name = "tar"; - - if ($this->compression !== null) { - $name .= "." . $this->compression; - } - - return $name; + return "tar"; } /** @@ -506,7 +468,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder */ public function getExtension() { - return $this->getName(); + return "tar"; } /** @@ -519,10 +481,19 @@ class TarArchiveBuilder extends AbstractArchiveBuilder */ public function getMimeType() { - return $this->compression === null ? - "application/x-tar" : - "application/x-gtar" - ; + return "application/x-tar"; } + protected function compressionEntryPoint() + { + /** + * This method must be overwritten if you want to do some + * stuff to compress you archive + */ + } + + public function getCompression() + { + return $this->compression; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarBz2ArchiveBuilder.php b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarBz2ArchiveBuilder.php new file mode 100644 index 000000000..bd85d8599 --- /dev/null +++ b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarBz2ArchiveBuilder.php @@ -0,0 +1,47 @@ + + */ +class TarBz2ArchiveBuilder extends TarArchiveBuilder +{ + public function getName() + { + return "tar.bz2"; + } + + public function getMimeType() + { + return "application/x-gtar"; + } + + public function getExtension() + { + return "tar.bz2"; + } + + protected function compressionEntryPoint() + { + if ($this->compression != \Phar::BZ2) { + $this->tar = $this->tar->compress(\Phar::BZ2, $this->getExtension()); + } + + $this->compression = \Phar::BZ2; + + return $this; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarGzArchiveBuilder.php b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarGzArchiveBuilder.php new file mode 100644 index 000000000..33c16aba9 --- /dev/null +++ b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarGzArchiveBuilder.php @@ -0,0 +1,52 @@ + + */ +class TarGzArchiveBuilder extends TarArchiveBuilder +{ + public function getName() + { + return "tar.gz"; + } + + public function getMimeType() + { + return "application/x-gtar"; + } + + public function getExtension() + { + return "tar.gz"; + } + + public function setEnvironment($environment) + { + parent::setEnvironment($environment); + + $this->previousFile = $this->cacheFile; + + if ($this->compression != \Phar::GZ) { + $this->tar = $this->tar->compress(\Phar::BZ2, $this->getExtension()); + } + + $this->compression = \Phar::GZ; + + return $this; + } + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php index 72ce37c91..7262923a8 100644 --- a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php +++ b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php @@ -92,7 +92,7 @@ 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 */ - $fileDownloadCache = $this->cacheDir . DS . "download.tmp"; + $fileDownloadCache = $this->cacheDir . DS . md5(uniqid()) . ".tmp"; $this->copyFile($filePath, $fileDownloadCache, $isOnline); /** @@ -110,9 +110,15 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder $this->logger->error($translatedErrorMessage); + // if error delete the cache file + unlink($fileDownloadCache); + throw new \ErrorException($translatedErrorMessage); } + // If not too + unlink($fileDownloadCache); + $this->commit(); return $this; @@ -331,41 +337,36 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder /** * @param string $pathToArchive - * @param string $environment * @param bool $isOnline * @param FileDownloaderInterface $fileDownloader - * @return $this + * @return ZipArchiveBuilder * @throws \Thelia\Exception\FileNotFoundException * @throws \Thelia\Exception\HttpUrlException * * Loads an archive */ - public static function loadArchive( - $pathToArchive, - $environment, - $isOnline = false, - FileDownloaderInterface $fileDownloader = null - ) { - /** @var ZipArchiveBuilder $instance */ - $instance = new static(); + public function loadArchive($pathToArchive, $isOnline = false) + { + $back = $this->zip; + $this->zip = new \ZipArchive(); + $zip = clone $this; + $this->zip = $back; - $instance->setEnvironment($environment); - $zip = $instance->getRawZipArchive(); - $zip->close(); + $zip->setEnvironment($this->environment); - if ($fileDownloader !== null) { - $instance->setFileDownloader($fileDownloader); - } + $zip->copyFile( + $pathToArchive, + $zip->getCacheFile(), + $isOnline + ); - $instance->copyFile($pathToArchive, $instance->getCacheFile(), $isOnline); - - if (true !== $return = $zip->open($instance->getCacheFile())) { + if (true !== $return = $zip->getRawZipArchive()->open($zip->getCacheFile())) { throw new ZipArchiveException( - $instance->getZipErrorMessage($return) + $zip->getZipErrorMessage($return) ); } - return $instance; + return $zip; } /** @@ -403,6 +404,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder */ public function setEnvironment($environment) { + parent::setEnvironment($environment); $cacheFile = $this->generateCacheFile($environment); diff --git a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderInterface.php b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderInterface.php index 88f5da78f..c8166779b 100644 --- a/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderInterface.php +++ b/core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderInterface.php @@ -11,7 +11,7 @@ /*************************************************************************************/ namespace Thelia\Core\FileFormat\Archive; -use Thelia\Tools\FileDownload\FileDownloaderInterface; + /** * Interface ArchiveBuilderInterface @@ -86,16 +86,14 @@ interface ArchiveBuilderInterface /** * @param string $pathToArchive - * @param string $environment * @param bool $isOnline - * @param FileDownloaderInterface $fileDownloader * @return $this * @throws \Thelia\Exception\FileNotFoundException * @throws \Thelia\Exception\HttpUrlException * * Loads an archive */ - public static function loadArchive($pathToArchive, $environment, $isOnline = false, FileDownloaderInterface $fileDownloader = null); + public function loadArchive($pathToArchive, $isOnline = false); /** * @param $pathToFile @@ -113,12 +111,4 @@ interface ArchiveBuilderInterface */ public function hasDirectory($directory); - /** - * @param string $environment - * @return $this - * - * Sets the execution environment of the Kernel, - * used to know which cache is used. - */ - public function setEnvironment($environment); } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php index 323300714..fe8728d14 100644 --- a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php @@ -151,9 +151,8 @@ class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase public function testLoadValidArchive() { - $tar = TarArchiveBuilder::loadArchive( - __DIR__ . DS . "TestResources/well_formatted.tar", - "dev" + $tar = $this->tar->loadArchive( + __DIR__ . DS . "TestResources/well_formatted.tar" ); $this->assertInstanceOf( @@ -171,9 +170,8 @@ class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase */ public function testLoadInvalidArchive() { - $tar = TarArchiveBuilder::loadArchive( - __DIR__ . DS . "TestResources/bad_formatted.tar", - "dev" + $tar = $this->tar->loadArchive( + __DIR__ . DS . "TestResources/bad_formatted.tar" ); } @@ -282,4 +280,12 @@ class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase $this->tar->formatFilePath("//foo/bar///baz/") ); } + + public function testCompression() + { + $this->assertEquals( + null, + $this->tar->getCompression() + ); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarBz2ArchiveBuilderTest.php b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarBz2ArchiveBuilderTest.php new file mode 100644 index 000000000..f968bf759 --- /dev/null +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarBz2ArchiveBuilderTest.php @@ -0,0 +1,38 @@ + + */ +class TarBz2ArchiveBuilderTest extends TarArchiveBuilderTest +{ + public function setUp() + { + parent::setUp(); + + $this->tar = new TarBz2ArchiveBuilder(); + $this->tar->setEnvironment("dev"); + } + + public function testCompression() + { + $this->assertEquals( + \Phar::BZ2, + $this->tar->getCompression() + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarGzArchiveBuilderTest.php b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarGzArchiveBuilderTest.php new file mode 100644 index 000000000..e70ea33ee --- /dev/null +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarGzArchiveBuilderTest.php @@ -0,0 +1,39 @@ + + */ +class TarGzArchiveBuilderTest extends TarArchiveBuilderTest +{ + public function setUp() + { + parent::setUp(); + + $this->tar = new TarGzArchiveBuilder(); + $this->tar->setEnvironment("dev"); + } + + public function testCompression() + { + $this->assertEquals( + \Phar::GZ, + $this->tar->getCompression() + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php index 0f141d1b6..bc3560694 100644 --- a/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php +++ b/core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php @@ -40,9 +40,10 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase $this->zip = new ZipArchiveBuilder(); + $this->zip->setEnvironment("dev"); + $this->loadedZip = $this->zip->loadArchive( - __DIR__ . DS . "TestResources/well_formatted.zip", - "dev" + __DIR__ . DS . "TestResources/well_formatted.zip" ); } @@ -158,8 +159,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase public function testLoadValidZip() { $loadedZip = $this->zip->loadArchive( - __DIR__ . DS . "TestResources/well_formatted.zip", - "dev" + __DIR__ . DS . "TestResources/well_formatted.zip" ); $this->assertInstanceOf( @@ -175,8 +175,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase public function testLoadNotValidZip() { $this->zip->loadArchive( - __DIR__ . DS . "TestResources/bad_formatted.zip", - "dev" + __DIR__ . DS . "TestResources/bad_formatted.zip" ); } @@ -186,18 +185,17 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase public function testLoadNotExistingFile() { $this->zip->loadArchive( - __DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip", - "dev" + __DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip" ); } public function testLoadOnlineAvailableAndValidFile() { + $this->zip->setFileDownloader(FakeFileDownloader::getInstance()); + $this->zip->loadArchive( __DIR__ . DS . "TestResources/well_formatted.zip", - "dev", - true, - FakeFileDownloader::getInstance() + true ); } @@ -207,11 +205,11 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase */ public function testLoadOnlineAvailableAndNotValidFile() { + $this->zip->setFileDownloader(FakeFileDownloader::getInstance()); + $this->zip->loadArchive( __DIR__ . DS . "TestResources/bad_formatted.zip", - "dev", - true, - FakeFileDownloader::getInstance() + true ); } @@ -220,11 +218,11 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase */ public function testLoadOnlineNotExistingFile() { + $this->zip->setFileDownloader(FakeFileDownloader::getInstance()); + $this->zip->loadArchive( __DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip", - "dev", - true, - FakeFileDownloader::getInstance() + true ); }