Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
63 / 63 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
HydeInstallCommand | |
100.00% |
63 / 63 |
|
100.00% |
9 / 9 |
15 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
handle | |
100.00% |
34 / 34 |
|
100.00% |
1 / 1 |
3 | |||
promptForSiteName | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
promptForSiteUrl | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
promptForHomepage | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
updateSiteName | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
updateSiteUrl | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
markInstalled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isInstalled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Hyde\Framework\Commands; |
4 | |
5 | use Hyde\Framework\Concerns\Commands\AsksToRebuildSite; |
6 | use Hyde\Framework\Hyde; |
7 | use Illuminate\Support\Facades\Cache; |
8 | use LaravelZero\Framework\Commands\Command; |
9 | |
10 | /** |
11 | * Initialize a new Hyde project. |
12 | * |
13 | * @see \Tests\Feature\Commands\HydeInstallCommandTest |
14 | */ |
15 | class HydeInstallCommand extends Command |
16 | { |
17 | use AsksToRebuildSite; |
18 | |
19 | protected $signature = 'install {--mark-installed : Hide the command without running the installer}'; |
20 | protected $description = 'Initialize a new Hyde project.'; |
21 | |
22 | public ?string $siteName = null; |
23 | public ?string $siteUrl = null; |
24 | |
25 | public function __construct() |
26 | { |
27 | parent::__construct(); |
28 | |
29 | if ($this->isInstalled()) { |
30 | $this->setHidden(); |
31 | } |
32 | } |
33 | |
34 | public function handle(): int |
35 | { |
36 | if ($this->option('mark-installed')) { |
37 | $this->info('Marking Hyde as installed and hiding the command!'); |
38 | $this->markInstalled(); |
39 | |
40 | return 0; |
41 | } |
42 | |
43 | $this->title('Welcome to HydePHP!'); |
44 | |
45 | $this->info('This guided installer is optional, but can help you to get set up quickly.'); |
46 | |
47 | $this->warn('Please note that this installer should not be run in existing projects.'); |
48 | |
49 | if (! $this->confirm('Do you want to continue?', true)) { |
50 | $this->comment('Aborting installation.'); |
51 | |
52 | return 130; |
53 | } |
54 | |
55 | $this->info('Installing HydePHP...'); |
56 | $this->newLine(); |
57 | |
58 | $this->call('update:configs'); |
59 | |
60 | $this->promptForSiteName(); |
61 | |
62 | $this->promptForSiteUrl(); |
63 | |
64 | $this->promptForHomepage(); |
65 | |
66 | $this->askToRebuildSite(); |
67 | |
68 | $this->markInstalled(); |
69 | |
70 | $this->newLine(); |
71 | |
72 | $this->line('<bg=blue;fg=white> </>'); |
73 | $this->line('<bg=blue;fg=white> HydePHP has been installed successfully! </>'); |
74 | $this->line('<bg=blue;fg=white> Go build something great! </>'); |
75 | $this->line('<bg=blue;fg=white> </>'); |
76 | |
77 | $this->newLine(); |
78 | |
79 | $this->info('What\'s next?'); |
80 | $this->newLine(); |
81 | $this->line('<fg=gray> > Run `hyde build` to build your site.</>'); |
82 | $this->line('<fg=gray> > Run `hyde serve` to start a development server that rebuilds your site on the fly.</>'); |
83 | $this->line('<fg=gray> > Run `hyde help` to get help for one of the commands.</>'); |
84 | $this->line('<fg=gray> > You can run `npm install` and `npm run dev` to compile any TailwindCSS assets.</>'); |
85 | $this->newLine(); |
86 | $this->line('<fg=gray> > You may also want to check out the <href=https://hydephp.com/docs>HydePHP Docs</>'); |
87 | $this->newLine(); |
88 | |
89 | return 0; |
90 | } |
91 | |
92 | protected function promptForSiteName() |
93 | { |
94 | if ($this->siteName = $this->ask('What is the name of your site? <fg=gray>(leave blank to skip)</>')) { |
95 | $this->updateSiteName(); |
96 | $this->info('Site name set to: <comment>'.$this->siteName.'</>'); |
97 | |
98 | return; |
99 | } |
100 | |
101 | $this->line('Skipping site name.'); |
102 | } |
103 | |
104 | protected function promptForSiteUrl() |
105 | { |
106 | if ($this->siteUrl = $this->ask('What is the URL of your site? <fg=gray>(leave blank to skip)</>')) { |
107 | $this->updateSiteUrl(); |
108 | $this->info('Site URL set to: <comment>'.$this->siteUrl.'</>'); |
109 | |
110 | return; |
111 | } |
112 | |
113 | $this->line('Skipping site URL.'); |
114 | } |
115 | |
116 | protected function promptForHomepage() |
117 | { |
118 | $this->newLine(); |
119 | |
120 | $this->info('Hyde has a few different homepage options.'); |
121 | if ($this->confirm('Would you like to select an index.blade.php file?')) { |
122 | $this->call('publish:homepage'); |
123 | } else { |
124 | $this->line('Okay, leaving the default homepage.'); |
125 | } |
126 | } |
127 | |
128 | protected function updateSiteName(): void |
129 | { |
130 | $config = file_get_contents(Hyde::path('config/hyde.php')); |
131 | $config = str_replace( |
132 | "'name' => ".'$siteName'." = env('SITE_NAME', 'HydePHP'),", |
133 | "'name' => ".'$siteName'." = env('SITE_NAME', '".$this->siteName."'),", |
134 | $config |
135 | ); |
136 | file_put_contents(Hyde::path('config/hyde.php'), $config); |
137 | } |
138 | |
139 | protected function updateSiteUrl(): void |
140 | { |
141 | $config = file_get_contents(Hyde::path('config/hyde.php')); |
142 | $config = str_replace( |
143 | "'site_url' => env('SITE_URL', null),", |
144 | "'site_url' => env('SITE_URL', '".$this->siteUrl."'),", |
145 | $config |
146 | ); |
147 | file_put_contents(Hyde::path('config/hyde.php'), $config); |
148 | } |
149 | |
150 | protected function markInstalled(): void |
151 | { |
152 | Cache::forever('hyde.installed', true); |
153 | } |
154 | |
155 | protected function isInstalled(): bool |
156 | { |
157 | return Cache::get('hyde.installed', false) === true; |
158 | } |
159 | } |