Completed
Push — master ( 4f68a8...3694fa )
by Anton
01:24
created

src/Console/Commands/Setup.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of Laravel Paket.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Paket\Console\Commands;
15
16
use Illuminate\Console\Command;
17
use Illuminate\Console\DetectsApplicationNamespace;
18
use Illuminate\Support\Facades\File;
19
use Symfony\Component\Console\Input\InputOption;
20
21
final class Setup extends Command
22
{
23
    use DetectsApplicationNamespace;
0 ignored issues
show
Deprecated Code introduced by
The trait Illuminate\Console\DetectsApplicationNamespace has been deprecated with message: Usage of this trait is deprecated and it will be removed in Laravel 7.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
24
25
    protected $name = 'paket:setup';
26
27
    protected $description = 'Set up all of the Paket resources';
28
29
    public function handle(): int
30
    {
31
        $this->warn('Paket scaffolding set up starting');
32
        $this->publishAssets();
33
        $this->createPaketStorage(storage_path('paket'));
34
        $this->createPaketJobsStorage(storage_path('paket/jobs'));
35
        $this->info('Paket scaffolding set up completed');
36
37
        return 0;
38
    }
39
40
    protected function getOptions(): array
41
    {
42
        return [
43
            ['force', null, InputOption::VALUE_NONE, 'Overwrite any existing files'],
44
        ];
45
    }
46
47
    private function publishAssets(): void
48
    {
49
        if ($this->option('force')) {
50
            $this->warn('Force publishing Paket assets');
51
            $this->call('vendor:publish', [
52
                '--tag' => 'paket-assets',
53
                '--force' => $this->option('force'),
54
            ]);
55
        } else {
56
            $this->warn('Publishing Paket assets');
57
            $this->call('vendor:publish', [
58
                '--tag' => 'paket-assets',
59
            ]);
60
        }
61
        $this->info('Published Paket assets');
62
    }
63
64
    private function createPaketStorage(string $path): void
65
    {
66
        $this->createDirectory($path);
67
        $this->createGitIgnore($path, "*\n!jobs/\n!.gitignore\n");
68
    }
69
70
    private function createPaketJobsStorage(string $path): void
71
    {
72
        $this->createDirectory($path);
73
        $this->createGitIgnore($path, "*\n!.gitignore\n");
74
    }
75
76
    private function createDirectory(string $path): void
77
    {
78
        if (file_exists($path)) {
79
            return;
80
        }
81
82
        $this->warn("Creating `{$path}` directory");
83
        mkdir($path);
84
        $this->info("Created `{$path}` directory");
85
    }
86
87
    private function createGitIgnore(string $path, string $content): void
88
    {
89
        $filepath = sprintf('%s/.gitignore', $path);
90
        if (file_exists($filepath)) {
91
            return;
92
        }
93
94
        $this->warn("Creating `{$filepath}` file");
95
        File::put($filepath, $content);
96
        $this->info("Created `{$filepath}` file");
97
    }
98
}
99