-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
Refactor the whole package to use events
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace DigipolisGent\Robo\Helpers\EventHandler; | ||
|
||
use DigipolisGent\Robo\Helpers\Util\TimeHelper; | ||
use Robo\Contract\ConfigAwareInterface; | ||
|
||
abstract class AbstractBackupHandler | ||
extends AbstractTaskEventHandler | ||
implements ConfigAwareInterface | ||
{ | ||
|
||
use \Consolidation\Config\ConfigAwareTrait; | ||
|
||
/** | ||
* Generate a backup filename based on the given time. | ||
* | ||
* @param string $extension | ||
* The extension to append to the filename. Must include leading dot. | ||
* @param int|null $timestamp | ||
* The timestamp to generate the backup name from. Defaults to the request | ||
* time. | ||
* | ||
* @return string | ||
* The generated filename. | ||
*/ | ||
public function backupFileName($extension, $timestamp = null) | ||
{ | ||
if (is_null($timestamp)) { | ||
$timestamp = TimeHelper::getInstance()->getTime(); | ||
} | ||
return $timestamp . '_' . date('Y_m_d_H_i_s', $timestamp) . $extension; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace DigipolisGent\Robo\Helpers\EventHandler; | ||
|
||
use DigipolisGent\Robo\Helpers\EventHandler\EventHandlerWithPriority; | ||
use DigipolisGent\Robo\Helpers\Util\AddToContainerInterface; | ||
use Robo\Tasks; | ||
|
||
abstract class AbstractTaskEventHandler | ||
extends Tasks | ||
implements EventHandlerWithPriority, AddToContainerInterface | ||
{ | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return static::DEFAULT_PRIORITY; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace DigipolisGent\Robo\Helpers\EventHandler\DefaultHandler; | ||
|
||
use DigipolisGent\Robo\Helpers\EventHandler\AbstractBackupHandler; | ||
use DigipolisGent\Robo\Helpers\Util\RemoteConfig; | ||
use DigipolisGent\Robo\Task\Deploy\Ssh\Auth\KeyFile; | ||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
|
||
class BackupRemoteHandler extends AbstractBackupHandler | ||
{ | ||
|
||
use \DigipolisGent\Robo\Helpers\Traits\RemoteDatabaseBackupTrait; | ||
use \DigipolisGent\Robo\Helpers\Traits\RemoteFilesBackupTrait; | ||
use \DigipolisGent\Robo\Task\Deploy\Tasks; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function handle(GenericEvent $event) | ||
{ | ||
/** @var RemoteConfig $remoteConfig */ | ||
$remoteConfig = $event->getArgument('remoteConfig'); | ||
$remoteSettings = $remoteConfig->getRemoteSettings(); | ||
$options = $event->getArgument('options'); | ||
$fileBackupConfig = $event->getArgument('fileBackupConfig'); | ||
$timeouts = $event->getArgument('timeouts'); | ||
|
||
if (!$options['files'] && !$options['data']) { | ||
$options['files'] = true; | ||
$options['data'] = true; | ||
} | ||
|
||
$backupDir = $remoteSettings['backupsdir'] . '/' . $remoteSettings['time']; | ||
$auth = new KeyFile($remoteConfig->getUser(), $remoteConfig->getPrivateKeyFile()); | ||
$collection = $this->collectionBuilder(); | ||
|
||
if ($options['files']) { | ||
$collection | ||
->taskRemoteFilesBackup($remoteConfig->getHost(), $auth, $backupDir, $remoteSettings['filesdir']) | ||
->backupFile($this->backupFileName('.tar.gz')) | ||
->excludeFromBackup($fileBackupConfig['exclude_from_backup']) | ||
->backupSubDirs($fileBackupConfig['file_backup_subdirs']) | ||
->timeout($timeouts['backup_files']); | ||
} | ||
|
||
if ($options['data']) { | ||
$collection | ||
->taskRemoteDatabaseBackup($remoteConfig->getHost(), $auth, $backupDir, $remoteConfig->getCurrentProjectRoot()) | ||
->backupFile($this->backupFileName('.sql')) | ||
->timeout($timeouts['backup_database']); | ||
} | ||
|
||
return $collection; | ||
} | ||
} |