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

@@ -0,0 +1,188 @@
<?php
namespace Shikiryu\Backup\Backup;
class BackupAbstract
{
protected $_filesToBackup;
protected $_streamsToBackup;
/**
*
*/
function __construct()
{
$this->_filesToBackup = array();
$this->_streamsToBackup = array();
}
/**
* @return array
*/
public function getFilesToBackup()
{
return $this->_filesToBackup;
}
/**
* @return array
*/
public function getStreamsToBackup()
{
return $this->_streamsToBackup;
}
/**
* Add the current date with the given format into the files names
*
* @param string $format
*
* @return $this
*/
function addDate($format = 'Ymd')
{
$tmpFiles = array();
foreach($this->_filesToBackup as $file => $name)
{
$nameA = explode('.', $name);
$nameA[] = end($nameA);
$nameA[count($nameA)-2] = date($format);
$name = implode('.', $nameA);
$tmpFiles[$file] = $name;
}
$this->_filesToBackup = $tmpFiles;
$tmpStream = array();
foreach($this->_streamsToBackup as $name => $stream)
{
$tmpStream[$name . '-' . date($format)] = $stream;
}
$this->_streamsToBackup = $tmpStream;
return $this;
}
/**
* Add the current time with the given format into the files names
*
* @param string $format
*
* @return $this
*/
function addTime($format = 'his')
{
$tmpFiles = array();
foreach($this->_filesToBackup as $file => $name)
{
$nameA = explode('.', $name);
$nameA[] = end($nameA);
$nameA[count($nameA)-2] = date($format);
$name = implode('.', $nameA);
$tmpFiles[$file] = $name;
}
$this->_filesToBackup = $tmpFiles;
$tmpStream = array();
foreach($this->_streamsToBackup as $name => $stream)
{
$tmpStream[$name . '-' . date($format)] = $stream;
}
$this->_streamsToBackup = $tmpStream;
return $this;
}
/**
*
* @param $name string
* @param $args mixed
*
* @return bool
*/
public function __call($name, $args)
{
if (substr($name,0,8) == 'backupTo') {
$type = substr($name, 8);
return Shikiryu_Backup_Transport_Factory::getAndSend($type, $this, $args);
}
}
/*function backupToEmail($to, $from, $object, $mes)
{
$email = new Shikiryu_Backup_Email();
$email->addTo($to)
->setFrom($from)
->setSubject($object)
->setMessage($mes)
->setFiles($this->_filesToBackup)
->setStreams($this->_streamsToBackup);
return $email->send();
}*/
function backupToDropbox()
{
}
function backupToFTP($adress, $login = '', $pwd = '', $path ='/')
{
$ftp = new Shikiryu_Backup_FTP($adress, $login, $pwd, $path);
$ftp->setFiles($this->_filesToBackup)
->setStreams($this->_streamsToBackup)
->send();
}
/**
* @param $folder
*/
function backupToFolder($folder)
{
if (!empty($folder)) {
$folder = sprintf('%s/',rtrim($folder, '/'));
}
// if($folder != '')
// {
// if(substr($folder, 0, -1) != '/')
// $folder .= '/';
// }
foreach($this->_filesToBackup as $file => $name)
{
copy($file, $folder . $name);
}
foreach($this->_streamsToBackup as $name => $file)
{
if (count(explode('.', $name)) < 2) {
$name = 'backup' . $name . '.txt';
}
file_put_contents($folder . $name, $file);
}
}
/**
* Check if all files got the minimum given size.
*
* @param int $fs
*
* @return bool
*/
function checkMinimumFilesize($fs)
{
foreach($this->_filesToBackup as $file => $name)
{
if (filesize($file) < $fs) {
return false;
}
}
foreach($this->_streamsToBackup as $name => $file)
{
if (mb_strlen($file, 'utf-8') < $fs) {
return false;
}
}
return true;
}
}
?>

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;
}
}

36
app/code/backup/Files.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
namespace Shikiryu\Backup\Backup;
class Files extends BackupAbstract
{
/**
* @param array $filesToBackup
*/
function __construct($filesToBackup = array())
{
parent::__construct();
if(!empty($filesToBackup) && is_array($filesToBackup)){
$names = array_map("basename",$filesToBackup);
$this->_filesToBackup = array_combine($filesToBackup,$names);
}
}
/**
*
*
* @param string[] $filestobackup a list of file path
*
* @return $this
*/
function setFilePath($filesToBackup = array())
{
if(!empty($filesToBackup) && is_array($filesToBackup))
{
$names = array_map("basename",$filesToBackup);
$this->_filesToBackup = array_combine($filesToBackup,$names);
}
return $this;
}
}
?>

113
app/code/backup/Mysql.php Normal file
View File

@@ -0,0 +1,113 @@
<?php
namespace Shikiryu\Backup\Backup;
class Mysql extends BackupAbstract
{
/**
* @var $_pdo PDO
*/
private $_pdo;
private $_tables;
/**
* @param $host
* @param $login
* @param $pwd
* @param $db
*/
public function __construct($host, $login, $pwd, $db) {
parent::__construct();
$this->_tables = array();
$this->_pdo = new PDO('mysql:host='.$host.';dbname='.$db, $login, $pwd);
}
/**
* set the list of table to backup
*
* @param array $tables
*
* @return $this
*/
public function setTables(array $tables)
{
if(is_array($tables) && !empty($tables)) {
$this->_tables = $tables;
}
return $this;
}
// function withSQL($sql) {
// $statement = $this->_pdo->query($sql);
// }
/**
* @param array $tables
* @return $this|string
*/
public function fromTables($tables = array())
{
if(!empty($tables)) {
$this->_tables = $tables;
}
$this->_streamsToBackup[] = $this->getFromTables();
return $this;
}
/**
* set the list of table to backup to all tables
*
* @return $this
*/
public function everything() {
$this->_tables = array();
foreach($this->_pdo->query('SHOW TABLES') as $table) {
$this->_tables[] = $table;
}
$this->_streamsToBackup[] = $this->getFromTables();
return $this;
}
/**
* @return string
*/
private function getFromTables()
{
$return = "";
foreach ($this->_tables as $table) {
if(is_array($table)) {
$table = $table[0];
}
$result = $this->_pdo->prepare('SELECT * FROM ' . $table);
$result->execute();
$num_fields = $result->columnCount();
$return .= 'DROP TABLE IF EXISTS ' . $table . ';';
$result2 = $this->_pdo->prepare('SHOW CREATE TABLE ' . $table);
$result2->execute();
$row2 = $result2->fetch();
$return.= "\n\n" . $row2[1] . ";\n\n";
foreach($result as $i=>$row){
$return.= 'INSERT INTO ' . $table . ' VALUES(';
for ($j = 0; $j < $num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("\n", "\\n", $row[$j]);
if (isset($row[$j])) {
$return.= '"' . $row[$j] . '"';
} else {
$return.= '""';
}
if ($j < ($num_fields - 1)) {
$return.= ',';
}
}
$return.= ");\n";
}
$return.="\n\n\n";
}
return $return;
}
}
?>