Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from baleen/config-refactor
Browse files Browse the repository at this point in the history
Refactored config file storage
  • Loading branch information
gsomoza committed Aug 13, 2015
2 parents 9d172fc + 9c86ac8 commit 09fcbc2
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 123 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,36 @@ With Composer:
composer install baleen/cli
```

And then to initialise Baleen for your project:
Baleen CLI is quite opinionated in its defaults, so it doesn't need extra configuration to run. So if you'd like to just
test-drive the project, you can now jump straight to the "usage" section.

But you can customize almost anything through a configuration file. To create a configuration file, run the following:

```bash
./vendor/bin/baleen init
```

This will generate two files in your working directory:
* `.baleen.yml`: the configuration file.
* `.baleen_versions`: a simple database to keep track of which versions have been migrated. This can later be replaced
* `.baleen_versions`: a simple file to keep track of which versions have been migrated. This can later be replaced
with a database table. You may want to ignore this file in your VCS system (e.g. using Git's `.gitignore` file).

The `.baleen_versions` file will be created for you automatically if you use the default configuration values. You
don't need to run `baleen init` in order for the file to be created.

If you don't want to type `./vendor/bin/baleen` to run baleen commands then you can alternatively use Composer as a
shortcut. Just edit your project's `composer.json` file to add the following:

```json
{
"scripts" {
"baleen": "vendor/bin/baleen --ansi"
// ... other scripts
}
}
```

Now you can run Baleen CLI easily by just typing `composer baleen`!

### Usage

Expand Down
11 changes: 4 additions & 7 deletions config/defaults.php
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',
];
12 changes: 12 additions & 0 deletions config/providers.php
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,
],
];
8 changes: 4 additions & 4 deletions src/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Baleen\Cli\Command;

use Baleen\Cli\Config\ConfigFileStorage;
use Baleen\Cli\Config\ConfigStorage;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -33,19 +33,19 @@ class InitCommand extends AbstractCommand
{
const COMMAND_NAME = 'init';

/** @var ConfigFileStorage */
/** @var ConfigStorage */
protected $configStorage;

/**
* @return ConfigFileStorage
* @return ConfigStorage
*/
public function getConfigStorage()
{
return $this->configStorage;
}

/**
* @param ConfigFileStorage $configStorage
* @param ConfigStorage $configStorage
*/
public function setConfigStorage($configStorage)
{
Expand Down
60 changes: 27 additions & 33 deletions src/Config/ConfigFileStorage.php → src/Config/ConfigStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

/**
Expand All @@ -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();
Expand All @@ -165,7 +159,7 @@ public function getConfig()
/**
* @param AppConfig $config
*/
public function setConfig($config)
public function setAppConfig($config)
{
$this->config = $config;
}
Expand All @@ -180,6 +174,6 @@ public function isLoaded()
*/
public function getConfigFileName()
{
return $this->getConfig()->getConfigFileName();
return $this->getAppConfig()->getConfigFileName();
}
}
25 changes: 12 additions & 13 deletions src/Container/ServiceProvider/AppConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace Baleen\Cli\Container\ServiceProvider;

use Baleen\Cli\Config\AppConfig;
use Baleen\Cli\Config\ConfigFileStorage;
use Baleen\Cli\Config\ConfigStorage;
use League\Container\ServiceProvider;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
Expand Down Expand Up @@ -51,22 +51,21 @@ public function register()
$baseDir = getcwd();
$baleenBaseDir = $this->getContainer()->get(self::BALEEN_BASE_DIR);
$this->getContainer()->singleton(self::SERVICE_CONFIG_STORAGE, function () use ($baseDir, $baleenBaseDir) {
$configFilesystem = new Filesystem(new Local($baseDir));
$defaultFilePath = implode(DIRECTORY_SEPARATOR, [$baleenBaseDir, 'config', 'defaults.php']);
$defaultConfig = [];
if (file_exists($defaultFilePath) && is_readable($defaultFilePath)) {
$defaultConfig = include $defaultFilePath;
$configFiles = glob(implode(DIRECTORY_SEPARATOR, [$baleenBaseDir, 'config', '*.php']));
$localConfig = [];
foreach ($configFiles as $file) {
if (is_file($file)) {
$fileConfig = include $file;
$localConfig = array_merge_recursive($localConfig, $fileConfig);
}
}

return new ConfigFileStorage($configFilesystem, $defaultConfig);
$configFilesystem = new Filesystem(new Local($baseDir));
return new ConfigStorage($configFilesystem, $localConfig);
});
$this->getContainer()->singleton(
self::SERVICE_CONFIG,
function (ConfigFileStorage $configStorage) use ($baseDir) {
return $configStorage->isInitialized(AppConfig::CONFIG_FILE_NAME) ?
// its important to call "load" and not just "read"
$configStorage->load(AppConfig::CONFIG_FILE_NAME) :
new AppConfig();
function (ConfigStorage $configStorage) use ($baseDir) {
return $configStorage->load(AppConfig::CONFIG_FILE_NAME);
}
)->withArgument(self::SERVICE_CONFIG_STORAGE);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Container/ServiceProvider/RepositoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
class RepositoryProvider extends ServiceProvider
{
const SERVICE_REPOSITORY = 'repository';
const SERVICE_FILESYSTEM = 'repository-filesystem';
const SERVICE_FILESYSTEM = 'repository-projectFileSystem';

protected $provides = [
self::SERVICE_REPOSITORY,
Expand All @@ -51,7 +51,6 @@ public function register()

$container->singleton(self::SERVICE_FILESYSTEM, function (AppConfig $appConfig) {
$adapter = new Local(dirname($appConfig->getConfigFilePath()));

return new Filesystem($adapter);
})->withArgument(AppConfigProvider::SERVICE_CONFIG);

Expand Down
6 changes: 3 additions & 3 deletions test/Command/InitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Baleen\Cli\Command\AbstractCommand;
use Baleen\Cli\Command\InitCommand;
use Baleen\Cli\Config\AppConfig;
use Baleen\Cli\Config\ConfigFileStorage;
use Baleen\Cli\Config\ConfigStorage;
use Mockery as m;

/**
Expand All @@ -32,7 +32,7 @@
class InitCommandTest extends CommandTestCase
{

/** @var ConfigFileStorage|m\Mock */
/** @var ConfigStorage|m\Mock */
protected $configStorage;

/**
Expand All @@ -42,7 +42,7 @@ public function setUp()
{
parent::setUp();
$this->instance = m::mock(InitCommand::class)->makePartial();
$this->configStorage = m::mock(ConfigFileStorage::class);
$this->configStorage = m::mock(ConfigStorage::class);
$this->instance->setConfigStorage($this->configStorage);
}

Expand Down
Loading

0 comments on commit 09fcbc2

Please sign in to comment.