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> </service>
<!-- tar --> <!-- 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" /> <tag name="thelia.manager.archive_builder" />
</service> </service>
<!-- tar.gz --> <!-- 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> <argument id="compressionType" type="string">gz</argument>
<tag name="thelia.manager.archive_builder" /> <tag name="thelia.manager.archive_builder" />
</service> </service>
<!-- tar.bz2 --> <!-- 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> <argument id="compressionType" type="string">bz2</argument>
<tag name="thelia.manager.archive_builder" /> <tag name="thelia.manager.archive_builder" />
</service> </service>

View File

@@ -41,6 +41,9 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
/** @var string */ /** @var string */
protected $cacheDir; protected $cacheDir;
/** @var string */
protected $environment;
public function __construct() public function __construct()
{ {
$this->translator = Translator::getInstance(); $this->translator = Translator::getInstance();
@@ -196,4 +199,16 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
{ {
return $this->cacheFile; 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 */ /** @var \Thelia\Log\Tlog */
protected $logger; 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() public function __destruct()
{ {
if ($this->tar instanceof \PharData) { if ($this->tar instanceof \PharData) {
@@ -303,29 +280,19 @@ class TarArchiveBuilder extends AbstractArchiveBuilder
* *
* Loads an archive * Loads an archive
*/ */
public static function loadArchive( public function loadArchive($pathToArchive, $isOnline = false)
$pathToArchive, {
$environment, $tar = clone $this;
$isOnline = false,
FileDownloaderInterface $fileDownloader = null
) {
/** @var TarArchiveBuilder $instance */
$instance = new static();
if ($fileDownloader !== null) { $tar
$instance->setFileDownloader($fileDownloader); ->setCacheFile($tar->generateCacheFile($this->environment))
} ->copyFile($pathToArchive, $tar->getCacheFile(), $isOnline);
$instance->setCacheFile($instance->generateCacheFile($environment))
->copyFile($pathToArchive, $instance->getCacheFile(), $isOnline);
/** /**
* This throws TarArchiveBuilderException if * This throws TarArchiveBuilderException if
* the archive is not valid. * the archive is not valid.
*/ */
$instance->setEnvironment($environment); return $tar->setEnvironment($tar->environment);
return $instance;
} }
/** /**
@@ -391,6 +358,14 @@ class TarArchiveBuilder extends AbstractArchiveBuilder
*/ */
public function setEnvironment($environment) 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) { if ($this->cacheFile === null) {
$cacheFile = $this->generateCacheFile($environment); $cacheFile = $this->generateCacheFile($environment);
@@ -406,16 +381,8 @@ class TarArchiveBuilder extends AbstractArchiveBuilder
try { try {
$this->tar = new \PharData($cacheFile, null, null, static::PHAR_FORMAT); $this->tar = new \PharData($cacheFile, null, null, static::PHAR_FORMAT);
switch ($this->compression) { $this->compressionEntryPoint();
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) { } catch(\BadMethodCallException $e) {
/** /**
@@ -438,6 +405,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder
} }
$this->cacheFile = $cacheFile; $this->cacheFile = $cacheFile;
$this->environment = $environment;
return $this; return $this;
} }
@@ -486,13 +454,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder
*/ */
public function getName() public function getName()
{ {
$name = "tar"; return "tar";
if ($this->compression !== null) {
$name .= "." . $this->compression;
}
return $name;
} }
/** /**
@@ -506,7 +468,7 @@ class TarArchiveBuilder extends AbstractArchiveBuilder
*/ */
public function getExtension() public function getExtension()
{ {
return $this->getName(); return "tar";
} }
/** /**
@@ -519,10 +481,19 @@ class TarArchiveBuilder extends AbstractArchiveBuilder
*/ */
public function getMimeType() public function getMimeType()
{ {
return $this->compression === null ? return "application/x-tar";
"application/x-tar" :
"application/x-gtar"
;
} }
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 * 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
*/ */
$fileDownloadCache = $this->cacheDir . DS . "download.tmp"; $fileDownloadCache = $this->cacheDir . DS . md5(uniqid()) . ".tmp";
$this->copyFile($filePath, $fileDownloadCache, $isOnline); $this->copyFile($filePath, $fileDownloadCache, $isOnline);
/** /**
@@ -110,9 +110,15 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$this->logger->error($translatedErrorMessage); $this->logger->error($translatedErrorMessage);
// if error delete the cache file
unlink($fileDownloadCache);
throw new \ErrorException($translatedErrorMessage); throw new \ErrorException($translatedErrorMessage);
} }
// If not too
unlink($fileDownloadCache);
$this->commit(); $this->commit();
return $this; return $this;
@@ -331,41 +337,36 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
/** /**
* @param string $pathToArchive * @param string $pathToArchive
* @param string $environment
* @param bool $isOnline * @param bool $isOnline
* @param FileDownloaderInterface $fileDownloader * @param FileDownloaderInterface $fileDownloader
* @return $this * @return ZipArchiveBuilder
* @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( public function loadArchive($pathToArchive, $isOnline = false)
$pathToArchive, {
$environment, $back = $this->zip;
$isOnline = false, $this->zip = new \ZipArchive();
FileDownloaderInterface $fileDownloader = null $zip = clone $this;
) { $this->zip = $back;
/** @var ZipArchiveBuilder $instance */
$instance = new static();
$instance->setEnvironment($environment); $zip->setEnvironment($this->environment);
$zip = $instance->getRawZipArchive();
$zip->close();
if ($fileDownloader !== null) { $zip->copyFile(
$instance->setFileDownloader($fileDownloader); $pathToArchive,
} $zip->getCacheFile(),
$isOnline
);
$instance->copyFile($pathToArchive, $instance->getCacheFile(), $isOnline); if (true !== $return = $zip->getRawZipArchive()->open($zip->getCacheFile())) {
if (true !== $return = $zip->open($instance->getCacheFile())) {
throw new ZipArchiveException( 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) public function setEnvironment($environment)
{ {
parent::setEnvironment($environment);
$cacheFile = $this->generateCacheFile($environment); $cacheFile = $this->generateCacheFile($environment);

View File

@@ -11,7 +11,7 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Core\FileFormat\Archive; namespace Thelia\Core\FileFormat\Archive;
use Thelia\Tools\FileDownload\FileDownloaderInterface;
/** /**
* Interface ArchiveBuilderInterface * Interface ArchiveBuilderInterface
@@ -86,16 +86,14 @@ 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, FileDownloaderInterface $fileDownloader = null); public function loadArchive($pathToArchive, $isOnline = false);
/** /**
* @param $pathToFile * @param $pathToFile
@@ -113,12 +111,4 @@ interface ArchiveBuilderInterface
*/ */
public function hasDirectory($directory); 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() public function testLoadValidArchive()
{ {
$tar = TarArchiveBuilder::loadArchive( $tar = $this->tar->loadArchive(
__DIR__ . DS . "TestResources/well_formatted.tar", __DIR__ . DS . "TestResources/well_formatted.tar"
"dev"
); );
$this->assertInstanceOf( $this->assertInstanceOf(
@@ -171,9 +170,8 @@ class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase
*/ */
public function testLoadInvalidArchive() public function testLoadInvalidArchive()
{ {
$tar = TarArchiveBuilder::loadArchive( $tar = $this->tar->loadArchive(
__DIR__ . DS . "TestResources/bad_formatted.tar", __DIR__ . DS . "TestResources/bad_formatted.tar"
"dev"
); );
} }
@@ -282,4 +280,12 @@ class TarArchiveBuilderTest extends \PHPUnit_Framework_TestCase
$this->tar->formatFilePath("//foo/bar///baz/") $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 = new ZipArchiveBuilder();
$this->zip->setEnvironment("dev");
$this->loadedZip = $this->zip->loadArchive( $this->loadedZip = $this->zip->loadArchive(
__DIR__ . DS . "TestResources/well_formatted.zip", __DIR__ . DS . "TestResources/well_formatted.zip"
"dev"
); );
} }
@@ -158,8 +159,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
public function testLoadValidZip() public function testLoadValidZip()
{ {
$loadedZip = $this->zip->loadArchive( $loadedZip = $this->zip->loadArchive(
__DIR__ . DS . "TestResources/well_formatted.zip", __DIR__ . DS . "TestResources/well_formatted.zip"
"dev"
); );
$this->assertInstanceOf( $this->assertInstanceOf(
@@ -175,8 +175,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
public function testLoadNotValidZip() public function testLoadNotValidZip()
{ {
$this->zip->loadArchive( $this->zip->loadArchive(
__DIR__ . DS . "TestResources/bad_formatted.zip", __DIR__ . DS . "TestResources/bad_formatted.zip"
"dev"
); );
} }
@@ -186,18 +185,17 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
public function testLoadNotExistingFile() public function testLoadNotExistingFile()
{ {
$this->zip->loadArchive( $this->zip->loadArchive(
__DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip", __DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip"
"dev"
); );
} }
public function testLoadOnlineAvailableAndValidFile() public function testLoadOnlineAvailableAndValidFile()
{ {
$this->zip->setFileDownloader(FakeFileDownloader::getInstance());
$this->zip->loadArchive( $this->zip->loadArchive(
__DIR__ . DS . "TestResources/well_formatted.zip", __DIR__ . DS . "TestResources/well_formatted.zip",
"dev", true
true,
FakeFileDownloader::getInstance()
); );
} }
@@ -207,11 +205,11 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
*/ */
public function testLoadOnlineAvailableAndNotValidFile() public function testLoadOnlineAvailableAndNotValidFile()
{ {
$this->zip->setFileDownloader(FakeFileDownloader::getInstance());
$this->zip->loadArchive( $this->zip->loadArchive(
__DIR__ . DS . "TestResources/bad_formatted.zip", __DIR__ . DS . "TestResources/bad_formatted.zip",
"dev", true
true,
FakeFileDownloader::getInstance()
); );
} }
@@ -220,11 +218,11 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
*/ */
public function testLoadOnlineNotExistingFile() public function testLoadOnlineNotExistingFile()
{ {
$this->zip->setFileDownloader(FakeFileDownloader::getInstance());
$this->zip->loadArchive( $this->zip->loadArchive(
__DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip", __DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip",
"dev", true
true,
FakeFileDownloader::getInstance()
); );
} }