| 1 | <?php |
||
| 7 | class BashFileManipulator |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var FileManipulator |
||
| 11 | */ |
||
| 12 | private $fileManipulator; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param FileManipulator $fileManipulator |
||
| 16 | */ |
||
| 17 | public function __construct(FileManipulator $fileManipulator) |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param string $variableName |
||
| 24 | * |
||
| 25 | * @return string|null |
||
| 26 | */ |
||
| 27 | public function getValue($variableName) |
||
| 28 | { |
||
| 29 | if (preg_match($this->getVariablePattern($variableName), $this->fileManipulator->read(), $matches)) { |
||
| 30 | return trim($matches[1]); |
||
| 31 | } |
||
| 32 | |||
| 33 | return; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param string $variableName |
||
| 38 | * @param string $value |
||
| 39 | */ |
||
| 40 | public function replaceValue($variableName, $value) |
||
| 41 | { |
||
| 42 | $variableDeclaration = $variableName.'=\''.$value.'\''; |
||
| 43 | if (null === $this->getValue($variableName)) { |
||
| 44 | $contents = $this->fileManipulator->read().PHP_EOL.$variableDeclaration; |
||
| 45 | } else { |
||
| 46 | $contents = preg_replace($this->getVariablePattern($variableName), $variableDeclaration, $this->fileManipulator->read()); |
||
| 47 | } |
||
| 48 | |||
| 49 | $this->fileManipulator->write($contents); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $variableName |
||
| 54 | * |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | private function getVariablePattern($variableName) |
||
| 61 | } |
||
| 62 |