Skip to content

Commit

Permalink
refactor logic to determine which compose file to use
Browse files Browse the repository at this point in the history
  • Loading branch information
jrushlow committed Apr 22, 2021
1 parent 76d65f9 commit 512fc75
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions src/Maker/MakeDockerDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ final class MakeDockerDatabase extends AbstractMaker
public function __construct(FileManager $fileManager)
{
$this->fileManager = $fileManager;
$this->composeFilePath = sprintf('%s/docker-compose.yaml', $this->fileManager->getRootDirectory());
}

public static function getCommandName(): string
Expand All @@ -80,27 +79,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
{
$io->section('- Docker Compose Setup-');

$composeFileExtensions = ['.yml', '.yaml'];
$composeFileExists = false;
$composeFileContents = '';
$statusMessage = 'Existing docker-compose.yaml not found: a new one will be generated!';

foreach ($composeFileExtensions as $extension) {
$composeFilePath = sprintf('%s/docker-compose%s', $this->fileManager->getRootDirectory(), $extension);

if (!$composeFileExists && $this->fileManager->fileExists($composeFilePath)) {
$composeFileExists = true;

$statusMessage = sprintf('We found your existing docker-compose%s: Let\'s update it!', $extension);

$this->composeFilePath = $composeFilePath;
$composeFileContents = $this->fileManager->getFileContents($composeFilePath);
}
}

$io->text($statusMessage);

$this->composeFileManipulator = new ComposeFileManipulator($composeFileContents);
$this->composeFileManipulator = new ComposeFileManipulator($this->getComposeFileContents($io));

$io->newLine();

Expand Down Expand Up @@ -194,4 +173,34 @@ private function checkForPDOSupport(string $databaseType, ConsoleStyle $io): voi
);
}
}

/**
* Determines and sets the correct Compose File Path and retrieves its contents
* if the file exists else an empty string.
*/
private function getComposeFileContents(ConsoleStyle $io): string
{
$this->composeFilePath = sprintf('%s/docker-compose.yaml', $this->fileManager->getRootDirectory());

$composeFileExists = false;
$statusMessage = 'Existing docker-compose.yaml not found: a new one will be generated!';
$contents = '';

foreach (['.yml', '.yaml'] as $extension) {
$composeFilePath = sprintf('%s/docker-compose%s', $this->fileManager->getRootDirectory(), $extension);

if (!$composeFileExists && $this->fileManager->fileExists($composeFilePath)) {
$composeFileExists = true;

$statusMessage = sprintf('We found your existing docker-compose%s: Let\'s update it!', $extension);

$this->composeFilePath = $composeFilePath;
$contents = $this->fileManager->getFileContents($composeFilePath);
}
}

$io->text($statusMessage);

return $contents;
}
}

0 comments on commit 512fc75

Please sign in to comment.