Finish tar, tar.gz, tar.bz2 and tests

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/TarArchiveBuilder.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarBz2ArchiveBuilder.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/TarGzArchiveBuilder.php
	modifié:         core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php
	modifié:         core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderInterface.php
	modifié:         core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarArchiveBuilderTest.php
	nouveau fichier: core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarBz2ArchiveBuilderTest.php
	nouveau fichier: core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TarGzArchiveBuilderTest.php
	modifié:         core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php
This commit is contained in:
Benjamin Perche
2014-07-04 09:00:03 +02:00
parent c4d22b8c79
commit f62b14b842
11 changed files with 280 additions and 122 deletions

View File

@@ -148,18 +148,18 @@
</service>
<!-- tar -->
<service id="thelia.manager.tar_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder">
<service id="thelia.manager.tar_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarArchiveBuilder">
<tag name="thelia.manager.archive_builder" />
</service>
<!-- tar.gz -->
<service id="thelia.manager.tar_gz_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveBuilder">
<service id="thelia.manager.tar_gz_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarGzArchiveBuilder">
<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">
<service id="thelia.manager.tar_bz2_archive_builder" class="Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarBz2ArchiveBuilder">
<argument id="compressionType" type="string">bz2</argument>
<tag name="thelia.manager.archive_builder" />
</service>

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,47 @@
<?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;
/**
* Class TarBz2ArchiveBuilder
* @package Thelia\Core\FileFormat\Archive\ArchiveBuilder
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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;
}
}

View File

@@ -0,0 +1,52 @@
<?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;
/**
* Class TarGzArchiveBuilder
* @package Thelia\Core\FileFormat\Archive\ArchiveBuilder
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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;
}
}

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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()
);
}
}

View File

@@ -0,0 +1,38 @@
<?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 Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarBz2ArchiveBuilder;
/**
* Class TarBz2ArchiveBuilderTest
* @package Thelia\Tests\FileFormat\Archive\ArchiveBuilder
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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()
);
}
}

View File

@@ -0,0 +1,39 @@
<?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 Thelia\Core\FileFormat\Archive\ArchiveBuilder\TarGzArchiveBuilder;
use Thelia\Core\HttpFoundation\Response;
/**
* Class TarGzArchiveBuilderTest
* @package Thelia\Tests\FileFormat\Archive\ArchiveBuilder
* @author Benjamin Perche <bperche@openstudio.fr>
*/
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()
);
}
}

View File

@@ -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
);
}