Skip to content

Commit

Permalink
Merge pull request #39 from district09/feature/events-refactor
Browse files Browse the repository at this point in the history
Refactor the whole package to use events
  • Loading branch information
Jelle-S authored Nov 24, 2022
2 parents cec6f05 + 8c4bbc2 commit c11b473
Show file tree
Hide file tree
Showing 84 changed files with 4,229 additions and 2,528 deletions.
583 changes: 487 additions & 96 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"digipolisgent/robo-digipolis-general": "^2.0",
"digipolisgent/command-builder": "^1.2.1",
"roave/better-reflection": "^5.0",
"consolidation/annotated-command": "^4, <=4.5.6"
"symfony/event-dispatcher": "^6.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5.20"
Expand Down
10 changes: 0 additions & 10 deletions src/DependencyInjection/AppTaskFactoryAwareInterface.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/DependencyInjection/BackupTaskFactoryAwareInterface.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/DependencyInjection/BuildTaskFactoryAwareInterface.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/DependencyInjection/CacheTaskFactoryAwareInterface.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/DependencyInjection/DeployTaskFactoryAwareInterface.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/DependencyInjection/PropertiesHelperAwareInterface.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/DependencyInjection/RemoteHelperAwareInterface.php

This file was deleted.

51 changes: 0 additions & 51 deletions src/DependencyInjection/ServiceProvider.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/DependencyInjection/SyncTaskFactoryAwareInterface.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/DependencyInjection/Traits/AppTaskFactoryAware.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/DependencyInjection/Traits/BackupTaskFactoryAware.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/DependencyInjection/Traits/BuildTaskFactoryAware.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/DependencyInjection/Traits/CacheTaskFactoryAware.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/DependencyInjection/Traits/DeployTaskFactoryAware.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/DependencyInjection/Traits/PropertiesHelperAware.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/DependencyInjection/Traits/RemoteHelperAware.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/DependencyInjection/Traits/SyncTaskFactoryAware.php

This file was deleted.

34 changes: 34 additions & 0 deletions src/EventHandler/AbstractBackupHandler.php
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;
}
}
21 changes: 21 additions & 0 deletions src/EventHandler/AbstractTaskEventHandler.php
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;
}
}
56 changes: 56 additions & 0 deletions src/EventHandler/DefaultHandler/BackupRemoteHandler.php
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;
}
}
Loading

0 comments on commit c11b473

Please sign in to comment.