Skip to content

Commit

Permalink
Merge pull request #30 from sitegeist/feature/proxyAsssetSupport-stash
Browse files Browse the repository at this point in the history
Add proxy assset support to stash feature
  • Loading branch information
grebaldi authored Feb 19, 2019
2 parents 4434062 + e7be863 commit 1e1499e
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 26 deletions.
8 changes: 7 additions & 1 deletion Classes/Command/AbstractCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Core\Bootstrap;
use Neos\Flow\Cli\CommandController;
use Sitegeist\MagicWand\Domain\Service\ConfigurationService;

abstract class AbstractCommandController extends CommandController
{

const HIDE_RESULT = 1;
const HIDE_COMMAND = 2;

Expand Down Expand Up @@ -44,6 +44,12 @@ abstract class AbstractCommandController extends CommandController
*/
protected $flowCommand;

/**
* @Flow\Inject
* @var ConfigurationService
*/
protected $configurationService;

/**
* @param string $commands
* @param array $arguments
Expand Down
7 changes: 0 additions & 7 deletions Classes/Command/CloneCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Neos\Utility\Arrays;
use Neos\Flow\Core\Bootstrap;
use Sitegeist\MagicWand\DBAL\SimpleDBAL;
use Sitegeist\MagicWand\Domain\Service\ConfigurationService;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -43,12 +42,6 @@ class CloneCommandController extends AbstractCommandController
*/
protected $dbal;

/**
* @Flow\Inject
* @var ConfigurationService
*/
protected $configurationService;

/**
* Show the list of predefined clone configurations
*/
Expand Down
105 changes: 95 additions & 10 deletions Classes/Command/StashCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
class StashCommandController extends AbstractCommandController
{



/**
* Creates a new stash entry with the given name.
*
Expand All @@ -32,10 +29,7 @@ public function createCommand($name)
# Build Paths #
#######################

$basePath = sprintf(
FLOW_PATH_ROOT . 'Data/MagicWandStash/%s',
$name
);
$basePath = $this->getStashEntryPath($name);

$databaseDestination = $basePath . '/database.sql';
$persistentDestination = $basePath . '/persistent/';
Expand All @@ -55,6 +49,24 @@ public function createCommand($name)
$this->addSecret($this->databaseConfiguration['user']);
$this->addSecret($this->databaseConfiguration['password']);

######################
# Write Manifest #
######################
$this->renderHeadLine('Write Manifest');
$presetName = $this->configurationService->getCurrentPreset();
$presetConfiguration = $this->configurationService->getCurrentConfiguration();
$cloneTimestamp = $this->configurationService->getMostRecentCloneTimeStamp();
$stashTimestamp = time();

$this->writeStashEntryManifest($name, [
'preset' => [
'name' => $presetName,
'configuration' => $presetConfiguration
],
'cloned_at' => $cloneTimestamp,
'stashed_at' => $stashTimestamp
]);

######################
# Backup Database #
######################
Expand Down Expand Up @@ -102,6 +114,8 @@ public function createCommand($name)
*/
public function listCommand()
{
$head = ['Name', 'Stashed At', 'From Preset', 'Cloned At'];
$rows = [];
$basePath = sprintf(FLOW_PATH_ROOT . 'Data/MagicWandStash');

if (!is_dir($basePath)) {
Expand All @@ -114,7 +128,16 @@ public function listCommand()

foreach ($baseDir as $entry) {
if (!in_array($entry, ['.', '..'])) {
$this->renderLine(' • %s', [$entry->getFilename()]);
$stashEntryName = $entry->getFilename();
$manifest = $this->readStashEntryManifest($stashEntryName) ?: [];

$rows[] = [
$stashEntryName,
$manifest['stashed_at'] ? date('Y-m-d H:i:s', $manifest['stashed_at']) : 'N/A',
isset($manifest['preset']['name']) ? $manifest['preset']['name'] : 'N/A',
$manifest['cloned_at'] ? date('Y-m-d H:i:s', $manifest['cloned_at']) : 'N/A',
];

$anyEntry = true;
}
}
Expand All @@ -123,6 +146,8 @@ public function listCommand()
$this->renderLine('Stash is empty.');
$this->quit(1);
}

$this->output->outputTable($rows, $head);
}

/**
Expand Down Expand Up @@ -158,7 +183,7 @@ public function clearCommand()
*/
public function restoreCommand($name, $yes = false, $keepDb = false)
{
$basePath = sprintf(FLOW_PATH_ROOT . 'Data/MagicWandStash/%s', $name);
$basePath = $this->getStashEntryPath($name);
$this->restoreStashEntry($basePath, $name, $yes, true, $keepDb);
}

Expand Down Expand Up @@ -277,7 +302,7 @@ protected function restoreStashEntry($source, $name, $force = false, $preserve =
. '`; CREATE DATABASE `'
. $this->databaseConfiguration['dbname']
. '` collate utf8_unicode_ci;';

$this->executeLocalShellCommand(
'echo %s | mysql --host=%s --user=%s --password=%s',
[
Expand Down Expand Up @@ -347,6 +372,13 @@ protected function restoreStashEntry($source, $name, $force = false, $preserve =
$this->renderHeadLine('Publish Resources');
$this->executeLocalFlowCommand('resource:publish');

#############################
# Restore Clone Information #
#############################
if($manifest = $this->readStashEntryManifest($name)) {
$this->configurationService->setCurrentStashEntry($name, $manifest);
}

#################
# Final Message #
#################
Expand All @@ -372,4 +404,57 @@ protected function checkConfiguration()

$this->renderLine(' - Configuration seems ok ...');
}

/**
* @param string $stashEntryName
* @return string
*/
protected function getStashEntryPath(string $stashEntryName): string
{
return sprintf(
FLOW_PATH_ROOT . 'Data/MagicWandStash/%s',
$stashEntryName
);
}

/**
* @param string $stashEntryName
* @return array|null
*/
protected function readStashEntryManifest(string $stashEntryName): ?array
{
$manifestDestination = $this->getStashEntryPath($stashEntryName) . '/manifest.json';

if (file_exists($manifestDestination)) {
if ($manifest = json_decode(file_get_contents($manifestDestination), true)) {
if (is_array($manifest)) {
return $manifest;
}
}

$this->outputLine('<error>Manifest file has been corrupted.</error>');
}

return null;
}

/**
* @param string $stashEntryName
* @param array $manifest
* @return void
*/
protected function writeStashEntryManifest(string $stashEntryName, array $manifest): void
{
$manifestDestination = $this->getStashEntryPath($stashEntryName) . '/manifest.json';

// Create directory, if not exists
if (!file_exists(dirname($manifestDestination))) {
FileUtils::createDirectoryRecursively(dirname($manifestDestination));
}

// Write manifest file
file_put_contents($manifestDestination, json_encode($manifest, JSON_PRETTY_PRINT));

$this->outputLine('Wrote "%s"', [$manifestDestination]);
}
}
84 changes: 76 additions & 8 deletions Classes/Domain/Service/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,46 @@ class ConfigurationService
*/
protected $clonePresets;

/**
* @return string
*/
public function getCurrentPreset(): ?string
{
$clonePresetInformation = $this->clonePresetInformationCache->get('current');

if ($clonePresetInformation && is_array($clonePresetInformation) && isset($clonePresetInformation['presetName'])) {
return $clonePresetInformation['presetName'];
}

return null;
}

/**
* @return integer
*/
public function getMostRecentCloneTimeStamp(): ?int
{
$clonePresetInformation = $this->clonePresetInformationCache->get('current');

if ($clonePresetInformation && is_array($clonePresetInformation) && isset($clonePresetInformation['cloned_at'])) {
return intval($clonePresetInformation['cloned_at']);
}

return null;
}

/**
* @return array
*/
public function getCurrentConfiguration(): array
{
$cloneInformations = $this->clonePresetInformationCache->get('current');
if ($cloneInformations && is_array($this->clonePresets) && array_key_exists($cloneInformations['presetName'], $this->clonePresets)) {
return $this->clonePresets[$cloneInformations['presetName']];
} else {
return [];
if ($presetName = $this->getCurrentPreset()) {
if (is_array($this->clonePresets) && array_key_exists($presetName, $this->clonePresets)) {
return $this->clonePresets[$presetName];
}
}

return [];
}

/**
Expand All @@ -44,17 +73,56 @@ public function getCurrentConfigurationByPath($path)
/**
* @return boolean
*/
public function hasConfiguration(): bool
public function hasCurrentPreset(): bool
{
return $this->clonePresetInformationCache->has('current');
if ($this->clonePresetInformationCache->has('current')) {
return true;
}

$clonePresetInformation = $this->clonePresetInformationCache->get('current');

if ($clonePresetInformation && is_array($clonePresetInformation) && isset($clonePresetInformation['presetName'])) {
return true;
}

return false;
}

/**
* @param $presetName string
* @return void
* @throws \Neos\Cache\Exception
*/
public function setCurrentPreset(string $presetName): void
{
$this->clonePresetInformationCache->set('current', ['presetName' => $presetName, 'timestamp' => time()]);
$this->clonePresetInformationCache->set('current', [
'presetName' => $presetName,
'cloned_at' => time()
]);
}

/**
* @param string $stashEntryName
* @param array $stashEntryManifest
* @return void
* @throws \Neos\Cache\Exception
*/
public function setCurrentStashEntry(string $stashEntryName, array $stashEntryManifest): void
{
if (!isset($stashEntryManifest['preset']['name'])) {
return;
}

if (!isset($stashEntryManifest['cloned_at'])) {
return;
}

$presetName = $stashEntryManifest['preset']['name'];
$clonedAt = $stashEntryManifest['cloned_at'];

$this->clonePresetInformationCache->set('current', [
'presetName' => $presetName,
'cloned_at' => $clonedAt
]);
}
}

0 comments on commit 1e1499e

Please sign in to comment.