Correction after some tests

This commit is contained in:
Shikiryu 2015-07-10 23:13:46 +02:00
parent 05dd396eac
commit 6eaf2f0176
9 changed files with 99 additions and 59 deletions

View File

@ -2,43 +2,76 @@
namespace Shikiryu\Backup;
use Shikiryu\Backup\Backup\BackupAbstract;
use Shikiryu\Backup\Transport\TransportAbstract;
use Shikiryu\Backup\Backup\Factory as BackupFactory;
use Shikiryu\Backup\Transport\Factory as TransportFactory;
class Scenario {
/* @var $backup BackupAbstract */
private $backup;
private $to;
/* @var $to TransportAbstract */
private $transport;
public static function register()
{
spl_autoload_register(array(__CLASS__, 'autoload'));
}
public static function autoload($class)
{
// Autoload only "sub-namespaced" class
if (strpos($class, __NAMESPACE__.'\\') === 0)
{
// Delete current namespace from class one
$relative_NS = str_replace(__NAMESPACE__, '', $class);
// Translate namespace structure into directory structure
$translated_path = str_replace('\\', '/', $relative_NS);
// Load class suffixed by ".class.php"
require __DIR__ . '/' . $translated_path . '.php';
}
}
/**
* @param array $scenario
*/
private function __construct(array $scenario)
{
$this->backup = BackupFactory::build($scenario['backup']);
$this->to = TransportFactory::build($scenario['to']);
define('TEMP_DIR', __DIR__.'/../temp/');
$this->backup = BackupFactory::build($scenario['backup']);
$this->transport = TransportFactory::build($this->backup, $scenario['transport']);
}
public function send()
{
$this->transport->send();
}
public static function launch($scenario)
{
// add autoloader
static::register();
// check the given scenario
if (is_readable($scenario)) {
$scenario = json_decode(file_get_contents($scenario), true);
if (static::isValid($scenario)) {
if (!is_null($scenario) && static::isValid($scenario)) {
$scenario = new self($scenario);
$scenario->send();
}
throw new \Exception('invalid scenario.');
}
throw new \Exception('scenario not found.');
}
public static function isValid(\StdClass $scenario)
public static function isValid(array $scenario)
{
return
isset($scenario->backup) &&
count($scenario->backup) === 1 &&
isset($scenario->to) &&
count($scenario->to) === 1;
isset($scenario['backup']) &&
count($scenario['backup']) === 1 &&
isset($scenario['transport']) &&
count($scenario['transport']) === 1;
}
}

View File

@ -1,26 +0,0 @@
<?php
namespace Shikiryu\Backup\Transport;
use Shikiryu\Backup\Backup\BackupAbstract;
abstract class TransportAbstract
{
protected $backup;
protected $config;
public function __construct(BackupAbstract $backup)
{
$config = parse_ini_file(dirname(__FILE__).'/../config/config.ini');
$classname = get_class($this);
$type = substr($classname, strrpos($classname, '_')+1);
$this->config = $config[ucfirst(strtolower($type))];
$this->backup = $backup;
}
/**
* @return bool
*/
public abstract function send();
}

View File

@ -2,6 +2,8 @@
namespace Shikiryu\Backup\Transport;
use Shikiryu\Backup\Backup\BackupAbstract;
class Email extends TransportAbstract
{
@ -141,10 +143,10 @@ class Email extends TransportAbstract
}
/**
* @param Shikiryu_Backup_Abstract $backup
* @param BackupAbstract $backup
*/
public function __construct(Shikiryu_Backup_Abstract $backup) {
parent::__construct($backup);
public function __construct(BackupAbstract $backup, array $config) {
parent::__construct($backup, $config);
$this->setFiles($this->backup->getFilesToBackup());
$this->setStreams($this->backup->getStreamsToBackup());
$this->to = $this->config['to'];
@ -233,9 +235,9 @@ class Email extends TransportAbstract
public function send() {
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip = new \ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open(dirname(__FILE__).'/bu/'.$zip_name, ZIPARCHIVE::CREATE)==TRUE) {
if($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::CREATE)==TRUE) {
if(!empty($this->files)) {
foreach($this->files as $file)
{
@ -245,7 +247,7 @@ class Email extends TransportAbstract
$zip->close();
}
$this->files = array(dirname(__FILE__).'/bu/'.$zip_name=>dirname(__FILE__).'/bu/'.$zip_name);
$this->files = array(TEMP_DIR.$zip_name=>dirname(__FILE__).'/bu/'.$zip_name);
$random_hash = md5(date('r', time()));
$headers = "From: " . $this->from . "\r\nReply-To: " . $this->from;
@ -259,8 +261,9 @@ Content-Transfer-Encoding: 8bit
" . $this->message . "\r\n";
if(!empty($this->files))
foreach($this->files as $file=>$name) {
$name = end(explode('/', $name));
foreach($this->files as $file => $name) {
$name = explode('/', $name);
$name = end($name);
$output .= "
--$random_hash
Content-Type: " . self::getMimeType($file) . "; name=" . $name . "

View File

@ -2,20 +2,22 @@
namespace Shikiryu\Backup\Transport;
use Shikiryu\Backup\Backup\BackupAbstract;
class Factory
{
/**
* @param $type
* @param Shikiryu_Backup_Abstract $backup
*
* @return bool
* @param BackupAbstract $backup
* @param array $config
*
* @return TransportAbstract|null
*/
public static function build(array $config)
public static function build(BackupAbstract $backup, array $config)
{
$class = array_keys($config)[0];
$class = __NAMESPACE__.'\\'.array_keys($config)[0];
if (class_exists($class)) {
/* @var $instance BackupAbstract */
return new $class(array_values($config));
return new $class($backup, array_values($config)[0]);
}
return null;
}

View File

@ -0,0 +1,23 @@
<?php
namespace Shikiryu\Backup\Transport;
use Shikiryu\Backup\Backup\BackupAbstract;
abstract class TransportAbstract
{
protected $backup;
protected $config;
public function __construct(BackupAbstract $backup, array $config)
{
$this->config = $config;
$this->backup = $backup;
}
/**
* @return bool
*/
public abstract function send();
}

View File

@ -6,14 +6,14 @@ class Factory
{
/**
* @param array $config
*
* @return BackupAbstract
*/
public static function build(array $config)
{
$class = array_keys($config)[0];
$class = __NAMESPACE__.'\\'.array_keys($config)[0];
if (class_exists($class)) {
/* @var $instance BackupAbstract */
return new $class(array_values($config));
return new $class(array_values($config)[0]);
}
return null;
}

View File

@ -5,11 +5,16 @@ class Files extends BackupAbstract
{
/**
* @param array $filesToBackup
* @param array $config
* @throws \Exception
*/
function __construct($filesToBackup = array())
function __construct($config = array())
{
parent::__construct();
if (!isset($config['files'])) {
throw new \Exception('Files needs a "files" configuration.');
}
$filesToBackup = $config['files'];
if(!empty($filesToBackup) && is_array($filesToBackup)){
$names = array_map("basename",$filesToBackup);
$this->_filesToBackup = array_combine($filesToBackup,$names);

View File

@ -1,4 +1,4 @@
[{
{
"backup": {
"Files": {
"files": [
@ -13,9 +13,9 @@
"db" : "mysql database"
}
},
"to": {
"transport": {
"Email": {
"to" : "recipient@example.net;recipient2@example.net", // TODO manage more than one recipient
"to" : "recipient@example.net;recipient2@example.net",
"from" : "<name>from@example.net",
"encoding" : "UTF-8",
"subject" : "[Example.net] backup of the day",
@ -28,4 +28,4 @@
"folder" : "/folder"
}
}
}]
}