-
Notifications
You must be signed in to change notification settings - Fork 10
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 #28 from swiftotter/21-config-loader-changes
#21 - Config loader changes, refactoring and cleanup
- Loading branch information
Showing
13 changed files
with
294 additions
and
513 deletions.
There are no files selected for viewing
File renamed without changes.
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Driver\System\Configuration; | ||
|
||
use function array_filter; | ||
use function array_map; | ||
use function array_merge; | ||
use function array_reverse; | ||
use function array_unique; | ||
use function file_exists; | ||
|
||
class FileCollector | ||
{ | ||
private const FILE_EXTENSION = '.yaml'; | ||
private const ALLOWED_FOLDERS = [ | ||
'config', | ||
'config.d' | ||
]; | ||
private const ALLOWED_FILES = [ | ||
'anonymize', | ||
'pipelines', | ||
'commands', | ||
'engines', | ||
'connections', | ||
'config', | ||
'reduce', | ||
'environments' | ||
]; | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function get(): array | ||
{ | ||
$folderCollection = (new FolderCollectionFactory())->create(self::ALLOWED_FOLDERS); | ||
$output = []; | ||
|
||
foreach ($folderCollection as $folder) { | ||
$files = array_filter(self::ALLOWED_FILES, function ($file) use ($folder) { | ||
return file_exists($folder . '/' . $file . self::FILE_EXTENSION); | ||
}); | ||
|
||
$output = array_merge($output, array_map(function ($file) use ($folder) { | ||
return $folder . '/' . $file . self::FILE_EXTENSION; | ||
}, $files)); | ||
} | ||
|
||
return array_unique(array_reverse($output)); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getIndividual(string $file): array | ||
{ | ||
$folderCollection = (new FolderCollectionFactory())->create(self::ALLOWED_FOLDERS); | ||
$output = []; | ||
|
||
foreach ($folderCollection as $folder) { | ||
$path = $folder . '/' . $file . self::FILE_EXTENSION; | ||
|
||
if (file_exists($path)) { | ||
$output[] = $path; | ||
} | ||
} | ||
|
||
return $output; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Driver\System\Configuration; | ||
|
||
use Exception; | ||
|
||
use function file_exists; | ||
|
||
class FileLoader | ||
{ | ||
/** | ||
* Returns the contents of a file. | ||
* @throws Exception | ||
*/ | ||
public function load(string $file): string | ||
{ | ||
if (!$file || !file_exists($file)) { | ||
throw new Exception("{$file} doesn't exist."); | ||
} | ||
$content = file_get_contents($file); | ||
if ($content === false) { | ||
throw new Exception("Unable to load {$file}"); | ||
} | ||
return $content; | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Driver\System\Configuration; | ||
|
||
class FolderCollection implements \Iterator | ||
{ | ||
/** @var string[] */ | ||
private array $folders; | ||
|
||
private int $position = 0; | ||
|
||
/** | ||
* @param string[] $folders | ||
*/ | ||
public function __construct(array $folders) | ||
{ | ||
$this->folders = $folders; | ||
} | ||
|
||
public function current(): string | ||
{ | ||
return $this->folders[$this->position]; | ||
} | ||
|
||
public function next(): void | ||
{ | ||
++$this->position; | ||
} | ||
|
||
public function key(): int | ||
{ | ||
return $this->position; | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
return isset($this->folders[$this->position]); | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->position = 0; | ||
} | ||
} |
Oops, something went wrong.