2015-07-10 19:07:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shikiryu\Backup;
|
|
|
|
|
2015-07-10 23:13:46 +02:00
|
|
|
use Shikiryu\Backup\Backup\BackupAbstract;
|
|
|
|
use Shikiryu\Backup\Transport\TransportAbstract;
|
2015-07-10 19:07:14 +02:00
|
|
|
use Shikiryu\Backup\Backup\Factory as BackupFactory;
|
|
|
|
use Shikiryu\Backup\Transport\Factory as TransportFactory;
|
|
|
|
|
2016-07-03 21:54:35 +02:00
|
|
|
class Scenario
|
|
|
|
{
|
2015-07-10 19:07:14 +02:00
|
|
|
|
2015-07-10 23:13:46 +02:00
|
|
|
/* @var $backup BackupAbstract */
|
2015-07-10 19:07:14 +02:00
|
|
|
private $backup;
|
2015-07-10 23:13:46 +02:00
|
|
|
/* @var $to TransportAbstract */
|
|
|
|
private $transport;
|
|
|
|
|
|
|
|
public static function register()
|
|
|
|
{
|
2015-08-17 13:16:28 +02:00
|
|
|
include __DIR__.'/../../vendor/autoload.php';
|
|
|
|
//spl_autoload_register(array(__CLASS__, 'autoload'));
|
2015-07-10 23:13:46 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 13:16:28 +02:00
|
|
|
/*public static function autoload($class)
|
2015-07-10 23:13:46 +02:00
|
|
|
{
|
|
|
|
if (strpos($class, __NAMESPACE__.'\\') === 0)
|
|
|
|
{
|
|
|
|
$relative_NS = str_replace(__NAMESPACE__, '', $class);
|
|
|
|
$translated_path = str_replace('\\', '/', $relative_NS);
|
|
|
|
require __DIR__ . '/' . $translated_path . '.php';
|
|
|
|
}
|
2015-08-17 13:16:28 +02:00
|
|
|
}*/
|
2015-07-10 19:07:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $scenario
|
|
|
|
*/
|
|
|
|
private function __construct(array $scenario)
|
|
|
|
{
|
2015-07-10 23:13:46 +02:00
|
|
|
define('TEMP_DIR', __DIR__.'/../temp/');
|
|
|
|
$this->backup = BackupFactory::build($scenario['backup']);
|
|
|
|
$this->transport = TransportFactory::build($this->backup, $scenario['transport']);
|
2015-07-10 19:07:14 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:02:37 +02:00
|
|
|
/**
|
|
|
|
* check if backup is valid and then launch the transfer
|
|
|
|
*
|
|
|
|
* @see BackupAbstract::isValid
|
|
|
|
* @see TransportAbstract::send
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2015-07-10 23:13:46 +02:00
|
|
|
public function send()
|
|
|
|
{
|
2015-08-18 17:02:37 +02:00
|
|
|
if ($this->backup->isValid()) {
|
|
|
|
$this->transport->send();
|
|
|
|
} else {
|
|
|
|
throw new \Exception("Backup configuration is invalid.");
|
2015-07-11 00:12:31 +02:00
|
|
|
}
|
2015-07-10 23:13:46 +02:00
|
|
|
}
|
2015-07-10 19:07:14 +02:00
|
|
|
|
2015-07-11 00:12:31 +02:00
|
|
|
/**
|
|
|
|
* Launch the whole job
|
|
|
|
*
|
|
|
|
* @param $scenario
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2015-07-10 19:07:14 +02:00
|
|
|
public static function launch($scenario)
|
|
|
|
{
|
2015-07-10 23:13:46 +02:00
|
|
|
// add autoloader
|
|
|
|
static::register();
|
|
|
|
|
2015-07-11 00:12:31 +02:00
|
|
|
$scenario = __DIR__.'/../scenario/'.$scenario;
|
|
|
|
|
2015-07-10 23:13:46 +02:00
|
|
|
// check the given scenario
|
2015-07-10 19:07:14 +02:00
|
|
|
if (is_readable($scenario)) {
|
|
|
|
$scenario = json_decode(file_get_contents($scenario), true);
|
2015-07-10 23:13:46 +02:00
|
|
|
if (!is_null($scenario) && static::isValid($scenario)) {
|
2015-07-11 00:12:31 +02:00
|
|
|
try {
|
|
|
|
$scenario = new self($scenario);
|
|
|
|
$scenario->send();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
exit;
|
2015-07-10 19:07:14 +02:00
|
|
|
}
|
|
|
|
throw new \Exception('invalid scenario.');
|
|
|
|
}
|
|
|
|
throw new \Exception('scenario not found.');
|
|
|
|
}
|
|
|
|
|
2015-07-11 00:12:31 +02:00
|
|
|
/**
|
|
|
|
* Check given scenario validation
|
|
|
|
*
|
|
|
|
* @param array $scenario
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-10 23:13:46 +02:00
|
|
|
public static function isValid(array $scenario)
|
2015-07-10 19:07:14 +02:00
|
|
|
{
|
|
|
|
return
|
2015-07-10 23:13:46 +02:00
|
|
|
isset($scenario['backup']) &&
|
|
|
|
count($scenario['backup']) === 1 &&
|
|
|
|
isset($scenario['transport']) &&
|
|
|
|
count($scenario['transport']) === 1;
|
2015-07-10 19:07:14 +02:00
|
|
|
}
|
2016-07-03 21:54:35 +02:00
|
|
|
}
|