Define archive builders and formatters

nouveau fichier: core/lib/Thelia/Core/FileFormat/Archive/AbstractArchiveBuilder.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilder.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilder/ZipArchiveException.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderInterface.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/Archive/ArchiveBuilderManager.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/FormatInterface.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/Formatter/AbstractFormatter.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/Formatter/Exception/BadFormattedStringException.php
	nouveau fichier: core/lib/Thelia/Core/FileFormat/Formatter/FormatterManager.php
	nouveau fichier: core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/bad_formatted.zip
	nouveau fichier: core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/test_file
	nouveau fichier: core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/TestResources/well_formatted.zip
	nouveau fichier: core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilder/ZipArchiveBuilderTest.php
	nouveau fichier: core/lib/Thelia/Tests/FileFormat/Archive/ArchiveBuilderManagerTest.php
	nouveau fichier: core/lib/Thelia/Tests/FileFormat/Formatter/FormatterManagerTest.php
	nouveau fichier: core/lib/Thelia/Tests/Tools/FakeFileDownloader.php
	nouveau fichier: core/lib/Thelia/Tests/Tools/FileDownloaderTest.php
	nouveau fichier: core/lib/Thelia/Tools/FileDownload/FileDownloader.php
	nouveau fichier: core/lib/Thelia/Tools/FileDownload/FileDownloaderAwareTrait.php
	nouveau fichier: core/lib/Thelia/Tools/FileDownload/FileDownloaderInterface.php
	modifié:         core/lib/Thelia/Tools/URL.php
This commit is contained in:
Benjamin Perche
2014-07-02 14:00:49 +02:00
parent 50adcecc27
commit 6af18cd3e7
19 changed files with 906 additions and 2 deletions

View File

