Skip to content

Commit

Permalink
use php config
Browse files Browse the repository at this point in the history
  • Loading branch information
sokil committed Mar 23, 2015
1 parent 1ff5bfe commit c096f88
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 22 deletions.
28 changes: 28 additions & 0 deletions mongo-migrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

return array(
'default_environment' => 'development',
'path' => array(
'migrations' => 'migrations'
),
'environments' => array(
'development' => array(
'dsn' => 'mongodb',
'default_database' => 'test',
'log_database' => 'test',
'log_collection' => 'migrations',
),
'staging' => array(
'dsn' => 'mongodb',
'default_database' => 'test',
'log_database' => 'test',
'log_collection' => 'migrations',
),
'production' => array(
'dsn' => 'mongodb',
'default_database' => 'test',
'log_database' => 'test',
'log_collection' => 'migrations',
),
),
);
12 changes: 4 additions & 8 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
class Config
{
private $_config;

private $_configFilePath;

public function __construct($path)
{
$this->_configFilePath = rtrim($path, '/');

$this->_config = Yaml::parse($path);
public function __construct(array $config)
{
$this->_config = $config;
}

public function __get($name)
Expand Down Expand Up @@ -43,7 +39,7 @@ public function get($name)

public function getMigrationsDir()
{
return dirname($this->_configFilePath) . '/' . trim($this->_config['path']['migrations'], '/');
return getcwd() . '/' . trim($this->_config['path']['migrations'], '/');
}

public function getDefaultEnvironment()
Expand Down
36 changes: 32 additions & 4 deletions src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sokil\Mongo\Migrator\Console;

use Sokil\Mongo\Migrator\Config;
use Sokil\Mongo\Migrator\Console\Exception\ConfigurationNotFound;

abstract class Command extends \Symfony\Component\Console\Command\Command
{
Expand All @@ -14,7 +15,7 @@ abstract class Command extends \Symfony\Component\Console\Command\Command
*/
private $_manager;

const CONFIG_FILENAME = 'mongo-migrator.yaml';
const CONFIG_FILENAME = 'mongo-migrator';

/**
*
Expand All @@ -23,15 +24,42 @@ abstract class Command extends \Symfony\Component\Console\Command\Command
protected function getConfig()
{
if(!$this->_config) {
$this->_config = new Config($this->getConfigPath());
$this->_config = new Config($this->readConfig());
}

return $this->_config;
}

public function getConfigPath()
private function readConfig()
{
return getcwd() . '/' . self::CONFIG_FILENAME;
$filename = $this->getProjectRoot() . '/' . self::CONFIG_FILENAME;

$yamlFilename = $filename . '.yaml';
if(file_exists($yamlFilename)) {
return Yaml::parse($yamlFilename);
}

$phpFilename = $filename . '.php';
if(file_exists($phpFilename)) {
return require($phpFilename);
}

throw new ConfigurationNotFound('Config not found');
}

public function isProjectInitialisd()
{
try {
$config = $this->getConfig();
return (bool) $config;
} catch (ConfigurationNotFound $e) {
return false;
}
}

public function getProjectRoot()
{
return getcwd();
}

/**
Expand Down
27 changes: 21 additions & 6 deletions src/Console/Command/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,48 @@

namespace Sokil\Mongo\Migrator\Console\Command;

use Sokil\Mongo\Migrator\Console\Exception\ConfigurationNotFound;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Init extends \Sokil\Mongo\Migrator\Console\Command
{
private $allowedConfigFormats = array('yaml', 'php');

protected function configure()
{
$this
->setName('init')
->setDescription('Initialize migrations project')
->setHelp('Create migrations project');
->setHelp('Create migrations project')
->addOption(
'--configFormat', '-f',
InputArgument::OPTIONAL,
'Format of config (use one of "' . implode('","', $this->allowedConfigFormats) . '")',
'yaml'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$configPath = $this->getConfigPath();
if(file_exists($configPath)) {
{
if($this->isProjectInitialisd()) {
throw new \Exception('Migration project already initialised');
}

// check permissions
if(!is_writable(dirname($configPath))) {
$configPath = $this->getProjectRoot();
if(!is_writable($this->getProjectRoot())) {
throw new \Exception('Directory ' . $configPath . ' must be writeabe');
}

$configFormat = $input->getOption('configFormat');
if(!in_array($configFormat, $this->allowedConfigFormats)) {
throw new \Exception('Config format "' . $configFormat . '" not allowed');
}

// copy config to target path
$configPatternPath = __DIR__ . '/../../../' . self::CONFIG_FILENAME;
$configPatternPath = __DIR__ . '/../../../' . self::CONFIG_FILENAME . '.' . $configFormat;
if(!copy($configPatternPath, $configPath)) {
throw new \Exception('Can\'t write config to target directory <info>' . $configPath . '</info>');
}
Expand Down
9 changes: 9 additions & 0 deletions src/Console/Exception/ConfigurationNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Sokil\Mongo\Migrator\Console\Exception;

class ConfigurationNotFound extends \Exception
{

}

6 changes: 4 additions & 2 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Sokil\Mongo\Migrator;

use Symfony\Component\Yaml\Yaml;

class ConfigTest extends \PHPUnit_Framework_TestCase
{
protected $_config;

public function setUp()
{
$configFile = __DIR__ . '/' . \Sokil\Mongo\Migrator\Console\Command::CONFIG_FILENAME;
$this->_config = new Config($configFile);
$configFile = __DIR__ . '/' . \Sokil\Mongo\Migrator\Console\Command::CONFIG_FILENAME . '.yaml';
$this->_config = new Config(Yaml::parse($configFile));
}

public function testGet()
Expand Down
11 changes: 9 additions & 2 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Sokil\Mongo\Migrator;

use Symfony\Component\Yaml\Yaml;

class ManagerMock extends \Sokil\Mongo\Migrator\Manager
{
public function resetCollection($environment)
Expand All @@ -16,8 +18,13 @@ class ManagerTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$configFile = __DIR__ . '/' . \Sokil\Mongo\Migrator\Console\Command::CONFIG_FILENAME;
$config = new Config($configFile);
$configFile = __DIR__ . '/' . \Sokil\Mongo\Migrator\Console\Command::CONFIG_FILENAME . '.yaml';
try {
$config = new Config(Yaml::parse($configFile));
} catch (\Exception $ex) {
var_export($ex);

}

$this->_manager = new ManagerMock($config);

Expand Down

0 comments on commit c096f88

Please sign in to comment.