mirror of
https://github.com/Chouchen/Shikiryu_Backup.git
synced 2021-06-30 16:02:14 +02:00
Correction after some tests
This commit is contained in:
parent
05dd396eac
commit
6eaf2f0176
@ -2,43 +2,76 @@
|
|||||||
|
|
||||||
namespace Shikiryu\Backup;
|
namespace Shikiryu\Backup;
|
||||||
|
|
||||||
|
use Shikiryu\Backup\Backup\BackupAbstract;
|
||||||
|
use Shikiryu\Backup\Transport\TransportAbstract;
|
||||||
use Shikiryu\Backup\Backup\Factory as BackupFactory;
|
use Shikiryu\Backup\Backup\Factory as BackupFactory;
|
||||||
use Shikiryu\Backup\Transport\Factory as TransportFactory;
|
use Shikiryu\Backup\Transport\Factory as TransportFactory;
|
||||||
|
|
||||||
class Scenario {
|
class Scenario {
|
||||||
|
|
||||||
|
/* @var $backup BackupAbstract */
|
||||||
private $backup;
|
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
|
* @param array $scenario
|
||||||
*/
|
*/
|
||||||
private function __construct(array $scenario)
|
private function __construct(array $scenario)
|
||||||
{
|
{
|
||||||
$this->backup = BackupFactory::build($scenario['backup']);
|
define('TEMP_DIR', __DIR__.'/../temp/');
|
||||||
$this->to = TransportFactory::build($scenario['to']);
|
$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)
|
public static function launch($scenario)
|
||||||
{
|
{
|
||||||
|
// add autoloader
|
||||||
|
static::register();
|
||||||
|
|
||||||
|
// check the given scenario
|
||||||
if (is_readable($scenario)) {
|
if (is_readable($scenario)) {
|
||||||
$scenario = json_decode(file_get_contents($scenario), true);
|
$scenario = json_decode(file_get_contents($scenario), true);
|
||||||
if (static::isValid($scenario)) {
|
if (!is_null($scenario) && static::isValid($scenario)) {
|
||||||
$scenario = new self($scenario);
|
$scenario = new self($scenario);
|
||||||
|
$scenario->send();
|
||||||
}
|
}
|
||||||
throw new \Exception('invalid scenario.');
|
throw new \Exception('invalid scenario.');
|
||||||
}
|
}
|
||||||
throw new \Exception('scenario not found.');
|
throw new \Exception('scenario not found.');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function isValid(\StdClass $scenario)
|
public static function isValid(array $scenario)
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
isset($scenario->backup) &&
|
isset($scenario['backup']) &&
|
||||||
count($scenario->backup) === 1 &&
|
count($scenario['backup']) === 1 &&
|
||||||
isset($scenario->to) &&
|
isset($scenario['transport']) &&
|
||||||
count($scenario->to) === 1;
|
count($scenario['transport']) === 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -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();
|
|
||||||
}
|
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Shikiryu\Backup\Transport;
|
namespace Shikiryu\Backup\Transport;
|
||||||
|
|
||||||
|
use Shikiryu\Backup\Backup\BackupAbstract;
|
||||||
|
|
||||||
class Email extends TransportAbstract
|
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) {
|
public function __construct(BackupAbstract $backup, array $config) {
|
||||||
parent::__construct($backup);
|
parent::__construct($backup, $config);
|
||||||
$this->setFiles($this->backup->getFilesToBackup());
|
$this->setFiles($this->backup->getFilesToBackup());
|
||||||
$this->setStreams($this->backup->getStreamsToBackup());
|
$this->setStreams($this->backup->getStreamsToBackup());
|
||||||
$this->to = $this->config['to'];
|
$this->to = $this->config['to'];
|
||||||
@ -233,9 +235,9 @@ class Email extends TransportAbstract
|
|||||||
public function send() {
|
public function send() {
|
||||||
|
|
||||||
// Checking files are selected
|
// Checking files are selected
|
||||||
$zip = new ZipArchive(); // Load zip library
|
$zip = new \ZipArchive(); // Load zip library
|
||||||
$zip_name = time().".zip"; // Zip name
|
$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)) {
|
if(!empty($this->files)) {
|
||||||
foreach($this->files as $file)
|
foreach($this->files as $file)
|
||||||
{
|
{
|
||||||
@ -245,7 +247,7 @@ class Email extends TransportAbstract
|
|||||||
$zip->close();
|
$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()));
|
$random_hash = md5(date('r', time()));
|
||||||
$headers = "From: " . $this->from . "\r\nReply-To: " . $this->from;
|
$headers = "From: " . $this->from . "\r\nReply-To: " . $this->from;
|
||||||
@ -259,8 +261,9 @@ Content-Transfer-Encoding: 8bit
|
|||||||
" . $this->message . "\r\n";
|
" . $this->message . "\r\n";
|
||||||
|
|
||||||
if(!empty($this->files))
|
if(!empty($this->files))
|
||||||
foreach($this->files as $file=>$name) {
|
foreach($this->files as $file => $name) {
|
||||||
$name = end(explode('/', $name));
|
$name = explode('/', $name);
|
||||||
|
$name = end($name);
|
||||||
$output .= "
|
$output .= "
|
||||||
--$random_hash
|
--$random_hash
|
||||||
Content-Type: " . self::getMimeType($file) . "; name=" . $name . "
|
Content-Type: " . self::getMimeType($file) . "; name=" . $name . "
|
||||||
|
@ -2,20 +2,22 @@
|
|||||||
|
|
||||||
namespace Shikiryu\Backup\Transport;
|
namespace Shikiryu\Backup\Transport;
|
||||||
|
|
||||||
|
use Shikiryu\Backup\Backup\BackupAbstract;
|
||||||
|
|
||||||
class Factory
|
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)) {
|
if (class_exists($class)) {
|
||||||
/* @var $instance BackupAbstract */
|
return new $class($backup, array_values($config)[0]);
|
||||||
return new $class(array_values($config));
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
23
app/code/Transport/TransportAbstract.php
Normal file
23
app/code/Transport/TransportAbstract.php
Normal 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();
|
||||||
|
}
|
@ -6,14 +6,14 @@ class Factory
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param array $config
|
* @param array $config
|
||||||
|
*
|
||||||
* @return BackupAbstract
|
* @return BackupAbstract
|
||||||
*/
|
*/
|
||||||
public static function build(array $config)
|
public static function build(array $config)
|
||||||
{
|
{
|
||||||
$class = array_keys($config)[0];
|
$class = __NAMESPACE__.'\\'.array_keys($config)[0];
|
||||||
if (class_exists($class)) {
|
if (class_exists($class)) {
|
||||||
/* @var $instance BackupAbstract */
|
return new $class(array_values($config)[0]);
|
||||||
return new $class(array_values($config));
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -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();
|
parent::__construct();
|
||||||
|
if (!isset($config['files'])) {
|
||||||
|
throw new \Exception('Files needs a "files" configuration.');
|
||||||
|
}
|
||||||
|
$filesToBackup = $config['files'];
|
||||||
if(!empty($filesToBackup) && is_array($filesToBackup)){
|
if(!empty($filesToBackup) && is_array($filesToBackup)){
|
||||||
$names = array_map("basename",$filesToBackup);
|
$names = array_map("basename",$filesToBackup);
|
||||||
$this->_filesToBackup = array_combine($filesToBackup,$names);
|
$this->_filesToBackup = array_combine($filesToBackup,$names);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
[{
|
{
|
||||||
"backup": {
|
"backup": {
|
||||||
"Files": {
|
"Files": {
|
||||||
"files": [
|
"files": [
|
||||||
@ -13,9 +13,9 @@
|
|||||||
"db" : "mysql database"
|
"db" : "mysql database"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"to": {
|
"transport": {
|
||||||
"Email": {
|
"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",
|
"from" : "<name>from@example.net",
|
||||||
"encoding" : "UTF-8",
|
"encoding" : "UTF-8",
|
||||||
"subject" : "[Example.net] backup of the day",
|
"subject" : "[Example.net] backup of the day",
|
||||||
@ -28,4 +28,4 @@
|
|||||||
"folder" : "/folder"
|
"folder" : "/folder"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}]
|
}
|
Loading…
Reference in New Issue
Block a user