2015-07-12 20:34:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shikiryu\Backup\Transport;
|
|
|
|
|
|
|
|
use Shikiryu\Backup\Backup\BackupAbstract;
|
|
|
|
|
|
|
|
class Folder extends TransportAbstract
|
|
|
|
{
|
2019-01-25 21:39:18 +01:00
|
|
|
/**
|
|
|
|
* Folder to backup
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $folder;
|
2015-07-12 20:34:54 +02:00
|
|
|
|
2019-01-25 21:39:18 +01:00
|
|
|
public function __construct(BackupAbstract $backup, array $config = array())
|
|
|
|
{
|
|
|
|
parent::__construct($backup, $config);
|
2015-07-12 20:34:54 +02:00
|
|
|
|
2019-01-25 21:39:18 +01:00
|
|
|
if (!empty($this->folder)) {
|
|
|
|
$this->folder = sprintf('%s/', rtrim($this->folder, '/'));
|
|
|
|
}
|
|
|
|
}
|
2015-07-12 20:34:54 +02:00
|
|
|
|
2019-01-25 21:39:18 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function send()
|
|
|
|
{
|
2020-04-16 17:57:14 +02:00
|
|
|
if ($this->backup->isDistant()) {
|
|
|
|
$this->backup = $this->backup->retrieve();
|
2019-01-25 21:39:18 +01:00
|
|
|
}
|
2020-04-16 17:57:14 +02:00
|
|
|
if ($this->backup->isLocal()) {
|
|
|
|
foreach ($this->backup->getFilesToBackup() as $file => $name) {
|
|
|
|
if (copy($file, $this->folder . $name) === false) {
|
|
|
|
throw new \Exception(sprintf('Copy of %s in %s failed', $name, $this->folder));
|
|
|
|
}
|
2019-01-25 21:39:18 +01:00
|
|
|
}
|
2020-04-16 17:57:14 +02:00
|
|
|
foreach ($this->backup->getStreamsToBackup() as $name => $file) {
|
|
|
|
if (substr_count($name, '.') + 1 < 2) {
|
|
|
|
$name = 'backup' . $name . '.txt';
|
|
|
|
}
|
|
|
|
if (file_put_contents($this->folder . $name, $file) === false) {
|
|
|
|
throw new \Exception(sprintf('Saving of %s in %s failed', $name, $this->folder));
|
|
|
|
}
|
2019-01-25 21:39:18 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 17:57:14 +02:00
|
|
|
|
2019-01-25 21:39:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
2016-07-03 21:54:35 +02:00
|
|
|
}
|