@ -1,88 +1,132 @@
<?php
class Mage_Config
{
private $_environmentName = null;
private $_environment = null;
private $_scm = null;
private $_general = null;
private $_arguments = array();
private $_parameters = array();
private $_environment = false;
private $_host = null;
private $_releaseId = null;
public function reloadConfig()
private $_config = array(
'general' => array(),
'scm' => array(),
'environment' => array(),
);
/**
* Load the Configuration and parses the Arguments
*
* @param array $arguments
*/
public function load($arguments)
{
$this->loadGeneral();
$this->loadSCM();
$this->loadEnvironment($this->getEnvironmentName());
}
public function loadEnvironment($environment)
$this->_parse($arguments);
$this->_loadGeneral();
$this->_loadSCM();
$this->_loadEnvironment();
}
/**
* Return the invocation argument based on a position
* 0 = Invoked Command Name
*
* @param integer $position
* @return mixed
*/
public function getArgument($position = 0)
{
if (($environment != '') & & file_exists('.mage/config/environment/' . $environment . '.yml')) {
$this->_environment = spyc_load_file('.mage/config/environment/' . $environment . '.yml');
$this->_environmentName = $environment;
// Create temporal directory for clone
if (isset($this->_environment['deployment']['source']) & & is_array($this->_environment['deployment']['source'])) {
if (trim($this->_environment['deployment']['source']['temporal']) == '') {
$this->_environment['deployment']['source']['temporal'] = '/tmp';
}
$newTemporal = rtrim($this->_environment['deployment']['source']['temporal'], '/')
. '/' . md5(microtime()) . '/';
$this->_environment['deployment']['source']['temporal'] = $newTemporal;
}
return true;
} else if (($environment != '') & & !file_exists('.mage/config/environment/' . $environment . '.yml')) {
if (isset($this->_arguments[$position])) {
return $this->_arguments[$position];
} else {
return false;
}
}
return true;
/**
* Returns all the invocation arguments
*
* @return array
*/
public function getArguments()
{
return $this->_arguments;
}
public function loadSCM()
/**
* Return the a parameter
*
* @param string $name
* @return mixed
*/
public function getParameter($name, $default = null)
{
if (file_exists('.mage/config/scm.yml')) {
$this->_scm = spyc_load_file('.mage/config/scm.yml');
if (isset($this->_parameters[$name])) {
return $this->_parameters[$name];
} else {
return $default;
}
}
public function loadGeneral()
/**
* Returns all the invocation arguments
*
* @return array
*/
public function getParameters()
{
if (file_exists('.mage/config/general.yml')) {
$this->_general = spyc_load_file('.mage/config/general.yml');
}
return $this->_parameters;
}
/**
* Returns the Current environment
*
* @return mixed
*/
public function getEnvironment()
{
return $this->_environment;
}
public function getEnvironmentName()
/**
* Reloads the configuration
*/
public function reload()
{
return $this->_environmentName;
$this->_loadGeneral();
$this->_loadSCM();
$this->_loadEnvironment();
}
public function getSCM()
/**
* Get the Tasks to execute
*
* @param string $stage
* @return array
*/
public function getTasks($stage = 'on-deploy')
{
return $this->_scm;
$tasks = array();
$config = $this->_getEnvironmentOption('tasks', array());
if (isset($config[$stage])) {
$tasks = ($config[$stage] ? (array) $config[$stage] : array());
}
public function getGlobal()
{
return $this->_global;
return $tasks;
}
/**
* Get the current Hosts to deploy
*
* @return array
*/
public function getHosts()
{
$config = $this->getEnvironment();
$hosts = array();
if (isset($config['hosts'])) {
if (is_array($config['hosts'])) {
$hosts = (array) $config['hosts'];
} else if (is_string($config['hosts'])) {
$fileContent = fopen($config['hosts'], 'r');
if (isset($this->_ config['environment'] ['hosts'])) {
if (is_array($this->_ config['environment'] ['hosts'])) {
$hosts = (array) $this->_ config['environment'] ['hosts'];
} else if (is_string($this->_ config['environment']['hosts']) & & file_exists($this->_config['environment']['hosts']) & & is_readable($this->_config['environment'] ['hosts'])) {
$fileContent = fopen($this->_ config['environment'] ['hosts'], 'r');
while (($host = fgets($fileContent)) == true) {
$host = trim($host);
if ($host != '') {
@ -95,18 +139,34 @@ class Mage_Config
return $hosts;
}
/**
* Set the current host
*
* @param string $host
* @return Mage_Config
*/
public function setHost($host)
{
$this->_host = $host;
return $this;
}
/**
* Get the current host name
*
* @return string
*/
public function getHostName()
{
$info = explode(':', $this->_host);
return $info[0];
}
/**
* Get the current Host Port
*
* @return unknown
*/
public function getHostPort()
{
$info = explode(':', $this->_host);
@ -114,144 +174,222 @@ class Mage_Config
return $info[1];
}
/**
* Get the current Host
*
* @return string
*/
public function getHost()
{
return $this->_host;
}
public function setReleaseId($id)
{
$this->_releaseId = $id;
return $this;
}
public function getReleaseId()
{
return $this->_releaseId;
}
public function getTasks($stage = 'on-deploy')
/**
* Gets General Configuration
*
* @param string $option
* @param string $default
* @return mixed
*/
public function general($option, $default = false)
{
switch ($stage) {
case 'pre-deploy':
$type = 'tasks';
$stage = 'pre-deploy';
break;
case 'post-deploy':
$type = 'tasks';
$stage = 'post-deploy';
break;
case 'post-release':
$type = 'releases';
$stage = 'post-release';
break;
case 'on-deploy':
default:
$type = 'tasks';
$stage = 'on-deploy';
break;
$config = $this->_config['general'];
if (isset($config[$option])) {
if (is_array($default) & & ($config[$option] == '')) {
return $default;
} else {
return $config[$option];
}
$tasks = array();
$config = $this->getEnvironment();
if (isset($config[$type]) & & isset($config[$type][$stage])) {
$tasks = ($config[$type][$stage] ? (array) $config[$type][$stage] : array());
} else {
return $default;
}
return $tasks;
}
public function getConfig($host = false)
/**
* Gets SCM Configuration
*
* @param string $option
* @param string $default
* @return mixed
*/
public function scm($option, $default = false)
{
$taskConfig = array();
$taskConfig['deploy'] = $this->getEnvironment();
$taskConfig['deploy']['host'] = $host;
$taskConfig['scm'] = $this->getSCM();
unset($taskConfig['deploy']['tasks']);
unset($taskConfig['deploy']['hosts']);
return $taskConfig ;
$config = $this->_config['scm'] ;
if (isset($config[$option])) {
if (is_array($default) & & ($config[$option] == '')) {
return $default ;
} else {
return $config[$option] ;
}
} else {
return $defaul t;
}
public function setFrom($from)
{
$this->_environment['deployment']['from'] = $from;
return $this;
}
/**
* Get deployment configuration
*
* @param string $option
* @param string $default
* @return string
*/
public function deployment($option, $default = false)
{
$options = $this->getEnvironment();
if (isset($options['deployment'][$option])) {
if (is_array($default) & & ($options['deployment'][$option] == '')) {
$config = $this->_getEnvironmentOption('deployment', array() );
if (isset($config [$option])) {
if (is_array($default) & & ($config [$option] == '')) {
return $default;
} else {
return $options['deployment'] [$option];
return $config [$option];
}
} else {
return $default;
}
}
/**
* Returns Releaseing Options
*
* @param string $option
* @param string $default
* @return mixed
*/
public function release($option, $default = false)
{
$options = $this->getEnvironment();
if (isset($options['releases'][$option])) {
if (is_array($default) & & ($options['releases'][$option] == '')) {
$config = $this->_getEnvironmentOption('releases', array() );
if (isset($config [$option])) {
if (is_array($default) & & ($config [$option] == '')) {
return $default;
} else {
return $options['releases'] [$option];
return $config [$option];
}
} else {
return $default;
}
}
public function scm($option, $default = false)
/**
* Set From Deployment Path
*
* @param string $from
* @return Mage_Config
*/
public function setFrom($from)
{
$options = $this->_scm;
if (isset($options[$option])) {
if (is_array($default) & & ($options[$option] == '')) {
return $default;
} else {
return $options[$option];
$this->_config['environment']['deployment']['from'] = $from;
return $this;
}
return $options[$option];
} else {
return $default;
/**
* Sets the Current Release ID
*
* @param integer $id
* @return Mage_Config
*/
public function setReleaseId($id)
{
$this->_releaseId = $id;
return $this;
}
/**
* Gets the Current Release ID
*
* @return integer
*/
public function getReleaseId()
{
return $this->_releaseId;
}
public function general($option, $default = false)
/**
* Parse the Command Line options
* @return boolean
*/
private function _parse($arguments)
{
$options = $this->_general;
if (isset($options[$option])) {
if (is_array($default) & & ($options[$option] == '')) {
return $default;
} else {
return $options[$option];
foreach ($arguments as $argument) {
if (preg_match('/to:[\w]+/i', $argument)) {
$this->_environment = str_replace('to:', '', $argument);
} else if (preg_match('/--[\w]+/i', $argument)) {
$optionValue = explode('=', substr($argument, 2));
if (count($optionValue) == 1) {
$this->_parameters[$optionValue[0]] = true;
} else if (count($optionValue) == 2) {
$this->_parameters[$optionValue[0]] = $optionValue[1];
}
} else {
return $default;
$this->_arguments[] = $argument;
}
}
}
public function mail($option, $default = false)
/**
* Loads the General Configuration
*/
private function _loadGeneral()
{
$options = $this->_general;
if (isset($options['mail'][$option])) {
if (is_array($default) & & ($options['mail'][$option] == '')) {
return $default;
} else {
return $options['mail'][$option];
if (file_exists('.mage/config/general.yml')) {
$this->_config['general'] = spyc_load_file('.mage/config/general.yml');
}
}
/**
* Loads the SCM Configuration
*/
private function _loadSCM()
{
if (file_exists('.mage/config/scm.yml')) {
$this->_config['scm'] = spyc_load_file('.mage/config/scm.yml');
}
}
/**
* Loads the Environment configuration
*
* @throws Exception
* @return boolean
*/
private function _loadEnvironment()
{
$environment = $this->getEnvironment();
if (($environment != false) & & file_exists('.mage/config/environment/' . $environment . '.yml')) {
$this->_config['environment'] = spyc_load_file('.mage/config/environment/' . $environment . '.yml');
// Create temporal directory for clone
if (isset($this->_config['environment']['deployment']['source']) & & is_array($this->_config['environment']['deployment']['source'])) {
if (trim($this->_config['environment']['deployment']['source']['temporal']) == '') {
$this->_config['environment']['deployment']['source']['temporal'] = '/tmp';
}
$newTemporal = rtrim($this->_config['environment']['deployment']['source']['temporal'], '/')
. '/' . md5(microtime()) . '/';
$this->_config['environment']['deployment']['source']['temporal'] = $newTemporal;
}
return true;
} else if (($environment != '') & & !file_exists('.mage/config/environment/' . $environment . '.yml')) {
throw new Exception('Environment does not exists.');
}
return false;
}
/**
* Get Environment root option
*
* @param string $option
* @param mixed $default
* @return mixed
*/
private function _getEnvironmentOption($option, $default = array())
{
$config = $this->_config['environment'];
if (isset($config[$option])) {
return $config[$option];
} else {
return $default;
}
}
}