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

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

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

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

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

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

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

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

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

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

74 75 76
        if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
            $process->setTty(true);
        }
Taylor Otwell's avatar
Taylor Otwell committed
77 78 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
     */
    protected function verifyApplicationDoesntExist($directory, OutputInterface $output)
    {
93
        if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) {
Graham Campbell's avatar
Graham Campbell committed
94
            throw new RuntimeException('Application already exists!');
Taylor Otwell's avatar
Taylor Otwell committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        }
    }

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

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

Graham Campbell's avatar
Graham Campbell committed
128
        file_put_contents($zipFile, $response->getBody());
Taylor Otwell's avatar
Taylor Otwell committed
129 130 131 132 133 134 135 136 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

        return $this;
    }

    /**
     * Extract the zip file into the given directory.
     *
     * @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;
    }

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

        return 'master';
    }

Taylor Otwell's avatar
Taylor Otwell committed
183 184 185 186 187 188 189 190 191 192 193 194 195
    /**
     * 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';
    }
196
}