Going namespaced, going scenario style

This commit is contained in:
Shikiryu 2015-07-10 19:07:14 +02:00
parent dbcd02c2af
commit c593f32378
12 changed files with 134 additions and 31 deletions

View File

@ -1,20 +0,0 @@
<?php
class Shikiryu_Backup_Transport_Factory
{
/**
* @param $type
* @param Shikiryu_Backup_Abstract $backup
*
* @return bool
*/
public static function getAndSend($type, Shikiryu_Backup_Abstract $backup, $args = null) {
$class = sprintf('Shikiryu_Backup_Transport_%s', ucfirst($type));
if (class_exists($class)) {
/* @var $instance Shikiryu_Backup_Transport_Abstract */
$instance = new $class($backup, $args);
return $instance->send();
}
return false;
}
}

44
app/code/Scenario.php Normal file
View File

@ -0,0 +1,44 @@
<?php
namespace Shikiryu\Backup;
use Shikiryu\Backup\Backup\Factory as BackupFactory;
use Shikiryu\Backup\Transport\Factory as TransportFactory;
class Scenario {
private $backup;
private $to;
/**
* @param array $scenario
*/
private function __construct(array $scenario)
{
$this->backup = BackupFactory::build($scenario['backup']);
$this->to = TransportFactory::build($scenario['to']);
}
public static function launch($scenario)
{
if (is_readable($scenario)) {
$scenario = json_decode(file_get_contents($scenario), true);
if (static::isValid($scenario)) {
$scenario = new self($scenario);
}
throw new \Exception('invalid scenario.');
}
throw new \Exception('scenario not found.');
}
public static function isValid(\StdClass $scenario)
{
return
isset($scenario->backup) &&
count($scenario->backup) === 1 &&
isset($scenario->to) &&
count($scenario->to) === 1;
}
}

View File

@ -1,12 +1,16 @@
<?php <?php
abstract class Shikiryu_Backup_Transport_Abstract namespace Shikiryu\Backup\Transport;
use Shikiryu\Backup\Backup\BackupAbstract;
abstract class TransportAbstract
{ {
protected $backup; protected $backup;
protected $config; protected $config;
public function __construct(Shikiryu_Backup_Abstract $backup) public function __construct(BackupAbstract $backup)
{ {
$config = parse_ini_file(dirname(__FILE__).'/../config/config.ini'); $config = parse_ini_file(dirname(__FILE__).'/../config/config.ini');
$classname = get_class($this); $classname = get_class($this);

View File

@ -1,6 +1,8 @@
<?php <?php
class Shikiryu_Backup_Transport_Email extends Shikiryu_Backup_Transport_Abstract namespace Shikiryu\Backup\Transport;
class Email extends TransportAbstract
{ {
private $to; private $to;

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Shikiryu\Backup\Transport;
class Shikiryu_Backup_Transport_Ftp extends Shikiryu_Backup_Transport_Abstract class Ftp extends TransportAbstract
{ {
private $path; private $path;

View File

@ -0,0 +1,22 @@
<?php
namespace Shikiryu\Backup\Transport;
class Factory
{
/**
* @param $type
* @param Shikiryu_Backup_Abstract $backup
*
* @return bool
*/
public static function build(array $config)
{
$class = array_keys($config)[0];
if (class_exists($class)) {
/* @var $instance BackupAbstract */
return new $class(array_values($config));
}
return null;
}
}

View File

@ -1,9 +1,8 @@
<?php <?php
include_once dirname(__FILE__) . '/Transport/Email.php'; namespace Shikiryu\Backup\Backup;
include_once dirname(__FILE__) . '/Transport/FTP.php';
class Shikiryu_Backup_Abstract class BackupAbstract
{ {
protected $_filesToBackup; protected $_filesToBackup;

View File

@ -0,0 +1,20 @@
<?php
namespace Shikiryu\Backup\Backup;
class Factory
{
/**
* @param array $config
* @return BackupAbstract
*/
public static function build(array $config)
{
$class = array_keys($config)[0];
if (class_exists($class)) {
/* @var $instance BackupAbstract */
return new $class(array_values($config));
}
return null;
}
}

View File

@ -1,7 +1,7 @@
<?php <?php
include_once dirname(__FILE__).'/Abstract.php'; namespace Shikiryu\Backup\Backup;
class Shikiryu_Backup_Files extends Shikiryu_Backup_Abstract class Files extends BackupAbstract
{ {
/** /**

View File

@ -1,8 +1,8 @@
<?php <?php
include_once dirname(__FILE__) . '/Abstract.php'; namespace Shikiryu\Backup\Backup;
class Shikiryu_Backup_MYSQL extends Shikiryu_Backup_Abstract class Mysql extends BackupAbstract
{ {
/** /**

View File

@ -0,0 +1,31 @@
[{
"backup": {
"Files": {
"files": [
"/path/to/file1",
"/path/to/file2"
]
},
"Mysql": {
"host" : "mysql address",
"login" : "mysql login",
"pwd" : "mysql password",
"db" : "mysql database"
}
},
"to": {
"Email": {
"to" : "recipient@example.net;recipient2@example.net", // TODO manage more than one recipient
"from" : "<name>from@example.net",
"encoding" : "UTF-8",
"subject" : "[Example.net] backup of the day",
"message" : "Hi,\n\nYou can find your backup in this mail.\n\nRegards,\nMyself."
},
"Ftp": {
"host" : "ftp.domain.com",
"login" : "login",
"password" : "password",
"folder" : "/folder"
}
}
}]

0
app/temp/.gitkeep Normal file
View File