Shikiryu_Backup/app/code/backup/BackupAbstract.php

237 lines
5.6 KiB
PHP
Raw Normal View History

2015-07-03 00:00:30 +02:00
<?php
2015-07-10 19:07:14 +02:00
namespace Shikiryu\Backup\Backup;
2015-07-03 00:00:30 +02:00
2015-08-17 13:16:28 +02:00
abstract class BackupAbstract
2015-07-03 00:00:30 +02:00
{
2016-07-01 21:59:39 +02:00
/** @var array */
2015-08-18 17:02:37 +02:00
protected $options;
/** @var string[] */
protected $_filesToBackup = [];
/** @var string[] */
protected $_streamsToBackup = [];
2015-07-03 00:00:30 +02:00
/**
2015-07-12 20:34:54 +02:00
* @param array $config
2015-07-03 00:00:30 +02:00
*/
2015-07-12 20:34:54 +02:00
function __construct($config = array())
2015-07-03 00:00:30 +02:00
{
2015-08-18 17:02:37 +02:00
$this->options = !empty($config['options']) ? $config['options'] : [];
unset($config['options']);
2015-07-12 20:34:54 +02:00
foreach ($config as $name => $value) {
$this->$name = $value;
}
2015-08-18 17:02:37 +02:00
$this->init();
2015-07-03 00:00:30 +02:00
}
2015-07-12 20:34:54 +02:00
/**
* Magic setter method
*
* @param $name
* @param $value
*/
public function __set($name, $value)
{
$this->$name = $value;
}
2015-07-09 00:46:17 +02:00
/**
* @return array
*/
public function getFilesToBackup()
{
return $this->_filesToBackup;
}
/**
* @return array
*/
public function getStreamsToBackup()
{
return $this->_streamsToBackup;
}
2015-08-18 17:02:37 +02:00
/**
* Check if all files got the minimum given size.
*
* @param int $file_size
2015-08-18 17:02:37 +02:00
*
* @return bool
*/
public function checkMinimumFilesize($file_size)
2015-08-18 17:02:37 +02:00
{
foreach ($this->_filesToBackup as $file => $name) {
if (filesize($file) < $file_size) {
2015-08-18 17:02:37 +02:00
return false;
}
}
foreach ($this->_streamsToBackup as $name => $file) {
if (mb_strlen($file, 'utf-8') < $file_size) {
2015-08-18 17:02:37 +02:00
return false;
}
}
return true;
}
2016-07-01 21:59:39 +02:00
/**
* Initialize everything
*/
2015-08-18 17:02:37 +02:00
protected function init()
{
$this->preBuild();
$this->build();
$this->postBuild();
$this->applyOptions();
}
2016-07-01 21:59:39 +02:00
/**
* Function that can be used to initialize the backup
*/
2015-08-18 17:02:37 +02:00
abstract protected function preBuild();
2016-07-01 21:59:39 +02:00
/**
* Function that can be used after the backup
*/
2015-08-18 17:02:37 +02:00
abstract protected function postBuild();
2016-07-01 21:59:39 +02:00
/**
* Mandatory function doing the backup
*/
2015-08-18 17:02:37 +02:00
abstract protected function build();
2016-07-01 21:59:39 +02:00
/**
* Check if the backup is valid
*
* @return bool
*/
2015-08-18 17:02:37 +02:00
abstract public function isValid();
/**
* @return $this
*/
protected function applyOptions()
{
// TODO isValid here ?
foreach($this->options as $name => $value)
{
$method = sprintf('setOption%s', ucfirst($name));
if (method_exists($this, $method)) {
call_user_func([$this, $method], $value);
}
}
return $this;
}
/**
* Zip every backup files and streams into one zip
* Enabled via options
*
* @return $this
* @throws \Exception
*/
protected function setOptionZip()
{
$zip = new \ZipArchive();
$zip_name = sprintf('%s.zip', (!empty($this->options['name']) ? $this->options['name'] : time())); // Zip name
if (touch(TEMP_DIR.$zip_name) === false) {
throw new \Exception('Backup::Zip::Permission denied.');
}
if ($zip->open(TEMP_DIR.$zip_name, \ZIPARCHIVE::OVERWRITE)==TRUE) {
foreach($this->_filesToBackup as $file => $name)
{
$zip->addFile($file, $name); // Adding files into zip
}
foreach($this->_streamsToBackup as $file => $name)
{
$zip->addFromString($file, $name); // Adding streams into zip
}
$zip->close();
} else {
throw new \Exception('Backup::Zip::Can\'t zip the given backup.');
}
$this->_filesToBackup = [TEMP_DIR.$zip_name => $zip_name];
$this->_streamsToBackup = [];
return $this;
}
2015-07-03 00:00:30 +02:00
/**
* Add the current date with the given format into the files names
*
* @param string $format
*
* @return $this
*/
2015-08-18 17:02:37 +02:00
protected function setOptionAddDate($format = 'Ymd')
2015-07-03 00:00:30 +02:00
{
2015-08-18 17:02:37 +02:00
if ($format === true) {
$format = 'Ymd';
}
2015-07-03 00:00:30 +02:00
$tmpFiles = array();
2015-07-12 20:34:54 +02:00
foreach ($this->_filesToBackup as $file => $name) {
2015-07-03 00:00:30 +02:00
$nameA = explode('.', $name);
$nameA[] = end($nameA);
2015-07-12 20:34:54 +02:00
$nameA[count($nameA) - 2] = date($format);
2015-07-03 00:00:30 +02:00
$name = implode('.', $nameA);
$tmpFiles[$file] = $name;
}
$this->_filesToBackup = $tmpFiles;
$tmpStream = array();
2015-07-12 20:34:54 +02:00
foreach ($this->_streamsToBackup as $name => $stream) {
2015-07-03 00:00:30 +02:00
$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
*/
2015-08-18 17:02:37 +02:00
protected function setOptionAddTime($format = 'his')
2015-07-03 00:00:30 +02:00
{
2015-08-18 17:02:37 +02:00
if ($format === true) {
$format = 'his';
}
2015-07-03 00:00:30 +02:00
$tmpFiles = array();
2015-07-12 20:34:54 +02:00
foreach ($this->_filesToBackup as $file => $name) {
2015-07-03 00:00:30 +02:00
$nameA = explode('.', $name);
$nameA[] = end($nameA);
2015-07-12 20:34:54 +02:00
$nameA[count($nameA) - 2] = date($format);
2015-07-03 00:00:30 +02:00
$name = implode('.', $nameA);
$tmpFiles[$file] = $name;
}
$this->_filesToBackup = $tmpFiles;
$tmpStream = array();
2015-07-12 20:34:54 +02:00
foreach ($this->_streamsToBackup as $name => $stream) {
2015-07-03 00:00:30 +02:00
$tmpStream[$name . '-' . date($format)] = $stream;
}
$this->_streamsToBackup = $tmpStream;
return $this;
}
/**
2015-08-18 17:02:37 +02:00
* @param $name
2016-07-01 21:59:39 +02:00
*
2015-08-18 17:02:37 +02:00
* @throws \Exception
2016-07-01 21:59:39 +02:00
*
* @SuppressWarnings("unused")
2015-07-03 00:00:30 +02:00
*/
2015-08-18 17:02:37 +02:00
protected function setOptionName($name)
2015-07-12 20:34:54 +02:00
{
2015-08-18 17:02:37 +02:00
if (empty($this->options['zip'])) {
throw new \Exception('name option is for zip only.');
2015-07-03 00:00:30 +02:00
}
2015-07-12 20:34:54 +02:00
}
2015-07-03 00:00:30 +02:00
}