NewLaravelApp::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scriptura\QuickStart\Tasks;
6
7
use Scriptura\QuickStart\CommandRunner;
8
use Scriptura\QuickStart\ProjectFilesystem;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class NewLaravelApp
13
{
14
    /**
15
     * @var bool
16
     */
17
    private $force;
18
19
    public function __construct(bool $force)
20
    {
21
        $this->force = $force;
22
    }
23
24
    public function __invoke(string $name, InputInterface $input, OutputInterface $output)
25
    {
26
        $output->writeln('<info># Building Laravel application [' . $name . ']...</info>');
27
28
        $directory = getcwd();
29
30
        $commands = [
31
            'laravel new ' . $name . ' ' . ($this->force ? '--force' : ''),
32
        ];
33
34
        $runner = new CommandRunner($output, $directory);
35
        $isSuccessful = $runner->run($commands);
36
37
        $filesystem = new ProjectFilesystem(getcwd() . '/' . $name);
38
39
        $output->writeln('<info>Update file: .gitignore</info>');
40
        $filesystem->updateFile('.gitignore', function (string $content) {
41
            $patterns = [
42
                '/(\/node_modules)/',
43
            ];
44
            $replace = [
45
                "/.idea\n\${1}",
46
            ];
47
48
            return preg_replace($patterns, $replace, $content);
49
        });
50
51
        $output->writeln('<info>Update file: .env</info>');
52
        $filesystem->updateFile('.env', function (string $content) use ($name) {
53
            $patterns = [
54
                '/APP_URL=http:\/\/localhost/',
55
                '/DB_DATABASE=laravel/',
56
                '/DB_USERNAME=root/',
57
                '/DB_PASSWORD=/',
58
            ];
59
            $replace = [
60
                "APP_URL=http://{$name}.test",
61
                "DB_DATABASE={$name}",
62
                "DB_USERNAME=homestead",
63
                "DB_PASSWORD=secret",
64
            ];
65
66
            return preg_replace($patterns, $replace, $content);
67
        });
68
69
        $output->writeln('<info>Update file: .env.example</info>');
70
        $filesystem->updateFile('.env.example', function (string $content) use ($name) {
71
            $patterns = [
72
                '/APP_URL=http:\/\/localhost/',
73
                '/DB_DATABASE=laravel/',
74
                '/DB_USERNAME=root/',
75
                '/DB_PASSWORD=/',
76
            ];
77
            $replace = [
78
                "APP_URL=http://{$name}.test",
79
                "DB_DATABASE={$name}",
80
                "DB_USERNAME=homestead",
81
                "DB_PASSWORD=secret",
82
            ];
83
84
            return preg_replace($patterns, $replace, $content);
85
        });
86
87
        if ($isSuccessful) {
88
            $output->writeln('<comment>Default Laravel install done.</comment>');
89
        }
90
    }
91
}
92