@@ -12,10 +12,13 @@
namespace Thelia\Core\FileFormat\Archive;
use Thelia\Core\FileFormat\FormatInterface;
<<<<<<< HEAD
use Thelia\Core\Translation\Translator;
use Thelia\Exception\FileNotFoundException;
use Thelia\Exception\FileNotReadableException;
use Thelia\Log\Tlog;
=======
>>>>>>> Define archive builders and formatters
use Thelia\Tools\FileDownload\FileDownloaderAwareTrait;
/**
@@ -26,6 +29,7 @@ use Thelia\Tools\FileDownload\FileDownloaderAwareTrait;
abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilderInterface
{
use FileDownloaderAwareTrait;
<<<<<<< HEAD
const TEMP_DIRECTORY_NAME = "archive_builder";
@@ -212,3 +216,6 @@ abstract class AbstractArchiveBuilder implements FormatInterface, ArchiveBuilder
$this->environment = $environment;
}
}
=======
}
>>>>>>> Define archive builders and formatters

View File

@@ -12,10 +12,19 @@
namespace Thelia\Core\FileFormat\Archive\ArchiveBuilder;
use Thelia\Core\FileFormat\Archive\AbstractArchiveBuilder;
<<<<<<< HEAD
use Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\ZipArchiveException;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Thelia;
use Thelia\Exception\FileNotReadableException;
=======
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Thelia;
use Thelia\Core\Translation\Translator;
use Thelia\Exception\FileNotFoundException;
use Thelia\Exception\FileNotReadableException;
use Thelia\Log\Tlog;
>>>>>>> Define archive builders and formatters
use Thelia\Tools\FileDownload\FileDownloaderInterface;
/**
@@ -31,34 +40,79 @@ use Thelia\Tools\FileDownload\FileDownloaderInterface;
*/
class ZipArchiveBuilder extends AbstractArchiveBuilder
{
<<<<<<< HEAD
=======
const TEMP_DIRECTORY_NAME = "archive_builder";
>>>>>>> Define archive builders and formatters
/**
* @var \ZipArchive
*/
protected $zip;
<<<<<<< HEAD
public function __construct()
{
parent::__construct();
$this->zip = new \ZipArchive();
=======
/**
* @var string This is the absolute path to the zip file in cache
*/
protected $zip_cache_file;
/**
* @var string This is the path of the cache
*/
protected $cache_dir;
/**
* @var \Thelia\Log\Tlog
*/
protected $logger;
/**
* @var Translator
*/
protected $translator;
public function __construct()
{
$this->zip = new \ZipArchive();
$this->logger = Tlog::getNewInstance();
$this->translator = Translator::getInstance();
>>>>>>> Define archive builders and formatters
}
/**
* On the destruction of the class,
* remove the temporary file.
*/
<<<<<<< HEAD
public function __destruct()
=======
function __destruct()
>>>>>>> Define archive builders and formatters
{
if ($this->zip instanceof \ZipArchive) {
@$this->zip->close();
<<<<<<< HEAD
if (file_exists($this->cacheFile)) {
unlink($this->cacheFile);
=======
if (file_exists($this->zip_cache_file)) {
unlink($this->zip_cache_file);
>>>>>>> Define archive builders and formatters
}
}
}
/**
<<<<<<< HEAD
* @param string $filePath It is the path to access the file.
* @param string $directoryInArchive This is the directory where it will be stored in the archive
* @param null|string $name The name of the file in the archive. if it null or empty, it keeps the same name
@@ -67,6 +121,15 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
* @throws \Thelia\Exception\FileNotFoundException
* @throws \Thelia\Exception\FileNotReadableException
* @throws \ErrorException
=======
* @param string $filePath It is the path to access the file.
* @param string $directoryInArchive This is the directory where it will be stored in the archive
* @param null|string $name The name of the file in the archive. if it null or empty, it keeps the same name
* @param bool $isOnline
* @return $this
* @throws \Thelia\Exception\FileNotFoundException
* @throws \Thelia\Exception\FileNotReadableException
>>>>>>> Define archive builders and formatters
*
* This methods adds a file in the archive.
* If the file is local, $isOnline must be false,
@@ -74,6 +137,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
*/
public function addFile($filePath, $directoryInArchive = null, $name = null, $isOnline = false)
{
<<<<<<< HEAD
$directoryInArchive = $this->formatDirectoryPath($directoryInArchive);
/**
@@ -178,6 +242,18 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$directoryInArchive = $this->formatDirectoryPath($directoryPath);
if (!empty($directoryInArchive)) {
=======
/**
* Add empty directory if it doesn't exist
*/
if (empty($directoryInArchive) || preg_match("#^\/+$#", $directoryInArchive)) {
$directoryInArchive = "";
}
if(!empty($directoryInArchive) && $directoryInArchive != "/") {
$directoryInArchive = $this->getDirectoryPath($directoryInArchive);
>>>>>>> Define archive builders and formatters
if (!$this->zip->addEmptyDir($directoryInArchive)) {
throw new \ErrorException(
$this->translator->trans(
@@ -190,6 +266,7 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
}
}
<<<<<<< HEAD
return $this;
}
@@ -263,6 +340,54 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
}
return $initialString . "/";
=======
if ($isOnline) {
$fileDownloadCache = $this->cache_dir . DS . "download";
$this->getFileDownloader()
->download($filePath, $fileDownloadCache)
;
$filePath = $fileDownloadCache;
} else {
if (!file_exists($filePath)) {
$this->throwFileNotFound($filePath);
} else if (!is_readable($filePath)) {
throw new FileNotReadableException(
$this->translator
->trans(
"The file %file is not readable",
[
"%file" => $filePath,
]
)
);
}
}
if (empty($name)) {
$name = basename($filePath);
}
$destination = $directoryInArchive . $name;
if (!$this->zip->addFile($filePath,$destination)) {
$translatedErrorMessage = $this->translator->trans(
"An error occurred while adding this file to the archive: %file",
[
"%file" => $filePath
]
);
$this->logger->error($translatedErrorMessage);
throw new \ErrorException($translatedErrorMessage);
}
$this->commit();
return $this;
>>>>>>> Define archive builders and formatters
}
/**
@@ -275,7 +400,11 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
*/
public function deleteFile($pathInArchive)
{
<<<<<<< HEAD
$pathInArchive = $this->formatFilePath($pathInArchive);
=======
$pathInArchive = $this->getFilePath($pathInArchive);
>>>>>>> Define archive builders and formatters
if (!$this->hasFile($pathInArchive)) {
$this->throwFileNotFound($pathInArchive);
@@ -302,28 +431,48 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
*
* This method return an instance of a Response with the archive as content.
*/
<<<<<<< HEAD
public function buildArchiveResponse($filename)
=======
public function buildArchiveResponse()
>>>>>>> Define archive builders and formatters
{
$this->zip->comment = "Generated by Thelia v" . Thelia::THELIA_VERSION;
$this->commit();
<<<<<<< HEAD
if (!file_exists($this->cacheFile)) {
$this->throwFileNotFound($this->cacheFile);
}
if (!is_readable($this->cacheFile)) {
=======
if (!file_exists($this->zip_cache_file)) {
$this->throwFileNotFound($this->zip_cache_file);
}
if (!is_readable($this->zip_cache_file)) {
>>>>>>> Define archive builders and formatters
throw new FileNotReadableException(
$this->translator->trans(
"The cache file %file is not readable",
[
<<<<<<< HEAD
"%file" => $this->cacheFile
=======
"%file" => $this->zip_cache_file
>>>>>>> Define archive builders and formatters
]
)
);
}
<<<<<<< HEAD
$content = file_get_contents($this->cacheFile);
=======
$content = file_get_contents($this->zip_cache_file);
>>>>>>> Define archive builders and formatters
$this->zip->close();
@@ -331,22 +480,35 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
$content,
200,
[
<<<<<<< HEAD
"Content-Type" => $this->getMimeType(),
"Content-Disposition" => $filename . "." . $this->getExtension(),
=======
"Content-Type" => $this->getMimeType()
>>>>>>> Define archive builders and formatters
]
);
}
/**
<<<<<<< HEAD
* @param string $pathToArchive
* @param bool $isOnline
* @param FileDownloaderInterface $fileDownloader
* @return ZipArchiveBuilder
=======
* @param string $pathToArchive
* @param string $environment
* @param bool $isOnline
* @param FileDownloaderInterface $fileDownloader
* @return $this
>>>>>>> Define archive builders and formatters
* @throws \Thelia\Exception\FileNotFoundException
* @throws \Thelia\Exception\HttpUrlException
*
* Loads an archive
*/
<<<<<<< HEAD
public function loadArchive($pathToArchive, $isOnline = false)
{
$back = $this->zip;
@@ -369,6 +531,64 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
}
return $zip;
=======
public static function loadArchive(
$pathToArchive,
$environment,
$isOnline = false,
FileDownloaderInterface $fileDownloader = null
) {
/** @var ZipArchiveBuilder $instance */
$instance = new static();
$instance->setEnvironment($environment);
$zip = $instance->getRawZipArchive();
$zip->close();
if ($fileDownloader !== null) {
$instance->setFileDownloader($fileDownloader);
}
if ($isOnline) {
/**
* It's an online file
*/
$instance->getFileDownloader()
->download($pathToArchive, $instance->getZipCacheFile())
;
} else {
/**
* It's a local file
*/
if (!is_file($pathToArchive) || !is_readable($pathToArchive)) {
$instance->throwFileNotFound($pathToArchive);
}
if (!copy($pathToArchive, $instance->getZipCacheFile())) {
$translatedErrorMessage = $instance->getTranslator()->trans(
"An unknown error happend while copying %prev to %dest",
[
"%prev" => $pathToArchive,
"%dest" => $instance->getZipCacheFile(),
]
);
$instance->getLogger()
->error($translatedErrorMessage)
;
throw new \ErrorException($translatedErrorMessage);
}
}
if (true !== $return = $zip->open($instance->getZipCacheFile())) {
throw new ZipArchiveException(
$instance->getZipErrorMessage($return)
);
}
return $instance;
>>>>>>> Define archive builders and formatters
}
/**
@@ -380,25 +600,41 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
public function hasFile($pathToFile)
{
return $this->zip
<<<<<<< HEAD
->locateName($this->formatFilePath($pathToFile)) !== false
=======
->locateName($this->getFilePath($pathToFile)) !== false
>>>>>>> Define archive builders and formatters
;
}
/**
<<<<<<< HEAD
* @param string $directory
=======
* @param string $directory
>>>>>>> Define archive builders and formatters
* @return bool
*
* Checks if the link $directory exists and if it's not a file.
*/
public function hasDirectory($directory)
{
<<<<<<< HEAD
$link = $this->zip->locateName($this->formatDirectoryPath($directory));
=======
$link = $this->zip->locateName($this->getDirectoryPath($directory));
>>>>>>> Define archive builders and formatters
return $link !== false;
}
/**
<<<<<<< HEAD
* @param string $environment
=======
* @param string $environment
>>>>>>> Define archive builders and formatters
* @return $this
*
* Sets the execution environment of the Kernel,
@@ -406,9 +642,42 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
*/
public function setEnvironment($environment)
{
<<<<<<< HEAD
parent::setEnvironment($environment);
$cacheFile = $this->generateCacheFile($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();
>>>>>>> Define archive builders and formatters
if (file_exists($cacheFile)) {
unlink($cacheFile);
@@ -419,15 +688,26 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
\ZipArchive::CREATE
);
<<<<<<< HEAD
if ($opening !== true) {
throw new \ErrorException(
$this->translator->trans(
"An unknown error append"
=======
if($opening !== true) {
throw new \ErrorException(
$this->translator->trans(
"Unknown"
>>>>>>> Define archive builders and formatters
)
);
}
<<<<<<< HEAD
$this->cacheFile = $cacheFile;
=======
$this->zip_cache_file = $cacheFile;
>>>>>>> Define archive builders and formatters
return $this;
}
@@ -499,7 +779,11 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
public function commit()
{
$this->zip->close();
<<<<<<< HEAD
$result = $this->zip->open($this->getCacheFile());
=======
$result = $this->zip->open($this->getZipCacheFile());
>>>>>>> Define archive builders and formatters
if ($result !== true) {
throw new \ErrorException(
@@ -513,12 +797,20 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
}
/**
<<<<<<< HEAD
* @param string $initialString
=======
* @param string $initialString
>>>>>>> Define archive builders and formatters
* @return string
*
* Gives a valid file path for \ZipArchive
*/
<<<<<<< HEAD
public function formatFilePath($initialString)
=======
public function getFilePath($initialString)
>>>>>>> Define archive builders and formatters
{
/**
* Remove the / at the beginning and the end.
@@ -533,27 +825,59 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
if (preg_match("#\/?[^\/]+\/[^/]+\/?#", $initialString)) {
$initialString = "/" . $initialString;
}
<<<<<<< HEAD
=======
>>>>>>> Define archive builders and formatters
return $initialString;
}
/**
<<<<<<< HEAD
* @param string $initialString
=======
* @param string $initialString
>>>>>>> Define archive builders and formatters
* @return string
*
* Gives a valid directory path for \ZipArchive
*/
<<<<<<< HEAD
public function formatDirectoryPath($initialString)
{
$initialString = $this->formatFilePath($initialString);
if ($initialString !== "" && $initialString[0] !== "/") {
=======
public function getDirectoryPath($initialString)
{
$initialString = $this->getFilePath($initialString);
if ($initialString[0] !== "/") {
>>>>>>> Define archive builders and formatters
$initialString = "/" . $initialString;
}
return $initialString . "/";
}
<<<<<<< HEAD
=======
public function throwFileNotFound($file)
{
throw new FileNotFoundException(
$this->getTranslator()
->trans(
"The file %file is missing or is not readable",
[
"%file" => $file,
]
)
);
}
>>>>>>> Define archive builders and formatters
/**
* @return string
*
@@ -595,10 +919,43 @@ class ZipArchiveBuilder extends AbstractArchiveBuilder
}
/**
<<<<<<< HEAD
=======
* @return Tlog
*/
public function getLogger()
{
return $this->logger;
}
/**
* @return Translator
*/
public function getTranslator()
{
return $this->translator;
}
/**
>>>>>>> Define archive builders and formatters
* @return \ZipArchive
*/
public function getRawZipArchive()
{
return $this->zip;
}
<<<<<<< HEAD
}
=======
public function getZipCacheFile()
{
return $this->zip_cache_file;
}
public function getCacheDir()
{
return $this->cache_dir;
}
}
>>>>>>> Define archive builders and formatters

View File

@@ -0,0 +1,23 @@
<?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 ZipArchiveException
* @package Thelia\Core\FileFormat\Archive\ArchiveBuilder
* @author Benjamin Perche <bperche@openstudio.fr>
*/
class ZipArchiveException extends \ErrorException
{
}

View File

@@ -17,11 +17,16 @@ namespace Thelia\Core\FileFormat\Archive;
* @package Thelia\Core\FileFormat\Archive
* @author Benjamin Perche <bperche@openstudio.fr>
*
<<<<<<< HEAD
* This interface defines the methods that an archive builder must have.
=======
* This interface defines the methods that an archive creator must have.
>>>>>>> Define archive builders and formatters
*/
interface ArchiveBuilderInterface
{
/**
<<<<<<< HEAD
* @param string $filePath It is the path to access the file.
* @param string $directoryInArchive This is the directory where it will be stored in the archive
* @param null|string $name The name of the file in the archive. if it null or empty, it keeps the same name
@@ -30,6 +35,15 @@ interface ArchiveBuilderInterface
* @throws \Thelia\Exception\FileNotFoundException
* @throws \Thelia\Exception\FileNotReadableException
* @throws \ErrorException
=======
* @param string $filePath It is the path to access the file.
* @param string $directoryInArchive This is the directory where it will be stored in the archive
* @param null|string $name The name of the file in the archive. if it null or empty, it keeps the same name
* @param bool $isOnline
* @return $this
* @throws \Thelia\Exception\FileNotFoundException
* @throws \Thelia\Exception\FileNotReadableException
>>>>>>> Define archive builders and formatters
*
* This methods adds a file in the archive.
* If the file is local, $isOnline must be false,
@@ -38,6 +52,7 @@ interface ArchiveBuilderInterface
public function addFile($filePath, $directoryInArchive = "/", $name = null, $isOnline = false);
/**
<<<<<<< HEAD
* @param $content
* @param $name
* @param string $directoryInArchive
@@ -63,12 +78,18 @@ interface ArchiveBuilderInterface
* @return $this
* @throws \Thelia\Exception\FileNotFoundException
* @throws \ErrorException
=======
* @param $pathInArchive
* @return $this
* @throws \Thelia\Exception\FileNotFoundException
>>>>>>> Define archive builders and formatters
*
* This method deletes a file in the archive
*/
public function deleteFile($pathInArchive);
/**
<<<<<<< HEAD
* @param $directoryPath
* @return $this
* @throws \ErrorException
@@ -79,22 +100,36 @@ interface ArchiveBuilderInterface
/**
* @params string $filename
=======
>>>>>>> Define archive builders and formatters
* @return \Thelia\Core\HttpFoundation\Response
*
* This method return an instance of a Response with the archive as content.
*/
<<<<<<< HEAD
public function buildArchiveResponse($filename);
/**
* @param string $pathToArchive
* @param bool $isOnline
=======
public function buildArchiveResponse();
/**
* @param string $pathToArchive
* @param bool $isOnline
>>>>>>> Define archive builders and formatters
* @return $this
* @throws \Thelia\Exception\FileNotFoundException
* @throws \Thelia\Exception\HttpUrlException
*
* Loads an archive
*/
<<<<<<< HEAD
public function loadArchive($pathToArchive, $isOnline = false);
=======
public static function loadArchive($pathToArchive, $environment, $isOnline = false);
>>>>>>> Define archive builders and formatters
/**
* @param $pathToFile
@@ -105,11 +140,27 @@ interface ArchiveBuilderInterface
public function hasFile($pathToFile);
/**
<<<<<<< HEAD
* @param string $directory
=======
* @param string $directory
>>>>>>> Define archive builders and formatters
* @return bool
*
* Check if the archive has a directory
*/
public function hasDirectory($directory);
<<<<<<< HEAD
}
=======
/**
* @param string $environment
* @return $this
*
* Sets the execution environment of the Kernel,
* used to know which cache is used.
*/
public function setEnvironment($environment);
}
>>>>>>> Define archive builders and formatters

View File

@@ -20,8 +20,12 @@ use Thelia\Core\Translation\Translator;
*/
class ArchiveBuilderManager
{
<<<<<<< HEAD
/** @var array */
protected $archiveBuilders = array();
=======
protected $archiveCreators = array();
>>>>>>> Define archive builders and formatters
protected $environment;
@@ -30,6 +34,7 @@ class ArchiveBuilderManager
$this->environment = $environment;
}
/**
<<<<<<< HEAD
* @param AbstractArchiveBuilder $archiveBuilder
* @return $this
*/
@@ -39,6 +44,17 @@ class ArchiveBuilderManager
$archiveBuilder->setEnvironment($this->environment);
$this->archiveBuilders[$archiveBuilder->getName()] = $archiveBuilder;
=======
* @param AbstractArchiveBuilder $archiveCreator
* @return $this
*/
public function add(AbstractArchiveBuilder $archiveCreator)
{
if (null !== $archiveCreator) {
$archiveCreator->setEnvironment($this->environment);
$this->archiveCreators[$archiveCreator->getName()] = $archiveCreator;
>>>>>>> Define archive builders and formatters
}
return $this;
@@ -51,16 +67,32 @@ class ArchiveBuilderManager
*/
public function delete($name)
{
<<<<<<< HEAD
if (!array_key_exists($name, $this->archiveBuilders)) {
$this->throwOutOfBounds($name);
}
unset($this->archiveBuilders[$name]);
=======
if (!array_key_exists($name, $this->archiveCreators)) {
throw new \OutOfBoundsException(
Translator::getInstance()->trans(
"The archive creator %name doesn't exist",
[
"%name" => $name
]
)
);
}
unset($this->archiveCreators[$name]);
>>>>>>> Define archive builders and formatters
return $this;
}
/**
<<<<<<< HEAD
* @return array
*/
public function getAll()
@@ -104,3 +136,12 @@ class ArchiveBuilderManager
);
}
}
=======
* @return array[AbstractArchiveBuilder]
*/
public function getAll()
{
return $this->archiveCreators;
}
}
>>>>>>> Define archive builders and formatters

View File

@@ -55,4 +55,8 @@ interface FormatInterface
* return "application/json";
*/
public function getMimeType();
<<<<<<< HEAD
}
=======
}
>>>>>>> Define archive builders and formatters

View File

@@ -0,0 +1,41 @@
<?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\Formatter;
use Thelia\Core\FileFormat\FormatInterface;
/**
* Class AbstractFormatter
* @package Thelia\Core\FileFormat\Formatter
* @author Benjamin Perche <bperche@openstudio.fr>
*/
abstract class AbstractFormatter implements FormatInterface
{
/**
* @param array $data
* @return mixed
*
* Encodes an array to the desired format.
* $data array only contains array and scalar data.
*/
abstract public function encode(array $data);
/**
* @param $data
* @return array
* @throws \Thelia\Core\FileFormat\Formatter\Exception\BadFormattedStringException
*
* this method must do exactly the opposite of encode and return
* an array composed of array and scalar data.
*/
abstract public function decode($data);
}

View File

@@ -0,0 +1,23 @@
<?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\Formatter\Exception;
/**
* Class BadFormattedStringException
* @package Thelia\Core\FileFormat\Formatter\Exception
* @author Benjamin Perche <bperche@openstudio.fr>
*/
class BadFormattedStringException extends \ErrorException
{
}

View File

@@ -0,0 +1,69 @@
<?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\Formatter;
use Thelia\Core\Translation\Translator;
/**
* Class FormatterManager
* @package Thelia\Core\FileFormat\Formatter
* @author Benjamin Perche <bperche@openstudio.fr>
*/
class FormatterManager
{
protected $formatters = array();
/**
* @param $archiveCreator
* @return $this
*/
public function add(AbstractFormatter $formatter)
{
if (null !== $formatter) {
$this->formatters[$formatter->getName()] = $formatter;
}
return $this;
}
/**
* @param $name
* @return $this
* @throws \OutOfBoundsException
*/
public function delete($name)
{
if (!array_key_exists($name, $this->formatters)) {
throw new \OutOfBoundsException(
Translator::getInstance()->trans(
"The formatter %name doesn't exist",
[
"%name" => $name
]
)
);
}
unset($this->formatters[$name]);
return $this;
}
/**
* @return array[AbstractFormatter]
*/
public function getAll()
{
return $this->formatters;
}
}

View File

@@ -40,65 +40,115 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
$this->zip = new ZipArchiveBuilder();
<<<<<<< HEAD
$this->zip->setEnvironment("dev");
$this->loadedZip = $this->zip->loadArchive(
__DIR__ . DS . "TestResources/well_formatted.zip"
=======
$this->loadedZip = $this->zip->loadArchive(
__DIR__ . DS . "TestResources/well_formatted.zip",
"dev"
>>>>>>> Define archive builders and formatters
);
}
/**
* This method formats a path to be compatible with \ZipArchive
<<<<<<< HEAD
*/
public function testFormatFilePath()
{
$this->assertEquals(
"foo",
$this->zip->formatFilePath("foo")
=======
*
*
*/
public function testGetFilePath()
{
$this->assertEquals(
"foo",
$this->zip->getFilePath("foo")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"foo",
<<<<<<< HEAD
$this->zip->formatFilePath("/foo")
=======
$this->zip->getFilePath("/foo")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"foo",
<<<<<<< HEAD
$this->zip->formatFilePath("foo/")
=======
$this->zip->getFilePath("foo/")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"foo",
<<<<<<< HEAD
$this->zip->formatFilePath("/foo/")
=======
$this->zip->getFilePath("/foo/")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar",
<<<<<<< HEAD
$this->zip->formatFilePath("foo/bar")
=======
$this->zip->getFilePath("foo/bar")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar",
<<<<<<< HEAD
$this->zip->formatFilePath("/foo/bar")
=======
$this->zip->getFilePath("/foo/bar")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar",
<<<<<<< HEAD
$this->zip->formatFilePath("/foo//bar/")
=======
$this->zip->getFilePath("/foo//bar/")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar",
<<<<<<< HEAD
$this->zip->formatFilePath("/foo/bar/")
=======
$this->zip->getFilePath("/foo/bar/")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar/baz",
<<<<<<< HEAD
$this->zip->formatFilePath("foo/bar/baz")
=======
$this->zip->getFilePath("foo/bar/baz")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar/baz",
<<<<<<< HEAD
$this->zip->formatFilePath("//foo/bar///baz/")
);
}
@@ -108,58 +158,110 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(
"/foo/",
$this->zip->formatDirectoryPath("foo")
=======
$this->zip->getFilePath("//foo/bar///baz/")
);
}
public function testGetDirectoryPath()
{
$this->assertEquals(
"/foo/",
$this->zip->getDirectoryPath("foo")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/",
<<<<<<< HEAD
$this->zip->formatDirectoryPath("/foo")
=======
$this->zip->getDirectoryPath("/foo")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/",
<<<<<<< HEAD
$this->zip->formatDirectoryPath("foo/")
=======
$this->zip->getDirectoryPath("foo/")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/",
<<<<<<< HEAD
$this->zip->formatDirectoryPath("/foo/")
=======
$this->zip->getDirectoryPath("/foo/")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar/",
<<<<<<< HEAD
$this->zip->formatDirectoryPath("foo/bar")
=======
$this->zip->getDirectoryPath("foo/bar")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar/",
<<<<<<< HEAD
$this->zip->formatDirectoryPath("/foo/bar")
=======
$this->zip->getDirectoryPath("/foo/bar")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar/",
<<<<<<< HEAD
$this->zip->formatDirectoryPath("/foo//bar/")
=======
$this->zip->getDirectoryPath("/foo//bar/")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar/",
<<<<<<< HEAD
$this->zip->formatDirectoryPath("/foo/bar/")
=======
$this->zip->getDirectoryPath("/foo/bar/")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar/baz/",
<<<<<<< HEAD
$this->zip->formatDirectoryPath("foo/bar/baz")
=======
$this->zip->getDirectoryPath("foo/bar/baz")
>>>>>>> Define archive builders and formatters
);
$this->assertEquals(
"/foo/bar/baz/",
<<<<<<< HEAD
$this->zip->formatDirectoryPath("//foo/bar///baz/")
=======
$this->zip->getDirectoryPath("//foo/bar///baz/")
>>>>>>> Define archive builders and formatters
);
}
public function testLoadValidZip()
{
$loadedZip = $this->zip->loadArchive(
<<<<<<< HEAD
__DIR__ . DS . "TestResources/well_formatted.zip"
=======
__DIR__ . DS . "TestResources/well_formatted.zip",
"dev"
>>>>>>> Define archive builders and formatters
);
$this->assertInstanceOf(
@@ -169,13 +271,22 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
}
/**
<<<<<<< HEAD
* @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\ZipArchiveException
=======
* @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveException
>>>>>>> Define archive builders and formatters
* @expectedExceptionMessage [Zip Error] The file is not a zip archive
*/
public function testLoadNotValidZip()
{
$this->zip->loadArchive(
<<<<<<< HEAD
__DIR__ . DS . "TestResources/bad_formatted.zip"
=======
__DIR__ . DS . "TestResources/bad_formatted.zip",
"dev"
>>>>>>> Define archive builders and formatters
);
}
@@ -185,31 +296,56 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
public function testLoadNotExistingFile()
{
$this->zip->loadArchive(
<<<<<<< HEAD
__DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip"
=======
__DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip",
"dev"
>>>>>>> Define archive builders and formatters
);
}
public function testLoadOnlineAvailableAndValidFile()
{
<<<<<<< HEAD
$this->zip->setFileDownloader(FakeFileDownloader::getInstance());
$this->zip->loadArchive(
__DIR__ . DS . "TestResources/well_formatted.zip",
true
=======
$this->zip->loadArchive(
__DIR__ . DS . "TestResources/well_formatted.zip",
"dev",
true,
FakeFileDownloader::getInstance()
>>>>>>> Define archive builders and formatters
);
}
/**
<<<<<<< HEAD
* @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\Exception\ZipArchiveException
=======
* @expectedException \Thelia\Core\FileFormat\Archive\ArchiveBuilder\ZipArchiveException
>>>>>>> Define archive builders and formatters
* @expectedExceptionMessage [Zip Error] The file is not a zip archive
*/
public function testLoadOnlineAvailableAndNotValidFile()
{
<<<<<<< HEAD
$this->zip->setFileDownloader(FakeFileDownloader::getInstance());
$this->zip->loadArchive(
__DIR__ . DS . "TestResources/bad_formatted.zip",
true
=======
$this->zip->loadArchive(
__DIR__ . DS . "TestResources/bad_formatted.zip",
"dev",
true,
FakeFileDownloader::getInstance()
>>>>>>> Define archive builders and formatters
);
}
@@ -218,11 +354,19 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
*/
public function testLoadOnlineNotExistingFile()
{
<<<<<<< HEAD
$this->zip->setFileDownloader(FakeFileDownloader::getInstance());
$this->zip->loadArchive(
__DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip",
true
=======
$this->zip->loadArchive(
__DIR__ . DS . "TestResources/this_file_doesn_t_exist.zip",
"dev",
true,
FakeFileDownloader::getInstance()
>>>>>>> Define archive builders and formatters
);
}
@@ -325,7 +469,11 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
public function testBuildArchiveResponse()
{
$loadedArchiveResponse = $this->loadedZip
<<<<<<< HEAD
->buildArchiveResponse("test")
=======
->buildArchiveResponse()
>>>>>>> Define archive builders and formatters
;
$loadedArchiveResponseContent = $loadedArchiveResponse->getContent();
@@ -337,6 +485,7 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
$loadedArchiveResponseContent
);
}
<<<<<<< HEAD
public function testAddValidFileFromString()
{
@@ -396,3 +545,6 @@ class ZipArchiveBuilderTest extends \PHPUnit_Framework_TestCase
}
}
=======
}
>>>>>>> Define archive builders and formatters

View File

@@ -72,4 +72,8 @@ class ArchiveBuilderManagerTest extends \PHPUnit_Framework_TestCase
$this->manager->delete("foo");
}
<<<<<<< HEAD
}
=======
}
>>>>>>> Define archive builders and formatters

View File

@@ -0,0 +1,74 @@
<?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\Formatter;
use Symfony\Component\DependencyInjection\Container;
use Thelia\Core\FileFormat\Formatter\AbstractFormatter;
use Thelia\Core\FileFormat\Formatter\FormatterManager;
use Thelia\Core\Translation\Translator;
/**
* Class FormatterManagerTest
* @package Thelia\Tests\FileFormat\Formatter
* @author Benjamin Perche <bperche@openstudio.fr>
*/
class FormatterManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var FormatterManager
*/
protected $manager;
public function setUp()
{
new Translator(
new Container()
);
$this->manager = new FormatterManager();
}
public function testAddFormatter()
{
/** @var AbstractFormatter $instance */
$instance = $this->getMock("Thelia\\Core\\FileFormat\\Formatter\\AbstractFormatter");
$this->manager->add($instance);
$archiveBuilders = $this->manager->getAll();
$this->assertTrue(
array_key_exists($instance->getName(), $archiveBuilders)
);
}
public function testDeleteFormatter()
{
/** @var AbstractFormatter $instance */
$instance = $this->getMock("Thelia\\Core\\FileFormat\\Formatter\\AbstractFormatter");
$this->manager->add($instance);
$this->manager->delete($instance->getName());
$this->assertTrue(
count($this->manager->getAll()) === 0
);
}
/**
* @expectedException \OutOfBoundsException
*/
public function testDeleteNotExistingFormatter()
{
$this->manager->delete("foo");
}
}

View File

@@ -22,8 +22,13 @@ use Thelia\Tools\FileDownload\FileDownloader;
class FakeFileDownloader extends FileDownloader
{
/**
<<<<<<< HEAD
* @param string $url
* @param string $pathToStore
=======
* @param string $url
* @param string $pathToStore
>>>>>>> Define archive builders and formatters
* @throws \Thelia\Exception\FileNotFoundException
* @throws \ErrorException
* @throws \HttpUrlException
@@ -41,4 +46,8 @@ class FakeFileDownloader extends FileDownloader
}
}
<<<<<<< HEAD
}
=======
}
>>>>>>> Define archive builders and formatters

View File

@@ -14,7 +14,11 @@ namespace Thelia\Tests\Type;
use Symfony\Component\DependencyInjection\Container;
use Thelia\Core\Translation\Translator;
use Thelia\Log\Tlog;
<<<<<<< HEAD
use Thelia\Tools\FileDownload\FileDownloader;
=======
use Thelia\Tools\FileDownloader;
>>>>>>> Define archive builders and formatters
/**
* Class FileDownloaderTest
@@ -60,4 +64,8 @@ class FileDownloaderTest extends \PHPUnit_Framework_TestCase
{
$this->downloader->download("https://github.com/thelia/thelia", "php://temp");
}
<<<<<<< HEAD
}
=======
}
>>>>>>> Define archive builders and formatters

View File

@@ -787,7 +787,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase
->getMock();
$stubUploadedFile = $this->getMockBuilder('\Symfony\Component\HttpFoundation\File\UploadedFile')
->setConstructorArgs([__DIR__ . '/fixtures/test.xml', 'test.xml'])
->disableOriginalConstructor()
->getMock();
$stubUploadedFile->expects($this->any())
->method('getClientOriginalExtension')
@@ -814,7 +814,7 @@ class FileManagerTest extends \PHPUnit_Framework_TestCase
->getMock();
$stubUploadedFile = $this->getMockBuilder('\Symfony\Component\HttpFoundation\File\UploadedFile')
->setConstructorArgs([__DIR__ . '/fixtures/test.xml', 'test.xml'])
->disableOriginalConstructor()
->getMock();
$stubUploadedFile->expects($this->any())
->method('getClientOriginalExtension')

View File

@@ -17,7 +17,10 @@ use Thelia\Core\Translation\Translator as TheliaTranslator;
use Thelia\Exception\FileNotFoundException;
use Thelia\Exception\HttpUrlException;
use Thelia\Log\Tlog;
<<<<<<< HEAD
use Thelia\Tools\URL;
=======
>>>>>>> Define archive builders and formatters
/**
* Class FileDownloader
@@ -45,8 +48,13 @@ class FileDownloader implements FileDownloaderInterface
}
/**
<<<<<<< HEAD
* @param string $url
* @param string $pathToStore
=======
* @param string $url
* @param string $pathToStore
>>>>>>> Define archive builders and formatters
* @throws \Thelia\Exception\FileNotFoundException
* @throws \ErrorException
* @throws \HttpUrlException
@@ -125,7 +133,11 @@ class FileDownloader implements FileDownloaderInterface
*/
$file = @fopen($pathToStore, "w");
<<<<<<< HEAD
if ($file === false) {
=======
if($file === false) {
>>>>>>> Define archive builders and formatters
$translatedErrorMessage = $this->translator->trans(
"Failed to open a writing stream on the file: %file",
[
@@ -140,4 +152,8 @@ class FileDownloader implements FileDownloaderInterface
fputs($file, $response);
fclose($file);
}
<<<<<<< HEAD
}
=======
}
>>>>>>> Define archive builders and formatters

View File

@@ -35,7 +35,11 @@ trait FileDownloaderAwareTrait
}
/**
<<<<<<< HEAD
* @param FileDownloaderInterface $fileDownloader
=======
* @param FileDownloaderInterface $fileDownloader
>>>>>>> Define archive builders and formatters
* @return $this
*/
public function setFileDownloader(FileDownloaderInterface $fileDownloader)
@@ -44,4 +48,8 @@ trait FileDownloaderAwareTrait
return $this;
}
<<<<<<< HEAD
}
=======
}
>>>>>>> Define archive builders and formatters

View File

@@ -14,6 +14,10 @@ namespace Thelia\Tools\FileDownload;
use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Translator;
<<<<<<< HEAD
=======
>>>>>>> Define archive builders and formatters
/**
* Class FileDownloader
* @package Thelia\Tools\FileDownload
@@ -22,8 +26,13 @@ use Symfony\Component\Translation\Translator;
interface FileDownloaderInterface
{
/**
<<<<<<< HEAD
* @param string $url
* @param string $pathToStore
=======
* @param string $url
* @param string $pathToStore
>>>>>>> Define archive builders and formatters
* @throws \Thelia\Exception\FileNotFoundException
* @throws \ErrorException
* @throws \HttpUrlException
@@ -40,4 +49,8 @@ interface FileDownloaderInterface
* Returns an hydrated instance
*/
public static function getInstance();
<<<<<<< HEAD
}
=======
}
>>>>>>> Define archive builders and formatters

View File

@@ -315,7 +315,11 @@ class URL
$clean;
}
<<<<<<< HEAD
public static function checkUrl($url, array $protocols = ["http", "https"])
=======
public function checkUrl($url, array $protocols = ["http", "https"])
>>>>>>> Define archive builders and formatters
{
$pattern = sprintf(UrlValidator::PATTERN, implode('|', $protocols));