*/ class Create extends SyCommand { protected $progressBar; /** * Configure the command * * @author Phil Burton */ protected function configure() { $this->addArgument('name', InputArgument::REQUIRED, 'The name of the new site.'); $this->addArgument('domain', InputArgument::REQUIRED, 'The domain name of the new site.'); $this ->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?' ); } /** * Run the command * * @param InputInterface $input * @param OutputInterface $output * @author Phil Burton */ protected function execute(InputInterface $input, OutputInterface $output) { $taskCount = 6; $name = $input->getArgument('name'); $domain = $input->getArgument('domain'); $output->writeln( 'Creating new site with domain: ' . $input->getArgument('domain') . ' and name: ' . $input->getArgument('name') ); // Set-up progress bar $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 $this->progressBar->advance(); $tasks = []; try { $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; } 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 $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); } }