forked from damsfx/valet-windows
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvalet.php
More file actions
1536 lines (1223 loc) · 48.6 KB
/
Copy pathvalet.php
File metadata and controls
1536 lines (1223 loc) · 48.6 KB
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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Load correct autoloader depending on install location.
*/
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
else {
require_once __DIR__ . '/../../../autoload.php';
}
require_once __DIR__ . '/version.php';
use Illuminate\Container\Container;
use Valet\Application;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Valet\Drivers\ValetDriver;
use function Valet\info;
use function Valet\info_dump;
use function Valet\output;
use function Valet\table;
use function Valet\default_table_headers;
use function Valet\warning;
use function Valet\error;
use function Valet\progressbar;
/**
* Create the application.
*/
Container::setInstance(new Container());
$app = new Application('Laravel Valet Windows', $version);
Upgrader::onEveryRun();
// TODO: Abstract all Xdebug related code into a separate opt-in custom extension, this could be an alternative to just deprecating Xdebug functionality altogether.
/**
* Install Valet's services and configs,
* and auto start Valet.
*
* @param bool $xdebug Optionally, install Xdebug for PHP
*/
$app->command('install [--xdebug]', function ($input, $output, $xdebug) {
$helper = $this->getHelperSet()->get('question');
/**
* Check if the Valet is already installed by checking if the services are running.
*/
$services = Valet::services(true);
$alreadyInstalled = false;
foreach ($services as $key => $value) {
if (str_contains($value["status"], "running")) {
$alreadyInstalled = true;
break;
};
}
if ($alreadyInstalled) {
$alreadyInstalledQuestion = new ConfirmationQuestion("<fg=red>Valet seems to already be installed. Do you want to reinstall? yes/no</fg=red>\n", false);
if (!$helper->ask($input, $output, $alreadyInstalledQuestion)) {
warning("Reinstall aborted.");
return;
}
}
$maxItems = $xdebug ? 8 : 7;
$progressBar = progressbar($maxItems, "Installing");
sleep(1);
$progressBar->setMessage("Gsudo", "placeholder");
$progressBar->advance();
Gsudo::install();
sleep(1);
$progressBar->setMessage("Configuration", "placeholder");
$progressBar->advance();
Configuration::install();
sleep(1);
$progressBar->setMessage("Nginx", "placeholder");
$progressBar->advance();
Nginx::install();
sleep(1);
$progressBar->setMessage("PHP CGI", "placeholder");
$progressBar->advance();
PhpCgi::install();
sleep(1);
if ($xdebug) {
$progressBar->setMessage("PHP CGI Xdebug", "placeholder");
$progressBar->advance();
PhpCgiXdebug::install();
sleep(1);
}
$progressBar->setMessage("Acrylic", "placeholder");
$progressBar->advance();
Acrylic::install(Configuration::read()['tld']);
sleep(1);
$progressBar->setMessage("Ansicon", "placeholder");
$progressBar->advance();
Ansicon::install();
sleep(1);
$progressBar->setMessage("Emergency Uninstall Files", "placeholder");
$progressBar->advance();
Valet::createEmergencyUninstallFiles();
sleep(1);
$progressBar->finish();
output(PHP_EOL . '<info>Valet installed and started successfully!</info>');
})->descriptions("Install Valet's services and configs, and auto start Valet.", [
"--xdebug" => "Optionally, install Xdebug for PHP"
])->addUsage("install --xdebug");
/**
* A sudo-like command to use Valet commands with elevated privileges
* that only require 1 User Account Control popup.
*
* @param array|null $valetCommand The Valet command plus it's argument's values. A string array separated by spaces.
* @param string|null $valetOptions The Valet options without the leading `--`. Multiple options must be separated by double slashes `//`.
* Example: `--valetOptions=isolate//secure` will be ran as `--isolate --secure`.
*
* @example `valet sudo link example --valetOptions=isolate//secure`
*/
$app->command('sudo valetCommand* [-o|--valetOptions=]', function ($valetCommand = null, $valetOptions = null) {
if (!$valetCommand) {
return;
}
$valetCommand = implode(" ", $valetCommand);
if (str_contains($valetCommand, 'sudo')) {
return error("You can not sudo the sudo command.");
}
// Install Gsudo if it's not already installed.
Gsudo::install();
$valetCommand = str_starts_with($valetCommand, 'valet') ? $valetCommand : "valet $valetCommand";
if ($valetOptions != null) {
$valetOptions = Valet\prefixOptions(explode("//", $valetOptions));
}
$valetCommand = implode(" ", [$valetCommand, $valetOptions]);
CommandLine::sudo($valetCommand);
})->descriptions("A sudo-like command to use Valet commands with elevated privileges that only require 1 User Account Control popup.", [
"valetCommand" => "The Valet command plus it's argument's values, separated by spaces.",
"--valetOptions" => "The Valet options without the leading <fg=green>--</>. Multiple options must be separated by double slashes <fg=green>//</>."
])->addUsage("sudo link mySite --valetOptions=isolate=7.4//secure")->addUsage("sudo link mySite -o isolate=7.4//secure");
/**
* Output diagnostics to aid in debugging Valet.
*
* @param bool $print Optionally print diagnostics output while running
* @param bool $plain Optionally format clipboard output as plain text
*/
$app->command('diagnose [-p|--print] [--plain]', function ($print, $plain) {
info('Running diagnostics... (this may take a while)');
Diagnose::run($print, $plain);
info("The diagnostics have been copied to your clipboard.");
})->descriptions('Output diagnostics to aid in debugging Valet.', [
'--print' => 'Print diagnostics output while running',
'--plain' => 'Print and format output as plain text (aka, pretty print)'
]);
/**
* Add PHP by specifying a path.
*
* @param string $path The path to the PHP
* @param bool $xdebug Optionally, install Xdebug
*/
$app->command('php:add path [--xdebug]', function ($path, $xdebug) {
info("Adding {$path}...");
if ($php = Configuration::addPhp($path)) {
\PhpCgi::install($php['version']);
if ($xdebug) {
info("Installing Xdebug for {$php['version']}...");
\PhpCgiXdebug::install($php['version']);
}
info("PHP {$php['version']} from {$path} has been added. You can make it default by running <bg=magenta> valet use </> command");
}
})->descriptions('Add PHP by specifying a path', [
"path" => "The path to the PHP",
"--xdebug" => "Optionally, install Xdebug"
])->addUsage("php:add c:/php/8.1")->addUsage("php:add c:/php/8.1 --xdebug");
/**
* Remove PHP by specifying it's version.
*
* @param string $phpVersion The PHP version
* @param string $path Optionally, specify the path to the PHP, instead of using the `$phpVersion`.
*/
$app->command('php:remove [phpVersion] [--path=]', function ($phpVersion, $path) {
$txt = $phpVersion ? "version $phpVersion" : "path $path";
info("Removing $txt...");
$config = Configuration::read();
$defaultPhp = $config['default_php'];
$php = $phpVersion ? Configuration::getPhpByVersion($phpVersion) : Configuration::getPhp($path);
if (!$php) {
warning("PHP $txt not found in Valet");
return;
}
if ($php['version'] === $defaultPhp) {
warning("Default PHP {$php['version']} cannot be removed. Change default PHP version by running <bg=magenta> valet use [version] </>");
return;
}
if ($php) {
\PhpCgi::uninstall($php['version']);
if (\PhpCgiXdebug::installed($php['version'])) {
info("Uninstalling {$php['version']} Xdebug...");
\PhpCgiXdebug::uninstall($php['version']);
}
}
if (Configuration::removePhp($php['path'])) {
info("PHP {$php['version']} from {$php['path']} has been removed.");
}
})->descriptions('Remove PHP by specifying the version', [
"phpVersion" => "The PHP version",
"--path" => "Optionally, specify the path to the PHP, instead of using the <fg=green>phpVersion</>"
])->addUsage("php:remove 8.1")->addUsage("php:remove 8.1.8")->addUsage("php:remove --path=c:/php/8.1");
/**
* Reinstall all PHP services that are specified in `valet php:list`
*/
$app->command('php:install', function () {
info('Reinstalling PHP services...');
PhpCgi::uninstall();
PhpCgi::install();
})->descriptions('Reinstall all PHP services from <fg=green>valet php:list</>');
/**
* Uninstall all PHP services that are specified in `valet php:list`
*/
$app->command('php:uninstall', function () {
info('Uninstalling PHP services...');
PhpCgi::uninstall();
info('PHP services uninstalled. Run php:install to install again');
})->descriptions('Uninstall all PHP services from <fg=green>valet php:list</>');
/**
* List all PHP versions and services
*/
$app->command('php:list', function () {
info('Listing PHP services...');
$config = Configuration::read();
$defaultPhpVersion = $config['default_php'];
$php = $config['php'] ?? [];
$php = collect($php)->map(function ($item) use ($defaultPhpVersion) {
$item['default'] = $defaultPhpVersion === $item['version'] ? 'X' : '';
return $item;
})->toArray();
table(['Version', 'Version Alias', 'Path', 'Port', 'Xdebug Port', 'Default'], $php, true);
})->descriptions('List all PHP versions and services');
/**
* Determine which PHP version the current working directory is using.
*
* @param null|string $site Optionally, specify a site
*/
$app->command('php:which [site]', function ($site = null) {
$txt = !$site ? "The current working directory" : "The specified site";
if (!$site) {
$site = basename(getcwd());
}
$which = Site::whichPhp($site);
if ($which === null) {
warning("The site doesn't exist.");
return false;
}
info("{$txt} {$which['site']} is using PHP {$which['phpVersion']}");
info("The executable is located at: " . PhpCgi::getPhpPath($which['phpVersion']));
})->descriptions('Determine which PHP version the current working directory is using', [
"site" => "Optionally, specify a site"
])->addUsage("php:which site2");
/**
* Proxy PHP commands through to a site's PHP executable.
*
* This command block doesn't handle the command logic, the `valet` script in the project root does.
* (The `valet` script is the actual command script that is called when `valet` is
* ran in the terminal.)
*
* This command block is only to document the command within the CLI.
*/
$app->command('php:proxy phpCommand* [--site=]', function ($phpCommand, $site = null) {
warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.');
})->setAliases(["php"])->descriptions("Proxy PHP commands through to a site's PHP executable", [
'phpCommand' => "PHP command to run with the site's PHP executable",
'--site' => 'Specify the site to use to get the PHP version.'
])->addUsage("php:proxy -v --site=site2")->addUsage("php -v --site=site2");
/**
* Install Xdebug services for all PHP versions that are specified in `valet php:list`.
*
* @param string $phpVersion Optionally, install a specific PHP version of Xdebug.
*/
$app->command('xdebug:install [phpVersion]', function ($input, $output, $phpVersion = null) {
$txt = $phpVersion === null ? "" : " for PHP $phpVersion";
if (PhpCgiXdebug::installed($phpVersion)) {
info("Xdebug{$txt} is already installed.");
$helper = $this->getHelperSet()->get('question');
$question = new ConfirmationQuestion("<fg=yellow>Do you want to reinstall it? yes/no</>\n", false);
if (!$helper->ask($input, $output, $question)) {
return warning('Install aborted.');
}
}
info('Installing Xdebug services...');
$phps = PhpCgiXdebug::install($phpVersion);
$phpVersion = $phpVersion ?: implode(", ", $phps);
info("Installed Xdebug for PHP $phpVersion");
})->descriptions('Install all PHP Xdebug services from <fg=green>valet php:list</>', [
"phpVersion" => "Optionally, install a specific PHP version of Xdebug"
])->addUsage("xdebug:install 7.4");
/**
* Uninstall all Xdebug services.
*
* @param string $phpVersion Optionally, uninstall a specific PHP version of Xdebug.
*/
$app->command('xdebug:uninstall [phpVersion]', function ($phpVersion = null) {
if (!PhpCgiXdebug::installed($phpVersion)) {
warning("Xdebug for PHP $phpVersion is not installed.");
return;
}
PhpCgiXdebug::uninstall($phpVersion);
info('Xdebug services uninstalled. Run <bg=gray>xdebug:install [phpVersion]</> to install again');
})->descriptions('Uninstall all PHP Xdebug services from <fg=green>valet php:list</>', [
"phpVersion" => "Optionally, uninstall a specific PHP version of Xdebug"
]);
/**
* Get a calculation of the percentage of parity completion.
*/
$app->command('parity', function () {
// Only get the released version instead of master, to make sure no commands
// are added or removed to the macOS version to ensure this version of Valet 3.0
// always has parity against the version in the URL. (ie. master can change.)
//
// Also, don't use a patch version, use the MAJOR.MINOR.0 format. eg. 4.3.0, 4.5.0
Valet::parity("https://raw.githubusercontent.com/laravel/valet/v4.8.0/cli/app.php");
})->descriptions("Get a calculation of the percentage of parity completion.");
/**
* List the installed Valet services.
*/
$app->command('services', function () {
info("Checking the Valet services...");
$services = Valet::services();
output("\n");
table(['Service', 'Windows Name', 'Status'], $services, true);
info('Use <bg=magenta> start </> <bg=magenta> stop </> or <bg=magenta> restart </> commands to change the status, eg. <bg=magenta> valet restart nginx </>');
})->descriptions('List the installed Valet services.');
/**
* Display the current visibility of the nginx error pages, or optionally
* update its visibility to enable or disable them.
*
* Valet's nginx error pages are shown when a site has internal server or PHP errors.
* They are enabled by default, but can be disabled to allow any 3rd party error
* reporting tools to show instead.
*
* @param string $visibility Optionally, set to `on` to enable Valet's nginx error pages, or `off` to disable them. Default is `on`.
*/
$app->command('nginx-error-page [visibility]', function ($visibility = null) {
$key = 'nginx_error_page';
$current = Configuration::get($key);
// If no visibility argument was passed, output the current status of the nginx error pages.
if ($visibility == null) {
return info("Valet's nginx error pages are $current.");
}
// If the visibility argument is valid...
if (in_array($visibility, ['on', 'off'])) {
// If the visibility is the same as the current value, output a message and return early.
// This avoids unnecessary nginx config rewrites when the user tries to set the visibility
// to its current value.
if ($visibility === $current) {
return info("Valet's nginx error pages are already $visibility.");
}
// Update the configuration value for the nginx error page visibility.
Configuration::updateKey($key, $visibility);
// Re-install nginx server config and restart nginx to apply the change.
Nginx::installServer();
Nginx::restart();
return info("Valet's nginx error pages are now $visibility.");
}
// If the visibility argument is invalid, output an error message.
else {
return warning("Invalid visibility. Please set to either 'on' or 'off'.");
}
})->descriptions("Display the current visibility of Valet's nginx error pages, or optionally update its visibility to enable or disable them.", [
'visibility' => "<fg=green>[on = default]</> will show Valet's nginx error pages \n <fg=green>[off]</> will disable them."
]);
/**
* Most commands are available only if Valet is installed.
*/
if (is_dir(Valet::homePath()) && Nginx::isInstalled()) {
/**
* Upgrade helper: ensure the tld config exists.
*/
if (empty(Configuration::read()['tld'])) {
Configuration::writeBaseConfiguration();
}
/**
* Registers the current working directory to automatically serve sub-directories as sites
* and adds it to the paths configuration.
*
* @param string $path Optionally, specify a path
*/
$app->command('park [path]', function ($path = null) {
Configuration::addPath($path ?: getcwd());
info(($path === null ? 'This' : "The [{$path}]") . " directory has been registered to Valet and all sub-directories will be accessible as sites. To list the sites use <bg=magenta> valet parked </>");
})->descriptions('Registers the current working directory to automatically serve sub-directories as sites', [
"path" => "Optionally, specify a path"
])->addUsage("park D:/sites");
/**
* List all the current sites within parked paths.
*/
$app->command('parked', function () {
$parked = Site::parked();
if (count($parked) === 0) {
return warning("No parked sites found.");
}
table(default_table_headers(), $parked->all());
})->descriptions('List all the current sites within parked paths');
/**
* Remove the current working directory from Valet's list of paths.
*
* @param string $path Optionally, specify a path
*/
$app->command('forget [path]', function ($path = null) {
Configuration::removePath($path ?: getcwd());
info(($path === null ? 'This' : "The [{$path}]") . " directory has been removed from Valet.");
})->setAliases(["unpark"])->descriptions('Remove the current working directory from Valet\'s list of paths', [
"path" => "Optionally, specify a path"
]);
/**
* Register the current working directory as a symbolic link.
*
* @param string $name Optionally specify a new name to be linked as.
* @param bool $secure Optionally secure the site
* @param string $isolate Optionally isolate the site to a specified PHP version
*/
$app->command('link [name] [--secure] [--isolate=]', function ($name, $secure, $isolate = null) {
$linkPath = Site::link(getcwd(), $name = $name ?: basename(getcwd()));
info('A [' . $name . '] symbolic link has been created in [' . $linkPath . '].');
if ($secure) {
$this->runCommand('secure ' . $name);
}
if ($isolate) {
Site::isolate($isolate, $name);
info("The site [$name] is now using $isolate.");
info('Restarting Nginx...');
Nginx::restart();
}
})->descriptions('Register the current working directory as a symbolic link', [
'name' => 'Optionally specify a new name to be linked as',
'--secure' => 'Optionally secure the site',
'--isolate' => 'Optionally isolate the site to a specified PHP version'
])->addUsage("link my_site_renamed")->addUsage("link my_site_renamed --isolate=7.4")->addUsage("link my_site_renamed --secure")->addUsage("link my_site_renamed --secure --isolate=7.4");
/**
* List all of the registered symbolic links.
*/
$app->command('links', function () {
$links = Site::links();
if (count($links) === 0) {
return warning("No linked sites found.");
}
// Recreate the list of links and remove the key of alias and aliasUrl,
// as this is unnecessary for this command.
$links = $links->map(function ($value, $key) {
unset($value['alias']);
unset($value['aliasUrl']);
return $value;
});
table(['Site', 'Secured', 'PHP', 'URL', 'Path'], $links->all(), true);
})->descriptions('List all of the registered symbolic links');
/**
* Unlink the current working directory linked site
*
* @param string $name Optionally specify the linked site name
*/
$app->command('unlink [name]', function ($name) {
if (!$name) {
$name = Site::getLinkNameByCurrentDir();
}
if (Site::isIsolated($name) === true) {
Site::unisolate($name);
info(sprintf('The site [%s] is now using the default PHP version.', $name));
}
if (Site::isSecured($name) === true) {
info('Unsecuring ' . $name . '...');
$site = $name . '.' . Configuration::read()['tld'];
Site::unsecure($site);
}
info('Restarting Nginx...');
Nginx::restart();
// Unlink the site.
Site::unlink($name);
info('The [' . $name . '] symbolic link has been removed.');
})->descriptions('Unlink the current working directory linked site', [
"name" => "Optionally specify the linked site name"
])->addUsage("unlink my_site_renamed");
/**
* Proxy a specified site to a specified host
*
* @param string $site The site to be proxied.
* Multiple sites can be proxied at the same time to 1 host. Separated by commas. eg. `site1,site2,site3`
* @param string $host The host to receive the site traffic
* @param bool $secure Optionally, create a proxy with a trusted TLS certificate
*/
$app->command('proxy site host [--secure]', function ($site, $host, $secure) {
Site::proxyCreate($site, $host, $secure);
Nginx::restart();
})->descriptions('Proxy a specified site to a specified host. Useful for docker, mailhog etc.', [
"site" => "The site to be proxied. Multiple sites can be proxied by separating them with a comma.",
"host" => "The host to receive the site traffic",
"--secure" => "Optionally, secure with a trusted TLS certificate"
])->addUsage("proxy site1 https://127.0.0.1:9200")->addUsage("proxy site1 https://127.0.0.1:9200 --secure")->addUsage("proxy site1,site2,site3 https://127.0.0.1:9200");
/**
* List all the proxy sites.
*/
$app->command('proxies', function () {
$proxies = Site::proxies();
if (count($proxies) === 0) {
return warning("No proxy sites found.");
}
table(['Site', 'Secured', 'URL', 'Host'], $proxies->all(), true);
})->descriptions('List all the proxy sites');
/**
* Remove a proxied site.
*
* @param string $site The site
*/
$app->command('unproxy site', function ($site) {
Site::proxyDelete($site);
Nginx::restart();
})->descriptions('Remove a proxied site')->addUsage("unproxy site1");
/**
* List all the parked, linked, and proxied sites.
*/
$app->command('sites', function () {
$parked = Site::parked();
$links = Site::links();
$proxies = Site::proxies();
if (count($parked) === 0 && count($links) === 0 && count($proxies) === 0) {
return warning("No sites found within Valet.");
}
// Recreate the list of links and remove the key of alias and aliasUrl,
// as this is unnecessary for this command.
$parked = $parked->map(function ($value, $key) {
unset($value['secured']);
unset($value['alias']);
unset($value['aliasUrl']);
$php = explode(" ", $value['php']);
if (str_contains($php[0], "<info>")) {
$php[0] = str_replace("<info>", "", $php[0]);
$php[1] = str_replace("</info>", "", $php[1]);
$value['php'] = "<info>{$php[0]}</info>" . "\n" . "<info>{$php[1]}</info>";
}
else {
$value['php'] = $php[0] . "\n" . $php[1];
}
return $value;
});
$links = $links->map(function ($value, $key) {
unset($value['secured']);
unset($value['alias']);
unset($value['aliasUrl']);
return $value;
});
$proxies = $proxies->map(function ($value, $key) {
unset($value['secured']);
return $value;
});
if (count($parked) > 0) {
table(['Site', 'PHP', 'URL', 'Path'], $parked->all(), true, "Parked");
}
if (count($links) > 0) {
table(['Site', 'PHP', 'URL', 'Path'], $links->all(), true, "Linked");
}
if (count($proxies) > 0) {
table(['Site', 'URL', 'Host'], $proxies->all(), true, "Proxied");
}
})->descriptions('List all the parked, linked, and proxied sites');
/**
* Secure the current working directory with a trusted TLS certificate.
*
* @param string $site Optionally specify the site.
*/
$app->command('secure [site]', function ($site = null) {
$url = Site::getSiteURL($site);
Site::secure($url);
Nginx::restart();
info('The [' . $url . '] site has been secured with a fresh TLS certificate and will now be served over HTTPS.');
})->descriptions('Secure the current working directory with a trusted TLS certificate', [
"site" => "Optionally specify the site"
])->addUsage("secure site1");
/**
* List all secured sites.
*/
$app->command('secured', function ($output) {
$sites = collect(Site::secured())->map(function ($url) {
return ['Site' => $url];
});
if (count($sites) === 0) {
info("There are no secured sites.");
return false;
}
table(['Site'], $sites->all(), true);
})->descriptions('List all secured sites');
/**
* Unsecure the current working directory
*
* @param string $site Optionally specify the site.
* @param bool $all Optionally unsecure all secured sites
*/
$app->command('unsecure [site] [--all]', function ($site = null, $all = null) {
if ($all) {
Site::unsecureAll();
Nginx::restart();
return;
}
$url = Site::getSiteURL($site);
Site::unsecure($url);
Nginx::restart();
info('The [' . $url . '] site has been unsecured and will now be served over HTTP.');
})->descriptions('Unsecure the current working directory', [
"site" => "Optionally specify the site.",
"--all" => "Optionally unsecure all secured sites"
])->addUsage("unsecure mySite")->addUsage("unsecure --all");
/**
* Change the default PHP version used by Valet.
*
* @param string $phpVersion The PHP version eg. 8.1.8, or an alias eg. 8.1 to be set as the default
*
* ###### Note: If using the alias, and multiple versions of 8.1 are available eg. 8.1.8 and 8.1.18; then the most latest version will be used, eg. 8.1.18.
*/
$app->command('use [phpVersion]', function ($phpVersion) {
if (empty($phpVersion)) {
warning('Please enter a PHP version. Example command <bg=magenta> valet use 8.1 </>');
return;
}
$php = Configuration::getPhpByVersion($phpVersion);
info("Setting the default PHP version to [$phpVersion].");
Configuration::updateKey('default_php', $php['version']);
info('Stopping Nginx...');
Nginx::stop();
Nginx::installConfiguration();
Nginx::installServer();
Nginx::installNginxDirectory();
Nginx::installService();
Nginx::restart();
info(sprintf('Valet is now using %s.', $php['version']) . PHP_EOL);
info('Note that you might need to run <comment>composer global update</comment> if your PHP version change affects the dependencies of global packages required by Composer.');
})->descriptions('Change the default PHP version used by Valet.', [
'phpVersion' => 'The PHP version you want to use, e.g 8.1, 8.1.8'
])->addUsage("use 8.1")->addUsage("use 8.1.8");
/**
* Isolate the current working directory to specific PHP version.
*
* @param string $phpVersion The PHP version you want to use, eg. "7.4.33"; or an alias, eg. "7.4"
* @param array $site Optionally specify the site.
* To specify multiple sites, you use it like:
* `--site=my-project --site=another-site`
*/
$app->command('isolate [phpVersion] [--site=]*', function ($phpVersion, $site = []) {
if (empty($phpVersion)) {
warning('Please enter a PHP version. Example command <bg=magenta> valet isolate 7.4 </>');
return;
}
// If $site is empty, then isolate the current working directory.
if (!$site) {
info("Isolating the current working directory...");
Site::isolate($phpVersion, $site);
info("The current working directory is now using $phpVersion.");
info('Restarting Nginx...');
Nginx::restart();
return;
}
// Loop through the sites array and isolate each one.
foreach ($site as $sitename) {
Site::isolate($phpVersion, $sitename);
info("The site [$sitename] is now using $phpVersion.");
}
info('Restarting Nginx...');
Nginx::restart();
})->descriptions('Isolate the current working directory to a specific PHP version', [
'phpVersion' => 'The PHP version you want to use; e.g 7.4, 7.4.33',
'--site' => 'Optionally specify the site.'
])->addUsage("isolate 7.4")->addUsage("isolate 7.4 --site=mySite")->addUsage("isolate 7.4.33 --site=mySite --site=site2");
/**
* List all isolated sites.
*/
$app->command('isolated', function ($output) {
$isolated = Site::isolated();
if (count($isolated) === 0) {
info("There are no isolated sites.");
return;
}
table(["Site", "PHP"], $isolated->all(), true);
})->descriptions('List all isolated sites.');
/**
* Remove [unisolate] the current working directory's site
*
* @param string $site Optionally specify the site
* @param bool $all Optionally unisolates all isolated sites
*/
$app->command('unisolate [--site=] [--all]', function ($output, $site = null, $all = null) {
if ($all) {
$isolated = Site::isolated();
foreach ($isolated as $array) {
Site::unisolate($array["site"]);
info(sprintf('The site [%s] is now using the default PHP version.', $array["site"]));
}
info('Restarting Nginx...');
Nginx::restart();
return;
}
if (!$site) {
info("Unisolating the current working directory...");
}
Site::unisolate($site);
info(sprintf('The site [%s] is now using the default PHP version.', $site));
info('Restarting Nginx...');
Nginx::restart();
})->descriptions('Remove [unisolate] the current working directory\'s site', [
'--site' => 'Optionally specify the site',
'--all' => 'Optionally remove all isolated sites'
])->addUsage("unisolate --site=mySite")->addUsage("unisolate --all");
/**
* Sharing
*/
// TODO: Add support for other share tools.
// https://github.com/robbie-cahill/tunnelmole-client
/**
* Get or set the name of the currently-selected share tool.
*
* @param string|null $tool Optionally, the share tool to set.
*/
$app->command('share-tool [tool]', function ($tool = null) {
$share_tools_list = Share::getShareTools();
// If a tool is not provided, then we want to get the current tool.
if ($tool === null) {
// Get the current share tool.
$shareTool = Share::getCurrentShareTool();
// If there is no share tool set, output a warning message.
if ($shareTool === null) {
return warning("There is no share tool set. The supported tools are: $share_tools_list");
}
// Otherwise, output the current share tool.
return info("The current share tool is: $shareTool");
}
// If the tool is not valid, output a warning message.
if (!Share::isToolValid($tool)) {
return warning("$tool is not a valid share tool. Please use $share_tools_list.");
}
// Otherwise, update the share tool key in the config.
Configuration::updateKey('share-tool', $tool);
info("Share tool set to $tool.");
})->descriptions('Get or set the name of the current share tool.', [
"tool" => "Optionally, set the current share tool"
]);
/**
* Share the current working directory site with a publically accessible URL.
*
* @param string $site Optionally, specify a site.
*
* @param string|null $options Optionally, specify ngrok options/flags of its `http` command to pass to ngrok.
* ---
* Pass the option name without the `--` prefix (so Valet doesn't get confused with its own options); eg. `--options domain=example.com`.
* If there's a space in the value you will need to surround the value in quotes.
* All options will be prefixed with `--` after Valet has processed the command.
* If the `host-header` option is not set, Valet will add it with the default of `rewrite`.
* Multiple options must be separated by double slashes `//`.
* ---
*
* The options `=` is optional:
* - `--options=[option]`
* - `--options [option]`
* - `-o [option]`
* ---
* `valet share mysite --options domain=example.com//region=eu//request-header-remove="blah blah"`
*/
$app->command('share [site] [-o|--options=]', function ($input, $site = null, $options = null) {
// If current share tool is set...
if (Share::getCurrentShareTool() === null) {
return info("Please set your share tool with `valet share-tool`.");
}
// Send an error if the option shortcut has a =
if (str_contains($input, "-o=")) {
return error("Option shortcuts cannot have a <bg=magenta> = </> immediately after them.\nPlease use a space to separate it from the value: <bg=magenta> valet share $site -o " . preg_replace("/=/", "", $options, 1) . " </>");
}
$tld = Configuration::read()['tld'];
// Remove the tld from the site if it's included, because it'll get added back in the URL variable and this can cause issues if it's included twice.
$site = str_replace(".$tld", "", $site);
$url = ($site ?: strtolower(Site::host(getcwd()))) . '.' . $tld;
$options = $options != null ? explode("//", $options) : [];
Share::shareTool()->start($url, Site::port($url), $options);
})->descriptions('Share the current working directory site with a publically accessible URL', [
"site" => "Optionally, the site name",
"--options" => "Optionally, specify ngrok options/flags of its `http` command to pass to ngrok, without the leading <fg=green>--</>. Multiple options must be separated by double slashes <fg=green>//</>."
])->addUsage("share mysite")->addUsage("share mysite --options domain=example.com//region=eu")->addUsage("share -o domain=example.com");
/**
* Get and copy the public URL of the current working directory site
* that is currently being shared
*
* @param string|null $site Optionally, specify a site
*/
$app->command('fetch-share-url [site]', function ($site = null) {
// If current share tool is not set, output a message.
if (Share::getCurrentShareTool() === null) {
return info("Please set your share tool with `valet share-tool`.");
}
$site = $site ?: Site::host(getcwd()) . '.' . Configuration::read()['tld'];
$url = Share::shareTool()->currentTunnelUrl($site);
info("The public URL for $site is: <fg=blue>$url</>");
})->setAliases(["url"])->descriptions('Get and copy the public URL of the current working directory site that is currently being shared', [
"site" => "Optionally, specify a site"
])->addUsage("fetch-share-url site1")->addUsage("url site1");
/**
* Sharing - ngrok
*/
/**
* Set the ngrok authtoken.
*
* @param string $token Your personal ngrok authtoken
*/
$app->command('set-ngrok-token [token]', function ($token = null) {
if (Share::getCurrentShareTool() != "ngrok") {
return info("Please set ngrok as your share tool with `valet share-tool ngrok`.");
}