Skip to content

Commit

Permalink
Adding updates to the config writer
Browse files Browse the repository at this point in the history
  • Loading branch information
tnylea committed May 9, 2024
1 parent d1e21fb commit f4c7e88
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/DataWriter/FileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public function __construct(Filesystem $files, string $defaultPath)
*/
public function write(string $item, $value, string $filename, string $fileExtension = '.php'): bool
{
$path = $this->getPath($item, $filename, $fileExtension);

if (!$path) return false;
$path = $filename;

$contents = $this->files->get($path);
$contents = $this->rewriter->toContent($contents, [$item => $value]);
Expand All @@ -66,6 +64,7 @@ private function getPath(string $item, string $filename, string $ext = '.php'):
{
$file = "{$this->defaultPath}/{$filename}{$ext}";

dd($file);
if ($this->files->exists($file) && $this->hasKey($file, $item)) {
return $file;
}
Expand Down
27 changes: 26 additions & 1 deletion src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function __construct(FileWriter $writer, array $items = [])
*/
public function write(string $key, $value): bool
{
list($filename, $item) = $this->parseKey($key);
list($filename, $item) = $this->getFileAndReturnConfigProperty($key);

$result = $this->writer->write($item, $value, $filename);

if(!$result) throw new Exception('File could not be written to');
Expand All @@ -47,6 +48,30 @@ public function write(string $key, $value): bool
return $result;
}

private function getFileAndReturnConfigProperty(string $key): array
{
$pathParts = explode('.', $key);
$path = base_path('config');
$foundFile = null;
$remainingKey = '';

for ($i = 0; $i < count($pathParts); $i++) {
$path .= '/' . $pathParts[$i];
$testPath = $path . '.php';
if (file_exists($testPath)) {
$foundFile = $testPath;
$remainingKey = implode('.', array_slice($pathParts, $i + 1));
break;
}
}

if (!$foundFile) {
throw new Exception("Configuration file for '{$key}' not found.");
}

return [$foundFile, $remainingKey];
}

/**
* Split key into 2 parts. The first part will be the filename
*
Expand Down

0 comments on commit f4c7e88

Please sign in to comment.