2011-11-24 02:45:04 +01:00
< ? php
2013-11-05 17:12:09 +01:00
/*
* This file is part of the Magallanes package .
*
* ( c ) Andrés Montañez < andres @ andresmontanez . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2013-11-06 15:44:05 +01:00
namespace Mage\Command\BuiltIn ;
use Mage\Command\AbstractCommand ;
use Mage\Console ;
use Exception ;
/**
* Initializes a Magallanes Configuration into a Proyect
*
* @ author Andrés Montañez < andres @ andresmontanez . com >
*/
class InitCommand extends AbstractCommand
2011-11-24 02:45:04 +01:00
{
2013-07-07 01:45:54 +02:00
2013-11-06 15:44:05 +01:00
/**
* Command for Initalize a new Configuration Proyect
* @ see \Mage\Command\AbstractCommand :: run ()
*/
2011-11-24 02:45:04 +01:00
public function run ()
{
$configDir = '.mage' ;
2012-09-21 05:23:07 +02:00
2013-11-06 15:44:05 +01:00
Console :: output ( 'Initiating managing process for application with <dark_gray>Magallanes</dark_gray>' );
2012-09-21 05:23:07 +02:00
2011-11-24 02:45:04 +01:00
// Check if there is already a config dir
if ( file_exists ( $configDir )) {
2013-11-06 15:44:05 +01:00
Console :: output ( '<light_red>Error!!</light_red> Already exists <dark_gray>.mage</dark_gray> directory.' , 1 , 2 );
2011-11-24 02:45:04 +01:00
} else {
$results = array ();
$results [] = mkdir ( $configDir );
$results [] = mkdir ( $configDir . '/logs' );
2013-07-07 01:45:54 +02:00
$results [] = file_put_contents ( $configDir . '/logs/.gitignore' , " * \n !.gitignore " );
2011-11-24 02:45:04 +01:00
$results [] = mkdir ( $configDir . '/tasks' );
2013-07-07 01:45:54 +02:00
$results [] = touch ( $configDir . '/tasks/.gitignore' );
2011-11-24 02:45:04 +01:00
$results [] = mkdir ( $configDir . '/config' );
$results [] = mkdir ( $configDir . '/config/environment' );
2013-07-07 01:45:54 +02:00
$results [] = touch ( $configDir . '/config/environment/.gitignore' );
$results [] = file_put_contents ( $configDir . '/config/general.yml' , $this -> getGeneralConfig ());
2012-09-21 05:23:07 +02:00
2011-11-24 02:45:04 +01:00
if ( ! in_array ( false , $results )) {
2013-11-06 15:44:05 +01:00
Console :: output ( '<light_green>Success!!</light_green> The configuration for <dark_gray>Magallanes</dark_gray> has been generated at <blue>.mage</blue> directory.' );
Console :: output ( '<dark_gray>Please!! Review and adjust the configuration.</dark_gray>' , 2 , 2 );
2011-11-24 02:45:04 +01:00
} else {
2013-11-06 15:44:05 +01:00
Console :: output ( '<light_red>Error!!</light_red> Unable to generate the configuration.' , 1 , 2 );
2011-11-24 02:45:04 +01:00
}
}
}
2013-07-07 01:45:54 +02:00
2013-11-06 15:44:05 +01:00
/**
* Returns the Global Configuration
* @ return string
*/
2013-07-07 01:45:54 +02:00
protected function getGeneralConfig ()
{
// Assamble Global Settings
$projectName = $this -> getConfig () -> getParameter ( 'name' , '' );
$notificationEmail = $this -> getConfig () -> getParameter ( 'email' , '' );
$notificationEnabled = ( $notificationEmail != '' ) ? 'true' : 'false' ;
$globalSettings = str_replace (
array (
'%projectName%' ,
'%notificationEmail%' ,
'%notificationEnabled%' ,
'%loggingEnabled%' ,
'%maxlogs%' ,
),
array (
$projectName ,
$notificationEmail ,
$notificationEnabled ,
'true' ,
30
),
2013-11-07 15:28:21 +01:00
$this -> getGeneralConfigTemplate ()
2013-07-07 01:45:54 +02:00
);
return $globalSettings ;
}
2013-11-06 15:44:05 +01:00
/**
* Returns the YAML Template for the Global Configuration
* @ return string
*/
protected function getGeneralConfigTemplate ()
{
$template = '# global settings' . PHP_EOL
. 'name: %projectName%' . PHP_EOL
. 'email: %notificationEmail%' . PHP_EOL
. 'notifications: %notificationEnabled%' . PHP_EOL
. 'logging: %loggingEnabled%' . PHP_EOL
. 'maxlogs: %maxlogs%' . PHP_EOL ;
return $template ;
}
2011-11-24 02:45:04 +01:00
}