NewCommand.php 5.21 KB
Newer Older
Erik Telford's avatar
Erik Telford committed
1 2 3
<?php

namespace Laravel\Installer\Console;
4 5

use ZipArchive;
6
use RuntimeException;
Graham Campbell's avatar
Graham Campbell committed
7
use GuzzleHttp\Client;
8
use Symfony\Component\Process\Process;
Graham Campbell's avatar
Graham Campbell committed
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputOption;
11 12 13 14
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Graham Campbell's avatar
Graham Campbell committed
15
class NewCommand extends Command
Taylor Otwell's avatar
Taylor Otwell committed
16 17 18 19 20 21 22 23
{
    /**
     * Configure the command options.
     *
     * @return void
     */
    protected function configure()
    {
Graham Campbell's avatar
Graham Campbell committed
24 25 26
        $this
            ->setName('new')
            ->setDescription('Create a new Laravel application.')
27
            ->addArgument('name', InputArgument::OPTIONAL)
28 29
            ->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release')
            ->addOption('force', null, InputOption::VALUE_NONE, 'Forces install even if the directory already exists');
Taylor Otwell's avatar
Taylor Otwell committed
30 31 32 33 34
    }

    /**
     * Execute the command.
     *
35 36
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
Taylor Otwell's avatar
Taylor Otwell committed
37 38 39 40
     * @return void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
41
        if (! class_exists('ZipArchive')) {
Taylor Otwell's avatar
Taylor Otwell committed
42
            throw new RuntimeException('The Zip PHP extension is not installed. Please install it and try again.');
43 44
        }

45 46 47 48 49
        $directory = ($input->getArgument('name')) ? getcwd().'/'.$input->getArgument('name') : getcwd();

        if (! $input->getOption('force')) {
            $this->verifyApplicationDoesntExist($directory);
        }
Taylor Otwell's avatar
Taylor Otwell committed
50 51 52

        $output->writeln('<info>Crafting application...</info>');

53 54 55
        $version = $this->getVersion($input);

        $this->download($zipFile = $this->makeFilename(), $version)
56 57 58
             ->extract($zipFile, $directory)
             ->cleanUp($zipFile);

Taylor Otwell's avatar
Taylor Otwell committed
59 60
        $composer = $this->findComposer();

Graham Campbell's avatar
Graham Campbell committed
61
        $commands = [
Taylor Otwell's avatar
Taylor Otwell committed
62
            $composer.' install --no-scripts',
63
            $composer.' run-script post-root-package-install',
64
            $composer.' run-script post-install-cmd',
Taylor Otwell's avatar
Taylor Otwell committed
65
            $composer.' run-script post-create-project-cmd',
Graham Campbell's avatar
Graham Campbell committed
66
        ];
Tomás Soler's avatar
Tomás Soler committed
67

Tomás Soler's avatar
Tomás Soler committed
68 69
        if ($input->getOption('no-ansi')) {
            $commands = array_map(function ($value) {
Tomás Soler's avatar
Tomás Soler committed
70
                return $value.' --no-ansi';
Tomás Soler's avatar
Tomás Soler committed
71 72
            }, $commands);
        }
Tomás Soler's avatar
Tomás Soler committed
73

74
        $process = new Process(implode(' && ', $commands), $directory, null, null, null);
Alex Bowers's avatar
Alex Bowers committed
75

76 77 78
        if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
            $process->setTty(true);
        }
Taylor Otwell's avatar
Taylor Otwell committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92

        $process->run(function ($type, $line) use ($output) {
            $output->write($line);
        });

        $output->writeln('<comment>Application ready! Build something amazing.</comment>');
    }

    /**
     * Verify that the application does not already exist.
     *
     * @param  string  $directory
     * @return void
     */
93
    protected function verifyApplicationDoesntExist($directory)
Taylor Otwell's avatar
Taylor Otwell committed
94
    {
95
        if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) {
Graham Campbell's avatar
Graham Campbell committed
96
            throw new RuntimeException('Application already exists!');
Taylor Otwell's avatar
Taylor Otwell committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        }
    }

    /**
     * Generate a random temporary filename.
     *
     * @return string
     */
    protected function makeFilename()
    {
        return getcwd().'/laravel_'.md5(time().uniqid()).'.zip';
    }

    /**
     * Download the temporary Zip to the given file.
     *
     * @param  string  $zipFile
114
     * @param  string  $version
Taylor Otwell's avatar
Taylor Otwell committed
115 116
     * @return $this
     */
117
    protected function download($zipFile, $version = 'master')
Taylor Otwell's avatar
Taylor Otwell committed
118
    {
119
        switch ($version) {
Taylor Otwell's avatar
Taylor Otwell committed
120 121 122
            case 'develop':
                $filename = 'latest-develop.zip';
                break;
123 124 125 126 127 128
            case 'master':
                $filename = 'latest.zip';
                break;
        }

        $response = (new Client)->get('http://cabinet.laravel.com/'.$filename);
Taylor Otwell's avatar
Taylor Otwell committed
129

Graham Campbell's avatar
Graham Campbell committed
130
        file_put_contents($zipFile, $response->getBody());
Taylor Otwell's avatar
Taylor Otwell committed
131 132 133 134 135

        return $this;
    }

    /**
136
     * Extract the Zip file into the given directory.
Taylor Otwell's avatar
Taylor Otwell committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
     *
     * @param  string  $zipFile
     * @param  string  $directory
     * @return $this
     */
    protected function extract($zipFile, $directory)
    {
        $archive = new ZipArchive;

        $archive->open($zipFile);

        $archive->extractTo($directory);

        $archive->close();

        return $this;
    }

    /**
     * Clean-up the Zip file.
     *
     * @param  string  $zipFile
     * @return $this
     */
    protected function cleanUp($zipFile)
    {
        @chmod($zipFile, 0777);

        @unlink($zipFile);

        return $this;
    }

170 171 172
    /**
     * Get the version that should be downloaded.
     *
173
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
174 175
     * @return string
     */
jerguslejko's avatar
jerguslejko committed
176
    protected function getVersion(InputInterface $input)
177 178 179 180 181 182 183 184
    {
        if ($input->getOption('dev')) {
            return 'develop';
        }

        return 'master';
    }

Taylor Otwell's avatar
Taylor Otwell committed
185 186 187 188 189 190 191 192 193 194 195 196 197
    /**
     * Get the composer command for the environment.
     *
     * @return string
     */
    protected function findComposer()
    {
        if (file_exists(getcwd().'/composer.phar')) {
            return '"'.PHP_BINARY.'" composer.phar';
        }

        return 'composer';
    }
198
}