From 60e76e39af0297408bdf252edce69c56f3eb9c3e Mon Sep 17 00:00:00 2001 From: Jesus Manuel Olivas Date: Tue, 21 Jul 2015 19:55:58 -0700 Subject: [PATCH] #6 Add dir option to init command --- src/Command/InitCommand.php | 59 ++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/Command/InitCommand.php b/src/Command/InitCommand.php index b970b13..c39550c 100644 --- a/src/Command/InitCommand.php +++ b/src/Command/InitCommand.php @@ -41,11 +41,17 @@ protected function configure() $this ->setName('init') ->setDescription('Copy configuration files to user home directory') + ->addOption( + 'dir', + null, + InputOption::VALUE_REQUIRED, + 'Directory to copy file(s) valid options home, current' + ) ->addOption( 'override', null, InputOption::VALUE_NONE, - 'Override files on user home directory' + 'Override files on directory' ); } @@ -53,23 +59,67 @@ protected function execute(InputInterface $input, OutputInterface $output) { $application = $this->getApplication(); $config = $application->getConfig(); - $customConfigDirectory = $config->getUserHomeDirectory().'/.phpqa'; - $baseConfigDirectory = $config->getBaseConfigDirectory(); + + $dir = $input->getOption('dir'); + + if (!$dir) { + throw new \Exception( + 'You must provide a valid dir value (home or current)' + ); + } $override = false; if ($input->hasOption('override')) { $override = $input->getOption('override'); } + if ($dir === 'current') { + $this->copyCurrentDirectory($output, $config, $override); + } + + if ($dir === 'home') { + $this->copyHomeDirectory($output, $config, $override); + } + } + + private function copyCurrentDirectory($output, $config, $override) + { + $baseConfigDirectory = $config->getBaseConfigDirectory(); + $currentDirectory = $config->getApplicationDirectory(); + + $source = $baseConfigDirectory.'/../phpqa.yml'; + $destination = $currentDirectory.'phpqa.yml'; + + if ($this->copyFile($source, $destination, $override)) { + $output->writeln('Copied file(s):'); + $output->writeln( + sprintf( + '1 - %s', + $destination + ) + ); + } + } + + private function copyHomeDirectory($output, $config, $override) + { + $baseConfigDirectory = $config->getBaseConfigDirectory(); + $customConfigDirectory = $config->getUserHomeDirectory().'/.phpqa'; + if (!is_dir($customConfigDirectory)) { mkdir($customConfigDirectory); } $index = 1; + $copiedMessage = true; foreach ($this->files as $file) { $source = $baseConfigDirectory.$file['source']; $destination = $customConfigDirectory.'/'.$file['destination']; if ($this->copyFile($source, $destination, $override)) { + if ($copiedMessage) { + $output->writeln('Copied file(s):'); + } + $copiedMessage = false; $output->writeln( sprintf( '%s - %s', @@ -77,11 +127,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $destination ) ); + $index++; } } } - public function copyFile($source, $destination, $override) + private function copyFile($source, $destination, $override) { if (file_exists($destination) && !$override) { return false;