* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ 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 */ class InitCommand extends AbstractCommand { /** * Command for Initalize a new Configuration Proyect * @see \Mage\Command\AbstractCommand::run() */ public function run() { $configDir = '.mage'; Console::output('Initiating managing process for application with Magallanes'); // Check if there is already a config dir if (file_exists($configDir)) { Console::output('Error!! Already exists .mage directory.', 1, 2); } else { $results = array(); $results[] = mkdir($configDir); $results[] = mkdir($configDir . '/logs'); $results[] = file_put_contents($configDir . '/logs/.gitignore', "*\n!.gitignore"); $results[] = mkdir($configDir . '/tasks'); $results[] = touch($configDir . '/tasks/.gitignore'); $results[] = mkdir($configDir . '/config'); $results[] = mkdir($configDir . '/config/environment'); $results[] = touch($configDir . '/config/environment/.gitignore'); $results[] = file_put_contents($configDir . '/config/general.yml', $this->getGeneralConfig()); if (!in_array(false, $results)) { Console::output('Success!! The configuration for Magallanes has been generated at .mage directory.'); Console::output('Please!! Review and adjust the configuration.', 2, 2); } else { Console::output('Error!! Unable to generate the configuration.', 1, 2); } } } /** * Returns the Global Configuration * @return string */ 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 ), $this->getGeneralConfigTemplate() ); return $globalSettings; } /** * 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; } }