-
Notifications
You must be signed in to change notification settings - Fork 554
/
Install.php
151 lines (131 loc) · 4.36 KB
/
Install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace A17\Twill\Commands;
use A17\Twill\Commands\Traits\HandlesPresets;
use Illuminate\Database\DatabaseManager;
use Illuminate\Filesystem\Filesystem;
class Install extends Command
{
use HandlesPresets;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'twill:install {preset? : Optional, the preset to install} {--fromBuild}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Install Twill into your Laravel application';
public function __construct(public Filesystem $files, public DatabaseManager $db)
{
parent::__construct();
}
/**
* Executes the console command.
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function handle(): void
{
//check the database connection before installing
try {
$this->db->connection()->getPdo();
} catch (\Exception $exception) {
$this->error('Could not connect to the database, please check your configuration:' . "\n" . $exception);
return;
}
if (filled($preset = $this->argument('preset'))) {
if ($this->presetExists($preset)) {
if (
$this->confirm(
'Are you sure to install this preset? This can overwrite your models, config and routes.'
)
) {
$this->installPreset($preset);
} else {
$this->warn('Cancelled.');
}
} else {
$this->error("Could not find preset: $preset");
}
} else {
$this->copyBlockPreviewFile();
$this->addRoutesFile();
$this->call('migrate');
$this->publishConfig();
$this->publishAssets();
$this->createSuperAdmin();
$this->info('All good!');
}
}
private function installPreset(string $preset): void
{
$this->installPresetFiles($preset);
$this->call('migrate');
$this->publishAssets();
$this->createSuperAdmin();
$this->info('Finished installing preset!');
}
/**
* Creates the default `twill.php` route configuration file.
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
private function addRoutesFile(): void
{
$routesPath = base_path('routes');
if (!$this->files->exists($routesPath)) {
$this->files->makeDirectory($routesPath, 0755, true);
}
if (!$this->files->exists($routesPath . '/twill.php')) {
$stub = $this->files->get(__DIR__ . '/stubs/admin.stub');
$this->files->put($routesPath . '/twill.php', $stub);
}
}
private function copyBlockPreviewFile(): void
{
$layoutsDirectory = base_path('resources/views/site/layouts');
if (!$this->files->exists($layoutsDirectory)) {
$this->files->makeDirectory($layoutsDirectory, 0755, true);
}
if (!$this->files->exists($layoutsDirectory . '/block.blade.php')) {
$stub = $this->files->get(__DIR__ . '/stubs/block.blade.php');
$this->files->put($layoutsDirectory . '/block.blade.php', $stub);
}
}
/**
* Calls the command responsible for creation of the default superadmin user.
*/
private function createSuperAdmin(): void
{
if (!$this->option('no-interaction')) {
$this->call('twill:superadmin');
}
}
/**
* Publishes the package configuration files.
*/
private function publishConfig(): void
{
$this->call('vendor:publish', [
'--provider' => \A17\Twill\TwillServiceProvider::class,
'--tag' => 'config',
]);
}
/**
* Publishes the package frontend assets.
*/
private function publishAssets(): void
{
if ($this->option('fromBuild')) {
// If this is from a build, we copy from dist to public.
$this->files->copyDirectory(__DIR__ . '/../../dist/', public_path());
} else {
$this->call('vendor:publish', [
'--provider' => \A17\Twill\TwillServiceProvider::class,
'--tag' => 'assets',
]);
}
}
}