|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the lucid-cli project. |
| 5 | + * |
| 6 | + |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Lucid\Console\Commands; |
| 13 | + |
| 14 | +use Symfony\Component\Process\Process; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Input\InputArgument; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Abed Halawi <[email protected]> |
| 23 | + */ |
| 24 | +class NewCommand extends Command |
| 25 | +{ |
| 26 | + /** |
| 27 | + * Configure the command options. |
| 28 | + */ |
| 29 | + protected function configure() |
| 30 | + { |
| 31 | + $this |
| 32 | + ->setName('new') |
| 33 | + ->setDescription('Create a new Lucid-architected project') |
| 34 | + ->addArgument('name', InputArgument::OPTIONAL) |
| 35 | + ->addOption('laravel', null, InputOption::VALUE_NONE, 'Specify the Laravel version you wish to install'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Execute the command. |
| 40 | + * |
| 41 | + * @param InputInterface $input |
| 42 | + * @param OutputInterface $output |
| 43 | + */ |
| 44 | + public function execute(InputInterface $input, OutputInterface $output) |
| 45 | + { |
| 46 | + $this->verifyApplicationDoesntExist( |
| 47 | + $directory = ($input->getArgument('name')) ? getcwd().'/'.$input->getArgument('name') : getcwd(), |
| 48 | + $output |
| 49 | + ); |
| 50 | + |
| 51 | + $output->writeln('<info>Crafting Lucid application...</info>'); |
| 52 | + |
| 53 | + /* |
| 54 | + * @TODO: Get Lucid based on the Laravel version. |
| 55 | + */ |
| 56 | + $process = new Process($this->findComposer().' create-project laravel/laravel '.$directory); |
| 57 | + |
| 58 | + if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) { |
| 59 | + $process->setTty(true); |
| 60 | + } |
| 61 | + |
| 62 | + $process->run(function ($type, $line) use ($output) { |
| 63 | + $output->write($line); |
| 64 | + }); |
| 65 | + |
| 66 | + $output->writeln('<comment>Application ready! Make your dream a reality.</comment>'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Verify that the application does not already exist. |
| 71 | + * |
| 72 | + * @param string $directory |
| 73 | + */ |
| 74 | + protected function verifyApplicationDoesntExist($directory, OutputInterface $output) |
| 75 | + { |
| 76 | + if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) { |
| 77 | + throw new RuntimeException('Application already exists!'); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Get the composer command for the environment. |
| 83 | + * |
| 84 | + * @return string |
| 85 | + */ |
| 86 | + protected function findComposer() |
| 87 | + { |
| 88 | + $composer = 'composer'; |
| 89 | + |
| 90 | + if (file_exists(getcwd().'/composer.phar')) { |
| 91 | + $composer = '"'.PHP_BINARY.'" composer.phar'; |
| 92 | + } |
| 93 | + |
| 94 | + return $composer; |
| 95 | + } |
| 96 | +} |
0 commit comments