-
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #40 from gacela-project/feature/refactor-config
Refactor Config: extract ConfigInit logic
- Loading branch information
Showing
9 changed files
with
353 additions
and
116 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,113 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gacela\Framework\Config; | ||
|
||
final class ConfigInit | ||
{ | ||
private string $applicationRootDir; | ||
|
||
private GacelaJsonConfigFactoryInterface $configFactory; | ||
|
||
private PathFinderInterface $pathFinder; | ||
|
||
/** @var array<string,ConfigReaderInterface> */ | ||
private array $readers; | ||
|
||
/** | ||
* @param array<string,ConfigReaderInterface> $readers | ||
*/ | ||
public function __construct( | ||
string $applicationRootDir, | ||
GacelaJsonConfigFactoryInterface $configFactory, | ||
PathFinderInterface $pathFinder, | ||
array $readers | ||
) { | ||
$this->applicationRootDir = $applicationRootDir; | ||
$this->configFactory = $configFactory; | ||
$this->pathFinder = $pathFinder; | ||
$this->readers = $readers; | ||
} | ||
|
||
public function readAll(): array | ||
{ | ||
$gacelaJsonConfig = $this->configFactory->createGacelaJsonConfig(); | ||
$configs = []; | ||
|
||
foreach ($this->scanAllConfigFiles($gacelaJsonConfig) as $absolutePath) { | ||
$configs[] = $this->readConfigFromFile($gacelaJsonConfig, $absolutePath); | ||
} | ||
|
||
$configs[] = $this->readLocalConfigFile($gacelaJsonConfig); | ||
|
||
return array_merge(...$configs); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
private function scanAllConfigFiles(GacelaJsonConfig $gacelaJsonConfig): array | ||
{ | ||
$configGroup = array_map( | ||
fn (GacelaJsonConfigItem $config): array => array_map( | ||
static fn ($p): string => (string)$p, | ||
array_diff( | ||
$this->pathFinder->matchingPattern($this->generateAbsolutePath($config->path())), | ||
[$this->generateAbsolutePath($config->pathLocal())] | ||
) | ||
), | ||
$gacelaJsonConfig->configs() | ||
); | ||
|
||
return array_merge(...$configGroup); | ||
} | ||
|
||
private function readConfigFromFile(GacelaJsonConfig $gacelaJson, string $absolutePath): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($gacelaJson->configs() as $config) { | ||
$result[] = $this->readConfigItem($config, $absolutePath); | ||
} | ||
|
||
return array_merge(...array_filter($result)); | ||
} | ||
|
||
private function readLocalConfigFile(GacelaJsonConfig $gacelaJson): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($gacelaJson->configs() as $config) { | ||
$absolutePath = $this->generateAbsolutePath($config->pathLocal()); | ||
|
||
$result[] = $this->readConfigItem($config, $absolutePath); | ||
} | ||
|
||
return array_merge(...array_filter($result)); | ||
} | ||
|
||
private function generateAbsolutePath(string $relativePath): string | ||
{ | ||
return sprintf( | ||
'%s/%s', | ||
$this->applicationRootDir, | ||
$relativePath | ||
); | ||
} | ||
|
||
private function readConfigItem(GacelaJsonConfigItem $config, string $absolutePath): array | ||
{ | ||
$reader = $this->readers[$config->type()] ?? null; | ||
|
||
if ($reader === null) { | ||
throw ConfigReaderException::notSupported($config->type(), $this->readers); | ||
} | ||
|
||
if ($reader->canRead($absolutePath)) { | ||
return $reader->read($absolutePath); | ||
} | ||
|
||
return []; | ||
} | ||
} |
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
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gacela\Framework\Config; | ||
|
||
final class GacelaJsonConfigFactory implements GacelaJsonConfigFactoryInterface | ||
{ | ||
private string $applicationRootDir; | ||
private string $gacelaConfigFilename; | ||
|
||
public function __construct( | ||
string $applicationRootDir, | ||
string $gacelaConfigFilename | ||
) { | ||
$this->applicationRootDir = $applicationRootDir; | ||
$this->gacelaConfigFilename = $gacelaConfigFilename; | ||
} | ||
|
||
public function createGacelaJsonConfig(): GacelaJsonConfig | ||
{ | ||
$gacelaJsonPath = $this->applicationRootDir . '/' . $this->gacelaConfigFilename; | ||
|
||
if (is_file($gacelaJsonPath)) { | ||
/** @psalm-suppress MixedArgumentTypeCoercion */ | ||
return GacelaJsonConfig::fromArray( | ||
(array)json_decode(file_get_contents($gacelaJsonPath), true) | ||
); | ||
} | ||
|
||
return GacelaJsonConfig::withDefaults(); | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gacela\Framework\Config; | ||
|
||
interface GacelaJsonConfigFactoryInterface | ||
{ | ||
public function createGacelaJsonConfig(): GacelaJsonConfig; | ||
} |
Oops, something went wrong.