Skip to content

Commit

Permalink
Merge branch 'bluec-filepath'
Browse files Browse the repository at this point in the history
  • Loading branch information
therouv committed Jan 19, 2023
2 parents d15a0a7 + b9c58e8 commit 073e4e9
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea
/.php_cs.cache
/.php-cs-fixer.cache
/composer.lock
/phpunit.xml
15 changes: 10 additions & 5 deletions Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ protected function configure()
'filename',
'f',
InputOption::VALUE_OPTIONAL,
'File name into which should the export be written. Defaults into var directory.'
'Specifies the export file name. Defaults to "config".'
);

$this->addOption(
'filepath',
'p',
InputOption::VALUE_OPTIONAL,
'Specifies the export path where the export file(s) will be written. Defaults to "var/export/config/Ymd_His/".'
);

$this->addOption(
Expand Down Expand Up @@ -139,10 +146,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \InvalidArgumentException(ucfirst($format) . ' file writer could not be instantiated."');
}

$filename = (string) $input->getOption('filename');
if ($filename != '') {
$writer->setBaseFilename($filename);
}
$writer->setBaseFilename((string) $input->getOption('filename'));
$writer->setBaseFilepath((string) $input->getOption('filepath'));

$writer->setOutput($output);
$writer->setIsHierarchical('y' === $input->getOption('hierarchical'));
Expand Down
56 changes: 42 additions & 14 deletions Model/File/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ abstract class AbstractWriter implements WriterInterface
*/
private $baseFilename = null;

/**
* @var string
*/
private $baseFilepath = null;

/**
* @var bool
*/
Expand Down Expand Up @@ -70,10 +75,10 @@ public function write(array $data = [])
}

foreach ($namespacedData as $namespace => $configData) {
$this->_write($this->getFilename($namespace), $configData);
$this->_write($this->getFilenameWithPath($namespace), $configData);
}
} else {
$this->_write($this->getFilename(), $preparedData);
$this->_write($this->getFilenameWithPath(), $preparedData);
}
}

Expand Down Expand Up @@ -137,6 +142,12 @@ public function getFilesystem()
*/
public function setBaseFilename($baseFilename)
{
if (strpos($baseFilename, DIRECTORY_SEPARATOR) !== false) {
throw new \InvalidArgumentException(
'The filename must not contain a directory separator. Use "--filepath" option to set the output directory.'
);
}

$this->baseFilename = $baseFilename;
}

Expand All @@ -148,6 +159,28 @@ public function getBaseFilename()
return $this->baseFilename;
}

/**
* @param string $baseFilepath
*
* @return void
*/
public function setBaseFilepath($baseFilepath)
{
if ($baseFilepath === '') {
$baseFilepath = implode(DIRECTORY_SEPARATOR, ['var', 'export', 'config', date('Ymd_His')]);
}

$this->baseFilepath = ltrim(rtrim($baseFilepath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
}

/**
* @return string
*/
public function getBaseFilepath()
{
return $this->baseFilepath;
}

/**
* @param OutputInterface $output
*
Expand Down Expand Up @@ -211,24 +244,19 @@ public function getIsFilePerNameSpace()
*
* @return string
*/
public function getFilename($namespace = null)
private function getFilenameWithPath($namespace = null)
{
$filename = [
date('Ymd_His'),
$this->getBaseFilepath(),
];

// Check if the a base filename was given
if ($baseFilename = $this->getBaseFilename()) {
$filename[] = $baseFilename;
// Add namespace to file name if specified.
if ($namespace !== null) {
$filename[] = $this->getBaseFilename() === '' ? 'config' . '_' . $namespace : $this->getBaseFilename() . '_' . $namespace;
} else {
$filename[] = 'config';
}

// Add namespace to filename
if (null !== $namespace) {
$filename[] = $namespace;
$filename[] = $this->getBaseFilename() === '' ? 'config' : $this->getBaseFilename();
}

return implode('_', $filename) . '.' . $this->getFileExtension();
return implode('', $filename) . '.' . $this->getFileExtension();
}
}
2 changes: 1 addition & 1 deletion Model/File/Writer/JsonWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function _write($filename, array $data)
}

// Write data to file
$tmpDirectory = $this->getFilesystem()->getDirectoryWrite(DirectoryList::VAR_DIR);
$tmpDirectory = $this->getFilesystem()->getDirectoryWrite(DirectoryList::ROOT);
$tmpDirectory->writeFile($filename, $content);
$this->getOutput()->writeln(sprintf(
'<info>Wrote: %s settings to file %s</info>',
Expand Down
13 changes: 13 additions & 0 deletions Model/File/Writer/WriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,24 @@ public function setBaseFilename($baseFilename);
*/
public function getBaseFilename();

/**
* @param string $baseFilepath
*
* @return void
*/
public function setBaseFilepath($baseFilepath);

/**
* @return string
*/
public function getBaseFilepath();

/**
* @param OutputInterface $output
*
* @return void
*/

public function setOutput(OutputInterface $output);

/**
Expand Down
2 changes: 1 addition & 1 deletion Model/File/Writer/YamlWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function _write($filename, array $data)
}

// Write data to file
$tmpDirectory = $this->getFilesystem()->getDirectoryWrite(DirectoryList::VAR_DIR);
$tmpDirectory = $this->getFilesystem()->getDirectoryWrite(DirectoryList::ROOT);
$tmpDirectory->writeFile($filename, $content);
$this->getOutput()->writeln(sprintf(
'<info>Wrote: %s settings to file %s</info>',
Expand Down
3 changes: 2 additions & 1 deletion docs/config-export.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Usage:
Options:
--format (-m) Format: yaml, json (default: "yaml")
--hierarchical (-a) Create a hierarchical or a flat structure (not all export format supports that). Enable with: y (default: "n")
--filename (-f) File name into which should the export be written. Defaults into var directory.
--filename (-f) Specifies the export file name. Defaults to "config" (when not using "--filePerNameSpace").
--filepath (-p) Specifies the export path where the export file(s) will be written. Defaults to "var/export/config/Ymd_His/".
--include (-i) Path prefix, multiple values can be comma separated; exports only those paths
--includeScope Scope name, multiple values can be comma separated; exports only those scopes
--exclude (-x) Path prefix, multiple values can be comma separated; exports everything except ...
Expand Down

0 comments on commit 073e4e9

Please sign in to comment.