NewCommand.php 6.28 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;
9
use Symfony\Component\Filesystem\Filesystem;
Graham Campbell's avatar
Graham Campbell committed
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputOption;
12 13 14
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
15 16
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

Graham Campbell's avatar
Graham Campbell committed
17
class NewCommand extends Command
Taylor Otwell's avatar
Taylor Otwell committed
18 19 20 21 22 23 24 25
{
    /**
     * Configure the command options.
     *
     * @return void
     */
    protected function configure()
    {
Graham Campbell's avatar
Graham Campbell committed
26 27 28
        $this
            ->setName('new')
            ->setDescription('Create a new Laravel application.')
29
            ->addArgument('name', InputArgument::OPTIONAL)
30 31
            ->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
32 33 34 35 36
    }

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

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

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

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

55 56 57
        $version = $this->getVersion($input);

        $this->download($zipFile = $this->makeFilename(), $version)
58
             ->extract($zipFile, $directory)
59
             ->prepareWritableDirectories($directory, $output)
60 61
             ->cleanUp($zipFile);

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

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

71 72 73 74 75 76
        if ($input->getOption('dev')) {
            unset($commands[2]);

            $commands[] = $composer.' run-script post-autoload-dump';
        }

Tomás Soler's avatar
Tomás Soler committed
77 78
        if ($input->getOption('no-ansi')) {
            $commands = array_map(function ($value) {
Tomás Soler's avatar
Tomás Soler committed
79
                return $value.' --no-ansi';
Tomás Soler's avatar
Tomás Soler committed
80 81
            }, $commands);
        }
Tomás Soler's avatar
Tomás Soler committed
82

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

85 86 87
        if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
            $process->setTty(true);
        }
Taylor Otwell's avatar
Taylor Otwell committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101

        $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
     */
102
    protected function verifyApplicationDoesntExist($directory)
Taylor Otwell's avatar
Taylor Otwell committed
103
    {
104
        if ((is_dir($directory) || is_file($directory)) && $directory != getcwd()) {
Graham Campbell's avatar
Graham Campbell committed
105
            throw new RuntimeException('Application already exists!');
Taylor Otwell's avatar
Taylor Otwell committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        }
    }

    /**
     * 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
123
     * @param  string  $version
Taylor Otwell's avatar
Taylor Otwell committed
124 125
     * @return $this
     */
126
    protected function download($zipFile, $version = 'master')
Taylor Otwell's avatar
Taylor Otwell committed
127
    {
128
        switch ($version) {
Taylor Otwell's avatar
Taylor Otwell committed
129 130 131
            case 'develop':
                $filename = 'latest-develop.zip';
                break;
132 133 134 135 136 137
            case 'master':
                $filename = 'latest.zip';
                break;
        }

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

Graham Campbell's avatar
Graham Campbell committed
139
        file_put_contents($zipFile, $response->getBody());
Taylor Otwell's avatar
Taylor Otwell committed
140 141 142 143 144

        return $this;
    }

    /**
145
     * Extract the Zip file into the given directory.
Taylor Otwell's avatar
Taylor Otwell committed
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 172 173 174 175 176 177 178
     *
     * @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;
    }

179
    /**
Peter Ojo's avatar
Peter Ojo committed
180 181
     * Make sure the storage and bootstrap cache directories are writable.
     * 
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
     * @param  string  $appDirectory
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
     * @return $this
     */
    protected function prepareWritableDirectories($appDirectory, OutputInterface $output)
    {
        $fs = new Filesystem();

        try {
            $fs->chmod($appDirectory . DIRECTORY_SEPARATOR . "bootstrap/cache", 0777, 0000, true);
            $fs->chmod($appDirectory . DIRECTORY_SEPARATOR . "storage", 0777, 0000, true);
        } catch (IOExceptionInterface $e) {
            $output->writeln('<question>Verify that the storage and bootstrap/cache directories are writable.</question>');
        }

        return $this;
    }

200 201 202
    /**
     * Get the version that should be downloaded.
     *
203
     * @param  \Symfony\Component\Console\Input\InputInterface  $input
204 205
     * @return string
     */
jerguslejko's avatar
jerguslejko committed
206
    protected function getVersion(InputInterface $input)
207 208 209 210 211 212 213 214
    {
        if ($input->getOption('dev')) {
            return 'develop';
        }

        return 'master';
    }

Taylor Otwell's avatar
Taylor Otwell committed
215 216 217 218 219 220 221 222 223 224 225 226 227
    /**
     * 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';
    }
228
}