diff options
Diffstat (limited to 'src/Script/Command')
-rw-r--r-- | src/Script/Command/Site/Create.php | 119 |
1 files changed, 94 insertions, 25 deletions
diff --git a/src/Script/Command/Site/Create.php b/src/Script/Command/Site/Create.php index 2526d2e..c7936fd 100644 --- a/src/Script/Command/Site/Create.php +++ b/src/Script/Command/Site/Create.php @@ -1,11 +1,12 @@ <?php -namespace App\Script\Command; +namespace App\Script\Command\Site; use Symfony\Component\Console\Command\Command as SyCommand; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Helper\ProgressBar; use App\Filesystem\CreateFile; use App\Filesystem\CreateDirectory; @@ -17,6 +18,8 @@ use App\Filesystem\CreateDirectory; */ class Create extends SyCommand { + protected $progressBar; + /** * Configure the command * @@ -31,6 +34,13 @@ class Create extends SyCommand ->setName('site:create') ->setDescription('Create a new site.') ->setHelp('This command allows you to create a new site.'); + + $this->addOption( + 'test', + 't', + InputOption::VALUE_NONE, + 'Run in test mode without any file or directory created?' + ); } /** @@ -55,45 +65,104 @@ class Create extends SyCommand ); // Set-up progress bar - $progressBar = new ProgressBar($output, $taskCount); - $progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%'); - $progressBar->setFormat('custom'); - $progressBar->setMessage('Starting...'); - $progressBar->start(); - $progressBar->setMessage('Running pre-flight checks'); + $this->progressBar = new ProgressBar($output, $taskCount); + $this->progressBar->setFormatDefinition('custom', ' %current%/%max% -- %message%'); + $this->progressBar->setFormat('custom'); + $this->progressBar->setMessage('Starting...'); + $this->progressBar->start(); + $this->progressBar->setMessage('Running pre-flight checks'); // Construct the file commands so we run checks first - $progressBar->advance(); + $this->progressBar->advance(); $tasks = []; try { - $tasks['Creating site directory'] = new \App\Filesystem\CreateDirectory(SITES_ROOT . $name); - $tasks['Creating logs directory'] = new \App\Filesystem\CreateDirectory(SITES_ROOT . $name . '/logs'); - $tasks['Creating access log file'] = new \App\Filesystem\CreateFile(SITES_ROOT . $name . '/logs/' . $name . '.access_log'); - $tasks['Creating error log file'] = new \App\Filesystem\CreateFile(SITES_ROOT . $name . '/logs/' . $name . '.errors_log'); - $tasks['Creating web folder'] = new \App\Filesystem\CreateDirectory(SITES_ROOT . $name . '/web'); - $tasks['Creating directory index.php'] = new \App\Filesystem\CreateFile(SITES_ROOT . $name . '/web/index.php'); - $tasks['Creating nginx config file'] = new \App\Filesystem\CreateFile(CONFIG_ROOT . $name . '.conf'); + $tasks['Creating site directory'] = new \App\Filesystem\CreateDirectory( + SITES_ROOT . $name + ); + + $tasks['Creating nginx config file'] = new \App\Filesystem\CreateFile( + CONFIG_ROOT . $name . '.conf' + ); } catch (\Exception $e) { + $output->writeln(''); $output->writeln('Error with pre-flight checks'); $output->writeln($e->getMessage()); exit; } - foreach ($tasks as $message => $task) { - // Create file in web root for execution - $progressBar->advance(); - $progressBar->setMessage($message); - $task->execute(); - sleep(1); - } + if (!$input->getOption("test")) { + foreach ($tasks as $message => $task) { + // Create file in web root for execution + $this->runTask($task, $message); + } + + try { + $tasks = []; + + $this->runTask( + new \App\Filesystem\CreateDirectory( + SITES_ROOT . $name . '/logs' + ), + 'Creating logs directory' + ); + $this->runTask( + new \App\Filesystem\CreateFile( + SITES_ROOT . $name . '/logs/' . $name . '.access_log' + ), + 'Creating access log file' + ); + + $this->runTask( + new \App\Filesystem\CreateFile( + SITES_ROOT . $name . '/logs/' . $name . '.errors_log' + ), + 'Creating error log file' + ); + + $this->runTask( + new \App\Filesystem\CreateDirectory( + SITES_ROOT . $name . '/web' + ), + 'Creating web folder' + ); + + $this->runTask( + new \App\Filesystem\CreateFile( + SITES_ROOT . $name . '/web/index.php' + ), + 'Creating directory index.php' + ); + + + foreach ($tasks as $task) { + // Create file in web root for execution + $this->progressBar->advance(); + $this->progressBar->setMessage($task[0]); + $task[1]->execute(); + sleep(1); + } + } catch (\Exception $e) { + $output->writeln('Unexpected Error creating files and directories'); + $output->writeln($e->getMessage()); + exit; + } + } // Finish up - $progressBar->advance(); - $progressBar->finish(); + $this->progressBar->advance(); + $this->progressBar->finish(); $output->writeln(''); $output->writeln('Complete'); } + + public function runTask($task, $message) + { + $this->progressBar->advance(); + $this->progressBar->setMessage($message); + $task->execute(); + sleep(1); + } } |