|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Companienv; |
| 4 | + |
| 5 | +use Companienv\DotEnv\Block; |
| 6 | +use Companienv\DotEnv\File; |
| 7 | +use Jackiedo\DotenvEditor\DotenvFormatter; |
| 8 | +use Jackiedo\DotenvEditor\DotenvWriter; |
| 9 | +use Symfony\Component\Console\Helper\QuestionHelper; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use Symfony\Component\Console\Question\Question; |
| 13 | + |
| 14 | +class Companion |
| 15 | +{ |
| 16 | + private $input; |
| 17 | + private $output; |
| 18 | + |
| 19 | + private $reference; |
| 20 | + private $path; |
| 21 | + |
| 22 | + public function __construct(InputInterface $input, OutputInterface $output, File $reference, string $path) |
| 23 | + { |
| 24 | + $this->input = $input; |
| 25 | + $this->output = $output; |
| 26 | + |
| 27 | + $this->reference = $reference; |
| 28 | + $this->path = $path; |
| 29 | + } |
| 30 | + |
| 31 | + public function fillGaps() |
| 32 | + { |
| 33 | + $missingVariables = $this->getMissingVariables(); |
| 34 | + if (count($missingVariables) == 0) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + $this->output->writeln(sprintf( |
| 39 | + 'It looks like you are missing some configuration (%d variables). I will help you to sort this out.', |
| 40 | + count($missingVariables) |
| 41 | + )); |
| 42 | + |
| 43 | + if (!$this->askConfirmation('<info>Let\'s fix this? (y) </info>')) { |
| 44 | + $this->output->writeln([ |
| 45 | + '', |
| 46 | + '<comment>I let you think about it then. Re-run the command to get started again.</comment>', |
| 47 | + '' |
| 48 | + ]); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $definedVariablesHash = $this->getDefinedVariablesHash(); |
| 54 | + foreach ($this->reference->getBlocks() as $block) { |
| 55 | + $this->fillBlockGaps($block, $missingVariables, $definedVariablesHash); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private function fillBlockGaps(Block $block, array $missingVariables, array $definedVariablesHash) |
| 60 | + { |
| 61 | + $variablesInBlock = $block->getVariablesInBlock($missingVariables); |
| 62 | + if (count($variablesInBlock) == 0) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + $this->output->writeln([ |
| 67 | + '', |
| 68 | + '<info>'.$block->getTitle().'</info>', |
| 69 | + $block->getDescription(), |
| 70 | + '' |
| 71 | + ]); |
| 72 | + |
| 73 | + foreach ($block->getVariables() as $variable) { |
| 74 | + $defaultValue = isset($definedVariablesHash[$variable->getName()]) ? $definedVariablesHash[$variable->getName()] : $variable->getValue(); |
| 75 | + $question = sprintf('<comment>%s</comment> ? ', $variable->getName()); |
| 76 | + |
| 77 | + if ($defaultValue) { |
| 78 | + $question .= '('.$defaultValue.') '; |
| 79 | + } |
| 80 | + |
| 81 | + $value = $this->ask($question, $defaultValue); |
| 82 | + $this->writeVariable($variable->getName(), $value); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private function writeVariable(string $name, string $value) |
| 87 | + { |
| 88 | + if (!file_exists($this->path)) { |
| 89 | + file_put_contents($this->path, ''); |
| 90 | + } |
| 91 | + |
| 92 | + $variablesInFileHash = $this->getDefinedVariablesHash(); |
| 93 | + |
| 94 | + $writer = new DotenvWriter(new DotenvFormatter()); |
| 95 | + $writer->setBuffer(file_get_contents($this->path)); |
| 96 | + |
| 97 | + if (isset($variablesInFileHash[$name])) { |
| 98 | + $writer->updateSetter($name, $value); |
| 99 | + } else { |
| 100 | + $writer->appendSetter($name, $value); |
| 101 | + } |
| 102 | + |
| 103 | + $writer->save($this->path); |
| 104 | + } |
| 105 | + |
| 106 | + private function getMissingVariables() |
| 107 | + { |
| 108 | + $variablesInFile = $this->getDefinedVariablesHash(); |
| 109 | + |
| 110 | + $variablesInReference = $this->reference->getAllVariables(); |
| 111 | + $missingVariables = []; |
| 112 | + |
| 113 | + foreach ($variablesInReference as $variable) { |
| 114 | + if ($variable->hasValue() && !isset($variablesInFile[$variable->getName()])) { |
| 115 | + $missingVariables[] = $variable; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return $missingVariables; |
| 120 | + } |
| 121 | + |
| 122 | + private function getDefinedVariablesHash() |
| 123 | + { |
| 124 | + $variablesInFile = []; |
| 125 | + if (file_exists($this->path)) { |
| 126 | + $dotEnv = new \Symfony\Component\Dotenv\Dotenv(); |
| 127 | + $variablesInFile = $dotEnv->parse(file_get_contents($this->path), $this->path); |
| 128 | + } |
| 129 | + |
| 130 | + return $variablesInFile; |
| 131 | + } |
| 132 | + |
| 133 | + private function askConfirmation(string $question) : bool |
| 134 | + { |
| 135 | + return in_array(strtolower($this->ask($question, 'y')), ['y', 'yes']); |
| 136 | + } |
| 137 | + |
| 138 | + private function ask(string $question, string $default = null) : string |
| 139 | + { |
| 140 | + return (new QuestionHelper())->ask($this->input, $this->output, new Question($question, $default)); |
| 141 | + } |
| 142 | +} |
0 commit comments