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 1d526b437f
commit d82fb479cf
22 changed files with 2754 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
<?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\Tools\FileDownload;
use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Translator;
use Thelia\Core\Translation\Translator as TheliaTranslator;
use Thelia\Exception\FileNotFoundException;
use Thelia\Exception\HttpUrlException;
use Thelia\Log\Tlog;
/**
* Class FileDownloader
* @package Thelia\Tools\FileDownload
* @author Benjamin Perche <bperche@openstudio.fr>
*/
class FileDownloader implements FileDownloaderInterface
{
/** @var LoggerInterface */
protected $logger;
/** @var Translator */
protected $translator;
public function __construct(LoggerInterface $logger, Translator $translator)
{
$this->logger = $logger;
$this->translator = $translator;
}
public static function getInstance()
{
return new static(Tlog::getInstance(), TheliaTranslator::getInstance());
}
/**
* @param string $url
* @param string $pathToStore
* @throws \Thelia\Exception\FileNotFoundException
* @throws \ErrorException
* @throws \HttpUrlException
*
* Downloads the file $url in $pathToStore
*/
public function download($url, $pathToStore)
{
if (!URL::checkUrl($url)) {
/**
* The URL is not valid
*/
throw new HttpUrlException(
$this->translator->trans(
"Tried to download a file, but the URL was not valid: %url",
[
"%url" => $url
]
)
);
}
/**
* Try to get the file if it is online
*/
$con = curl_init($url);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($con);
$errno = curl_errno($con);
$curlErrorMessage = curl_error($con);
$httpCode = curl_getinfo($con, CURLINFO_HTTP_CODE);
curl_close($con);
if (false === $response || $errno !== 0 ||
($httpCode != "200" && $httpCode != "204")
) {
/**
* The server is down ? The file doesn't exist ? Anything else ?
*/
$errorMessage = $this->translator->trans(
"cURL errno %errno, http code %http_code on link \"%path\": %error",
[
"%errno" => $errno,
"%path" => $url,
"%error" => $curlErrorMessage,
"%http_code" => $httpCode,
]
);
$this->logger
->error($errorMessage)
;
throw new FileNotFoundException($errorMessage);
}
/**
* Inform that you've downloaded a file
*/
$this->logger
->info(
$this->translator->trans(
"The file %path has been successfully downloaded",
[
"%path" => $url
]
)
)
;
/**
* Then try to write it on the disk
*/
$file = @fopen($pathToStore, "w");
if($file === false) {
$translatedErrorMessage = $this->translator->trans(
"Failed to open a writing stream on the file: %file",
[
"%file" => $pathToStore
]
);
$this->logger->error($translatedErrorMessage);
throw new \ErrorException($translatedErrorMessage);
}
fputs($file, $response);
fclose($file);
}
}

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\Tools\FileDownload;
/**
* Trait FileDownloaderAwareTrait
* @package Thelia\Tools\FileDownload
* @author Benjamin Perche <bperche@openstudio.fr>
*/
trait FileDownloaderAwareTrait
{
/** @var FileDownloaderInterface */
protected $fileDownloader;
/**
* @return FileDownloaderInterface
*/
public function getFileDownloader()
{
if (!$this->fileDownloader instanceof FileDownloaderInterface) {
$this->fileDownloader = FileDownloader::getInstance();
}
return $this->fileDownloader;
}
/**
* @param FileDownloaderInterface $fileDownloader
* @return $this
*/
public function setFileDownloader(FileDownloaderInterface $fileDownloader)
{
$this->fileDownloader = $fileDownloader;
return $this;
}
}

View File

@@ -0,0 +1,44 @@
<?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\Tools\FileDownload;
use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Translator;
/**
* Class FileDownloader
* @package Thelia\Tools\FileDownload
* @author Benjamin Perche <bperche@openstudio.fr>
*/
interface FileDownloaderInterface
{
/**
* @param string $url
* @param string $pathToStore
* @throws \Thelia\Exception\FileNotFoundException
* @throws \ErrorException
* @throws \HttpUrlException
*
* Downloads the file $url in $pathToStore
*/
public function download($url, $pathToStore);
public function __construct(LoggerInterface $logger, Translator $translator);
/**
* @return $this
*
* Returns an hydrated instance
*/
public static function getInstance();
}