Skip to content

Commit

Permalink
Refactor the whole package to use events
Browse files Browse the repository at this point in the history
Because of consolidation/annotated-command#273,
which technically speaking contains a BC break, it was made clear for us
that we're not using Robo or the consolidation packages as they were
intended. In an effort to keep this package as extendible as possible,
and keeping the same philosophy as before, we switched to an event-based
system. Basically every step in a command fires an event. The event
listener returns a `HandlerWithPriority`, which it turn returns a
`TaskInterface` to be executed for this step in the command. We made our
own implementation of event priorities, since the default implementation
used in Robo doesn't support them
(consolidation/annotated-command#244).

I tried to document everything as well as possible in the README, but it
might (probably will) need some lovin'.

This package contains default event handlers with some sensible (to us)
defaults. You can prevent these handlers from being executed by writig
your own handler with a higher priority (lower number) which does its
thing and then calls `$event->stopPropagation()`.
  • Loading branch information
Jelle-S committed Nov 7, 2022
1 parent cec6f05 commit 6b756a6
Show file tree
Hide file tree
Showing 82 changed files with 4,158 additions and 2,528 deletions.
621 changes: 525 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 6b756a6

Please sign in to comment.