Skip to content

Commit

Permalink
#129. Add rollback system ability for rollback="2019-02-15 15:16:17"
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Jan 16, 2019
1 parent 4805eff commit 3bb6354
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,8 @@ There are also more things you can do, about rewinding history:
- by passing option like this `--merge=9` generator will get back for 9 steps
- `--merge="2017-07-29 11:35:32"` generator gets to the concrete files by time in history

Although, if you need to totally rollback the state of a system - use `--rollback` option, with the same keys as in merge.

======================

HTTP request/response examples can be found on WiKi page - https://github.com/RJAPI/api-generator/wiki
Expand Down
14 changes: 14 additions & 0 deletions src/controllers/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ private function setGenHistory()
}
}

/**
* Get files to process within rollback
*
* @return array
* @throws \Symfony\Component\Yaml\Exception\ParseException
* @throws DirectoryException
*/
protected function getRollbackInputFile(): array
{
$rollBack = $this->option('rollback');
Expand All @@ -350,6 +357,13 @@ protected function getRollbackInputFile(): array
}
}

if (strtotime($rollBack) !== false) {
$this->isRollback = true;
$dateTime = explode(PhpInterface::SPACE, $rollBack);

return $this->composeTimeFiles($dateTime);
}

return [];
}
}
77 changes: 58 additions & 19 deletions src/controllers/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@ private function createMigrations(): void

/**
* Collects all attrs, types and diffs for further code-generation
*
* @throws \Symfony\Component\Yaml\Exception\ParseException
*/
private function setMergedTypes(): void
{
$opMerge = $this->options[ConsoleInterface::OPTION_MERGE];
$timeCheck = strtotime($opMerge); // only for validation - coz of a diff timezones
$timeCheck = strtotime($opMerge); // only for validation - with respect to diff timezones

if (false !== $timeCheck) {
$dateTime = explode(PhpInterface::SPACE, $opMerge);

try {
$this->mergeTime($dateTime);
$this->mergeTime($opMerge);
} catch (DirectoryException $e) {
$this->error($e->getTraceAsString());
}
Expand All @@ -159,41 +159,66 @@ private function setMergedTypes(): void
}

/**
* Merges history RAML files with current by time in the past
* Merges history OAS files with current by time in the past
*
* @param string $opMerge
* @throws \Symfony\Component\Yaml\Exception\ParseException
* @throws DirectoryException
*/
private function mergeTime(string $opMerge): void
{
$dateTime = explode(PhpInterface::SPACE, $opMerge);
$this->composeTypes($this->composeTimeFiles($dateTime), $this->files);
}

/**
* @param array $dateTime
* @return array
* @throws DirectoryException
*/
private function mergeTime(array $dateTime): void
private function composeTimeFiles(array $dateTime): array
{
$date = $dateTime[0];
$time = str_replace(':', '', $dateTime[1]);
$path = DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $date . DIRECTORY_SEPARATOR;

$this->genDir = $dateTime[0];
$path = DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $this->genDir . DIRECTORY_SEPARATOR;

if (is_dir($path) === false) {
throw new DirectoryException('The directory: ' . $path . ' was not found.');
}

$files = glob($path . $time . '*' . DefaultInterface::RAML_EXT);
$files = glob($path . $time . '*');
foreach ($files as &$fullPath) {
$fullPath = str_replace($path, '', $fullPath);
}

$files = array_diff($files, DirsInterface::EXCLUDED_DIRS);
$this->composeTypes($date, $files, $this->files);

return $this->adjustFiles($files);
}

/**
* Merges history RAML files with current by backward steps
* Merges history OAS files with current by backward steps
*
* @param int $step
* @throws \Symfony\Component\Yaml\Exception\ParseException
*/
private function mergeStep(int $step): void
{
$dirs = scandir(DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR, SCANDIR_SORT_DESCENDING);
if ($dirs !== false) {
$dirs = array_diff($dirs, DirsInterface::EXCLUDED_DIRS);
$this->composeTypes($this->genDir, $this->composeStepFiles($dirs, $step), $this->files);
$this->composeTypes($this->composeStepFiles($dirs, $step), $this->files);
}
}

/**
* Composes files for step back in history via .gen dir
*
* @param array $dirs
* @param int $step
* @return array
*/
private function composeStepFiles(array $dirs, int $step): array
{
$filesToPass = [];
Expand Down Expand Up @@ -227,11 +252,16 @@ private function composeStepFiles(array $dirs, int $step): array
return $this->adjustFiles($filesToPass);
}

/**
* Merges current state with the last one
*
* @throws \Symfony\Component\Yaml\Exception\ParseException
*/
private function mergeLast(): void
{
$lastFiles = $this->getLastFiles();
if (empty($lastFiles) === false) {
$this->composeTypes($lastFiles['dir'], $lastFiles['files'], $this->files);
$this->composeTypes($lastFiles, $this->files);
}
}

Expand Down Expand Up @@ -273,22 +303,24 @@ private function getLastFiles(): array
}

/**
* Gets history files and merges them with current raml files
* @param string $dir desc sorted last date YYYY-mmm-dd directory
* @param array $files files from .gen/ dir saved history
* Gets history files and merges them with current OAS files
*
* @param array $files files from .gen/ dir saved history
* @param array $inputFiles file that were passed as an option + files from uses RAML property
* @throws \Symfony\Component\Yaml\Exception\ParseException
*/
private function composeTypes(string $dir, array $files, array $inputFiles): void
private function composeTypes(array $files, array $inputFiles): void
{
$attrsCurrent = [];
$attrsHistory = [];

$path = DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $this->genDir . DIRECTORY_SEPARATOR;
foreach ($files as $file) {
foreach ($inputFiles as $inFile) {

if (mb_strpos($file, basename($inFile), null, PhpInterface::ENCODING_UTF8) !== false) {
$dataCurrent = Yaml::parse(file_get_contents($inFile));
$dataHistory = Yaml::parse(file_get_contents(DirsInterface::GEN_DIR . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $file));
$dataHistory = Yaml::parse(file_get_contents($path . $file));

$this->currentTypes = $dataCurrent[ApiInterface::API_COMPONENTS][ApiInterface::API_SCHEMAS];
$this->historyTypes = $dataHistory[ApiInterface::API_COMPONENTS][ApiInterface::API_SCHEMAS];
Expand Down Expand Up @@ -342,7 +374,14 @@ private function composeDiffs(array $attrsCurrent, array $attrsHistory): void
}
}

private function adjustFiles(array $files)
/**
* Gets an unordered array of files and returns an ordered one
* stating from *openapi.yaml
*
* @param array $files
* @return array
*/
private function adjustFiles(array $files): array
{
$tmpFile = '';
foreach ($files as $k => $file) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/DefaultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

interface DefaultInterface
{
public const RAML_EXT = '.raml';
public const YAML_EXT = '.yaml';
public const CONTROLLER_POSTFIX = 'Controller';
public const FORM_REQUEST_POSTFIX = 'FormRequest';
// set the functional postfix for Codeception
Expand Down

0 comments on commit 3bb6354

Please sign in to comment.