Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from boite/develop
Add fs:setline command
- Loading branch information
Showing
28 changed files
with
2,637 additions
and
1,164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
namespace Droid\Plugin\Fs\Command; | ||
|
||
use RuntimeException; | ||
|
||
use Droid\Lib\Plugin\Command\CheckableTrait; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
use Droid\Plugin\Fs\Model\File\FileFactory; | ||
use Droid\Plugin\Fs\Model\File\NameValueLine; | ||
use Droid\Plugin\Fs\Model\File\UnusableFileException; | ||
|
||
class FsSetlineCommand extends Command | ||
{ | ||
use CheckableTrait; | ||
|
||
protected $fileFactory; | ||
|
||
public function __construct( | ||
FileFactory $fileFactory, | ||
$name = null | ||
) { | ||
$this->fileFactory = $fileFactory; | ||
return parent::__construct($name); | ||
} | ||
|
||
public function configure() | ||
{ | ||
$this->setName('fs:setline') | ||
->setDescription('Add a (name, value) pair to a line based file, or update an existing (name, value) pair.') | ||
->addArgument( | ||
'file', | ||
InputArgument::REQUIRED, | ||
'The path to a line based file' | ||
) | ||
->addArgument( | ||
'option-name', | ||
InputArgument::REQUIRED, | ||
'The name in a (name, value) pair' | ||
) | ||
->addArgument( | ||
'option-value', | ||
InputArgument::REQUIRED, | ||
'The value in a (name, value) pair' | ||
) | ||
->addOption( | ||
'--separator', | ||
's', | ||
InputOption::VALUE_REQUIRED, | ||
'Characters which separate the name from the value in a (name, value) pair.' | ||
) | ||
; | ||
$this->configureCheckMode(); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->activateCheckMode($input); | ||
|
||
if (! file_exists($input->getArgument('file'))) { | ||
throw new RuntimeException( | ||
sprintf('The file "%s" does not exist.', $input->getArgument('file')) | ||
); | ||
} | ||
|
||
if ($input->getOption('separator')) { | ||
$this | ||
->fileFactory | ||
->getLineFactory() | ||
->setFieldSeparator($input->getOption('separator')) | ||
; | ||
} | ||
|
||
$file = $this->fileFactory->makeFile($input->getArgument('file')); | ||
$line = $this->fileFactory->getLineFactory()->makeLine(); | ||
|
||
$line | ||
->setFieldValue( | ||
NameValueLine::FIELD_NAME, | ||
$input->getArgument('option-name') | ||
) | ||
->setFieldValue( | ||
NameValueLine::FIELD_VALUE, | ||
$input->getArgument('option-value') | ||
) | ||
; | ||
|
||
try { | ||
$file->setLine($line); | ||
} catch (UnusableFileException $e) { | ||
throw new RuntimeException( | ||
'I cannot set a line in the file', | ||
null, | ||
$e | ||
); | ||
} | ||
|
||
if (! $file->changed()) { | ||
$output->WriteLn( | ||
sprintf( | ||
'I am not making any changes to the file "%s"; it already has the line "%s%s%s".', | ||
$input->getArgument('file'), | ||
$input->getArgument('option-name'), | ||
$this->fileFactory->getLineFactory()->getFieldSeparator(), | ||
$input->getArgument('option-value') | ||
) | ||
); | ||
$this->reportChange($output); | ||
return 0; | ||
} elseif ($this->checkMode()) { | ||
$this->markChange(); | ||
$output->WriteLn( | ||
sprintf( | ||
'I would make a change to the file "%s".', | ||
$input->getArgument('file') | ||
) | ||
); | ||
$this->reportChange($output); | ||
return 0; | ||
} | ||
|
||
$this->markChange(); | ||
$output->WriteLn( | ||
sprintf( | ||
'I am making your changes to the file "%s".', | ||
$input->getArgument('file') | ||
) | ||
); | ||
$file | ||
->backup($this->backupName($input->getArgument('file'))) | ||
->finish() | ||
; | ||
$this->reportChange($output); | ||
} | ||
|
||
private function backupName($originalName) | ||
{ | ||
return sprintf('%s.%s.backup', $originalName, date('Y-m-d_H-i-s')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace Droid\Plugin\Fs\Model\File; | ||
|
||
abstract class AbstractLine implements LineInterface | ||
{ | ||
protected $fieldSeparator = ' '; | ||
protected $givenValues = array(); | ||
protected $originalLine; | ||
protected $originalValues = array(); | ||
|
||
public function set($content) | ||
{ | ||
$this->originalLine = $content; | ||
|
||
if (trim($this->originalLine, " \t") == '' | ||
|| substr($this->originalLine, 0, 1) == '#' | ||
) { | ||
return $this; | ||
} | ||
|
||
$this->originalValues = $this->parse($this->originalLine); | ||
|
||
return $this; | ||
} | ||
|
||
public function setFieldValue($fieldName, $value) | ||
{ | ||
if (isset($this->originalValues[$fieldName]) | ||
&& $this->originalValues[$fieldName] === $value | ||
) { | ||
# the given value is the same as the original value; ignore any given value | ||
unset($this->givenValues[$fieldName]); | ||
} else { | ||
$this->givenValues[$fieldName] = $value; | ||
} | ||
return $this; | ||
} | ||
|
||
public function getFieldValue($fieldName) | ||
{ | ||
if (array_key_exists($fieldName, $this->givenValues)) { | ||
return $this->givenValues[$fieldName]; | ||
} | ||
if (array_key_exists($fieldName, $this->originalValues)) { | ||
return $this->originalValues[$fieldName]; | ||
} | ||
return null; | ||
} | ||
|
||
public function isData() | ||
{ | ||
return (bool) sizeof($this->originalValues); | ||
} | ||
|
||
public function changed() | ||
{ | ||
if (empty($this->originalValues)) { | ||
return false; | ||
} | ||
return (bool) sizeof($this->givenValues); | ||
} | ||
|
||
public function update(LineInterface $line) | ||
{ | ||
foreach ($this->givenValues as $fieldName => $value) { | ||
$line->setFieldValue($fieldName, $value); | ||
} | ||
return $this; | ||
} | ||
|
||
public function format($values) | ||
{ | ||
return implode($this->fieldSeparator, $values); | ||
} | ||
|
||
public function setFieldSeparator($separator) | ||
{ | ||
$this->fieldSeparator = $separator; | ||
|
||
return $this; | ||
} | ||
|
||
abstract public function getMappingValues(); | ||
|
||
public function __toString() | ||
{ | ||
if ($this->originalValues && $this->givenValues) { | ||
# an updated line | ||
return $this->format( | ||
array_values( | ||
array_replace($this->originalValues, $this->givenValues) | ||
) | ||
); | ||
} elseif ($this->originalValues || $this->originalLine) { | ||
# an unchanged line | ||
return $this->originalLine; | ||
} elseif ($this->givenValues) { | ||
# a new line | ||
return $this->format(array_values($this->givenValues)); | ||
} | ||
return ''; | ||
} | ||
|
||
/** | ||
* Return a mapping of named fields to parsed values. | ||
* | ||
* @param string $data | ||
* | ||
* @return array | ||
*/ | ||
abstract protected function parse($data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Droid\Plugin\Fs\Model\File; | ||
|
||
class FileFactory | ||
{ | ||
private $fileClassName; | ||
private $lineFactory; | ||
|
||
public function __construct( | ||
$fileClassName, | ||
LineFactory $lineFactory | ||
) { | ||
$this->fileClassName = $fileClassName; | ||
$this->lineFactory = $lineFactory; | ||
} | ||
|
||
/** | ||
* @return \Droid\Plugin\Fs\Model\File\LineFactory | ||
*/ | ||
public function getLineFactory() | ||
{ | ||
return $this->lineFactory; | ||
} | ||
|
||
public function makeFile($filename) | ||
{ | ||
return new $this->fileClassName($this->lineFactory, $filename); | ||
} | ||
} |
Oops, something went wrong.