This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
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 #11 from baleen/config-refactor
Refactored config file storage
- Loading branch information
Showing
10 changed files
with
121 additions
and
123 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 |
---|---|---|
@@ -1,12 +1,9 @@ | ||
<?php | ||
|
||
return [ | ||
'providers' => [ | ||
'application' => \Baleen\Cli\Container\ServiceProvider\DefaultProvider::class, | ||
'storage' => \Baleen\Cli\Container\ServiceProvider\StorageProvider::class, | ||
'repository' => \Baleen\Cli\Container\ServiceProvider\RepositoryProvider::class, | ||
'timeline' => \Baleen\Cli\Container\ServiceProvider\TimelineProvider::class, | ||
'helperSet' => \Baleen\Cli\Container\ServiceProvider\HelperSetProvider::class, | ||
'commands' => \Baleen\Cli\Container\ServiceProvider\CommandsProvider::class, | ||
'migrations' => [ | ||
'directory' => 'migrations', | ||
'namespace' => 'Migrations', | ||
], | ||
'storage_file' => '.baleen_versions', | ||
]; |
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,12 @@ | ||
<?php | ||
|
||
return [ | ||
'providers' => [ | ||
'application' => \Baleen\Cli\Container\ServiceProvider\DefaultProvider::class, | ||
'storage' => \Baleen\Cli\Container\ServiceProvider\StorageProvider::class, | ||
'repository' => \Baleen\Cli\Container\ServiceProvider\RepositoryProvider::class, | ||
'timeline' => \Baleen\Cli\Container\ServiceProvider\TimelineProvider::class, | ||
'helperSet' => \Baleen\Cli\Container\ServiceProvider\HelperSetProvider::class, | ||
'commands' => \Baleen\Cli\Container\ServiceProvider\CommandsProvider::class, | ||
], | ||
]; |
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 |
---|---|---|
|
@@ -26,65 +26,58 @@ | |
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
* Class ConfigFileStorage. | ||
* Class ConfigStorage. | ||
* | ||
* @author Gabriel Somoza <[email protected]> | ||
*/ | ||
class ConfigFileStorage | ||
class ConfigStorage | ||
{ | ||
/** @var FilesystemInterface */ | ||
protected $filesystem; | ||
protected $projectFileSystem; | ||
|
||
/** @var AppConfig */ | ||
protected $config; | ||
|
||
/** @var array */ | ||
protected $defaultConfig; | ||
|
||
/** @var Processor */ | ||
protected $processor; | ||
|
||
/** @var ConfigurationDefinition */ | ||
protected $definition; | ||
|
||
/** | ||
* ConfigFileStorage constructor. | ||
* ConfigStorage constructor. | ||
* | ||
* @param FilesystemInterface $filesystem | ||
* @param FilesystemInterface $projectFileSystem | ||
* @param array $defaultConfig | ||
*/ | ||
public function __construct(FilesystemInterface $filesystem, array $defaultConfig = []) | ||
public function __construct(FilesystemInterface $projectFileSystem, array $defaultConfig = []) | ||
{ | ||
$this->filesystem = $filesystem; | ||
$this->projectFileSystem = $projectFileSystem; | ||
$this->defaultConfig = $defaultConfig; | ||
|
||
$this->processor = new Processor(); | ||
$this->definition = new ConfigurationDefinition(); | ||
} | ||
|
||
/** | ||
* @param $file | ||
* @param $externalFile | ||
* | ||
* @return AppConfig | ||
* | ||
* @throws CliException | ||
*/ | ||
public function read($file = null) | ||
public function read($externalFile = null) | ||
{ | ||
if (null === $file) { | ||
$file = AppConfig::CONFIG_FILE_NAME; | ||
} | ||
if (!$this->filesystem->has($file)) { | ||
throw new CliException(sprintf( | ||
'Configuration file "%s" could not be read.', | ||
$file | ||
)); | ||
if (null === $externalFile) { | ||
$externalFile = AppConfig::CONFIG_FILE_NAME; | ||
} | ||
$configs = []; | ||
if (!empty($this->defaultConfig)) { | ||
$configs[] = $this->defaultConfig; | ||
} | ||
$configs[] = Yaml::parse($this->filesystem->read($file)); | ||
if ($this->projectFileSystem->has($externalFile)) { | ||
$configs[] = Yaml::parse($this->projectFileSystem->read($externalFile)); | ||
} | ||
|
||
$config = $this->processor->processConfiguration( | ||
$this->definition, | ||
|
@@ -106,29 +99,30 @@ public function read($file = null) | |
public function load($file = null) | ||
{ | ||
$config = $this->read($file); | ||
$this->setConfig($config); | ||
$this->setAppConfig($config); | ||
|
||
return $config; | ||
} | ||
|
||
/** | ||
* @param null $file | ||
* @param bool $defaultsOnly | ||
* | ||
* @return bool | ||
* | ||
* @throws CliException | ||
*/ | ||
public function write($file = null) | ||
public function write($file = null, $defaultsOnly = true) | ||
{ | ||
if (!$this->isLoaded()) { | ||
throw new CliException('Configuration file not loaded. Nothing to write!'); | ||
} | ||
if (null === $file) { | ||
$file = $this->config->getConfigFileName(); | ||
} | ||
$contents = Yaml::dump(['baleen' => $this->config->toArray()]); | ||
$config = $this->getAppConfig()->toArray(); | ||
if ($defaultsOnly) { | ||
unset($config['providers']); // we don't want to write that | ||
} | ||
$contents = Yaml::dump($config); | ||
|
||
return $this->filesystem->write($file, $contents); | ||
return $this->projectFileSystem->write($file, $contents); | ||
} | ||
|
||
/** | ||
|
@@ -147,13 +141,13 @@ public function isInitialized($pathOrConfig = null) | |
$path = $pathOrConfig; | ||
} | ||
|
||
return null === $path ? false : $this->filesystem->has($path); | ||
return null === $path ? false : $this->projectFileSystem->has($path); | ||
} | ||
|
||
/** | ||
* @return AppConfig | ||
*/ | ||
public function getConfig() | ||
public function getAppConfig() | ||
{ | ||
if (null === $this->config) { | ||
$this->config = new AppConfig(); | ||
|
@@ -165,7 +159,7 @@ public function getConfig() | |
/** | ||
* @param AppConfig $config | ||
*/ | ||
public function setConfig($config) | ||
public function setAppConfig($config) | ||
{ | ||
$this->config = $config; | ||
} | ||
|
@@ -180,6 +174,6 @@ public function isLoaded() | |
*/ | ||
public function getConfigFileName() | ||
{ | ||
return $this->getConfig()->getConfigFileName(); | ||
return $this->getAppConfig()->getConfigFileName(); | ||
} | ||
} |
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
Oops, something went wrong.