2011-11-23 23:45:04 -02:00
<? php
2013-11-05 14:12:09 -02: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 12:44:05 -02:00
namespace Mage\Command\BuiltIn ;
use Mage\Command\AbstractCommand ;
use Mage\Console ;
/**
* Initializes a Magallanes Configuration into a Proyect
*
* @author Andrés Montañez <andres@andresmontanez.com>
*/
class InitCommand extends AbstractCommand
2011-11-23 23:45:04 -02:00
{
2015-03-08 14:46:39 +01:00
public function __construct ()
{
$this -> setName ( 'Initialize command' )
-> setHelpMessage ( 'Initialize Magallanes project, create .mage directory with starter configs' )
-> setSyntaxMessage ( 'mage init --name=[project_name] [--email=[author_email]]' )
-> addUsageExample (
'mage init --name="My awesome project"' ,
'Initialize "My awesome project" configuration'
)
-> addUsageExample (
'mage init --name="My project" --email="john.smith@example.com"' ,
'Initialize "My project" configuration with email notification enabled for john.smith@example.com'
);
}
2013-07-06 20:45:54 -03:00
2014-08-06 14:01:39 -03:00
/**
* Command for Initalize a new Configuration Proyect
* @see \Mage\Command\AbstractCommand::run()
*/
2011-11-23 23:45:04 -02:00
public function run ()
{
2014-08-06 21:43:29 -03:00
$exitCode = 50 ;
2014-07-16 15:52:23 +02:00
$configDir = getcwd () . '/.mage' ;
2012-09-21 00:23:07 -03:00
2014-11-01 19:31:04 -02:00
Console :: output ( 'Initiating managing process for application with <bold>Magallanes</bold>' );
2012-09-21 00:23:07 -03:00
2011-11-23 23:45:04 -02:00
// Check if there is already a config dir
if ( file_exists ( $configDir )) {
2014-11-01 19:31:04 -02:00
Console :: output ( '<light_red>Error!!</light_red> Already exists <bold>.mage</bold> directory.' , 1 , 2 );
2011-11-23 23:45:04 -02:00
} else {
$results = array ();
$results [] = mkdir ( $configDir );
$results [] = mkdir ( $configDir . '/logs' );
2013-07-06 20:45:54 -03:00
$results [] = file_put_contents ( $configDir . '/logs/.gitignore' , "* \n !.gitignore" );
2011-11-23 23:45:04 -02:00
$results [] = mkdir ( $configDir . '/tasks' );
2013-07-06 20:45:54 -03:00
$results [] = touch ( $configDir . '/tasks/.gitignore' );
2011-11-23 23:45:04 -02:00
$results [] = mkdir ( $configDir . '/config' );
$results [] = mkdir ( $configDir . '/config/environment' );
2013-07-06 20:45:54 -03:00
$results [] = touch ( $configDir . '/config/environment/.gitignore' );
$results [] = file_put_contents ( $configDir . '/config/general.yml' , $this -> getGeneralConfig ());
2012-09-21 00:23:07 -03:00
2011-11-23 23:45:04 -02:00
if ( ! in_array ( false , $results )) {
2014-11-01 19:31:04 -02:00
Console :: output ( '<light_green>Success!!</light_green> The configuration for <bold>Magallanes</bold> has been generated at <blue>.mage</blue> directory.' );
Console :: output ( '<bold>Please!! Review and adjust the configuration.</bold>' , 2 , 2 );
2014-08-06 21:43:29 -03:00
$exitCode = 0 ;
2011-11-23 23:45:04 -02:00
} else {
2013-11-06 12:44:05 -02:00
Console :: output ( '<light_red>Error!!</light_red> Unable to generate the configuration.' , 1 , 2 );
2011-11-23 23:45:04 -02:00
}
2014-08-06 21:43:29 -03:00
return $exitCode ;
2011-11-23 23:45:04 -02:00
}
}
2013-07-06 20:45:54 -03:00
2013-11-06 12:44:05 -02:00
/**
* Returns the Global Configuration
* @return string
*/
2013-07-06 20:45:54 -03:00
protected function getGeneralConfig ()
{
2014-08-06 14:01:39 -03:00
// Assamble Global Settings
$projectName = $this -> getConfig () -> getParameter ( 'name' , '' );
$notificationEmail = $this -> getConfig () -> getParameter ( 'email' , '' );
$notificationEnabled = ( $notificationEmail != '' ) ? 'true' : 'false' ;
2013-07-06 20:45:54 -03:00
2014-08-06 14:01:39 -03:00
$globalSettings = str_replace (
array (
'%projectName%' ,
'%notificationEmail%' ,
'%notificationEnabled%' ,
'%loggingEnabled%' ,
'%maxlogs%' ,
'%ssh_needs_tty%' ,
),
array (
$projectName ,
$notificationEmail ,
$notificationEnabled ,
'true' ,
30 ,
'false'
),
$this -> getGeneralConfigTemplate ()
);
2013-07-06 20:45:54 -03:00
2014-08-06 14:01:39 -03:00
return $globalSettings ;
2013-07-06 20:45:54 -03:00
}
2013-11-06 12:44:05 -02:00
/**
* Returns the YAML Template for the Global Configuration
* @return string
*/
protected function getGeneralConfigTemplate ()
{
2014-08-06 14:01:39 -03:00
$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
. 'ssh_needs_tty: %ssh_needs_tty%' . PHP_EOL ;
2013-11-06 12:44:05 -02:00
2014-08-06 14:01:39 -03:00
return $template ;
2013-11-06 12:44:05 -02:00
}
2014-01-28 21:43:43 -05:00
}