NewCommand.php 5.26 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)
Taylor Otwell's avatar
Taylor Otwell committed
28 29
            ->addOption('dev', null, InputOption::VALUE_NONE, 'Installs the latest "development" release')
            ->addOption('5.2', null, InputOption::VALUE_NONE, 'Installs the "5.2" release');
Taylor Otwell's avatar
Taylor Otwell committed
30 31 32 33 34 35 36 37 38 39 40
    }

    /**
     * Execute the command.
     *
     * @param  InputInterface  $input
     * @param  OutputInterface  $output
     * @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
        }

Taylor Otwell's avatar
Taylor Otwell committed
45
        $this->verifyApplicationDoesntExist(
46
            $directory = ($input->getArgument('name')) ? getcwd().'/'.$input->getArgument('name') : getcwd(),
Taylor Otwell's avatar
Taylor Otwell committed
47 48 49 50 51
            $output
        );

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

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

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

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

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

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

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

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

        $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
     */
    protected function verifyApplicationDoesntExist($directory, OutputInterface $output)
    {
94
        if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) {
Graham Campbell's avatar
Graham Campbell committed
95
            throw new RuntimeException('Application already exists!');
Taylor Otwell's avatar
Taylor Otwell committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        }
    }

    /**
     * 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
113
     * @param  string  $version
Taylor Otwell's avatar
Taylor Otwell committed
114 115
     * @return $this
     */
116
    protected function download($zipFile, $version = 'master')
Taylor Otwell's avatar
Taylor Otwell committed
117
    {
118
        switch ($version) {
Taylor Otwell's avatar
Taylor Otwell committed
119 120 121
            case 'develop':
                $filename = 'latest-develop.zip';
                break;
122 123 124
            case 'master':
                $filename = 'latest.zip';
                break;
Taylor Otwell's avatar
Taylor Otwell committed
125 126
            case '5.2':
                $filename = 'latest-52.zip';
127 128 129 130
                break;
        }

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

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

        return $this;
    }

    /**
138
     * Extract the Zip file into the given directory.
Taylor Otwell's avatar
Taylor Otwell committed
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 170 171
     *
     * @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;
    }

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

Taylor Otwell's avatar
Taylor Otwell committed
184 185 186 187
        if ($input->getOption('5.2')) {
            return '5.2';
        }

188 189 190
        return 'master';
    }

Taylor Otwell's avatar
Taylor Otwell committed
191 192 193 194 195 196 197 198 199 200 201 202 203
    /**
     * 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';
    }
204
}