generated from wpbones/WPKirk-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bones
executable file
·3021 lines (2537 loc) · 88.3 KB
/
bones
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
#!/usr/bin/env php
<?php
/**
* This file is part of SemVer.
*
* https://github.com/PHLAK/SemVer
*/
namespace Bones\SemVer\Exceptions {
use Exception;
class InvalidVersionException extends Exception {}
}
namespace Bones\SemVer\Traits {
use Bones\SemVer\Version;
trait Comparable
{
/**
* Check if this Version object is greater than another.
*
* @param Version $version An instance of SemVer/Version
*
* @return bool True if this Version object is greater than the comparing
* object, otherwise false
*/
public function gt(Version $version): bool
{
return self::compare($this, $version) > 0;
}
/**
* Compare two versions. Returns -1, 0 or 1 if the first version is less
* than, equal to or greater than the second version respectively.
*
* @param Version $version1 An instance of SemVer/Version
* @param Version $version2 An instance of SemVer/Version
*/
public static function compare(Version $version1, Version $version2): int
{
$v1 = [$version1->major, $version1->minor, $version1->patch];
$v2 = [$version2->major, $version2->minor, $version2->patch];
$baseComparison = $v1 <=> $v2;
if ($baseComparison !== 0) {
return $baseComparison;
}
if ($version1->preRelease !== null && $version2->preRelease === null) {
return -1;
}
if ($version1->preRelease === null && $version2->preRelease !== null) {
return 1;
}
$v1preReleaseParts = explode('.', $version1->preRelease ?? '');
$v2preReleaseParts = explode('.', $version2->preRelease ?? '');
$preReleases1 = array_pad(
$v1preReleaseParts,
count($v2preReleaseParts),
null
);
$preReleases2 = array_pad(
$v2preReleaseParts,
count($v1preReleaseParts),
null
);
return $preReleases1 <=> $preReleases2;
}
/**
* Check if this Version object is less than another.
*
* @param Version $version An instance of SemVer/Version
*
* @return bool True if this Version object is less than the comparing
* object, otherwise false
*/
public function lt(Version $version): bool
{
return self::compare($this, $version) < 0;
}
/**
* Check if this Version object is equal to than another.
*
* @param Version $version An instance of SemVer/Version
*
* @return bool True if this Version object is equal to the comparing
* object, otherwise false
*/
public function eq(Version $version): bool
{
return self::compare($this, $version) === 0;
}
/**
* Check if this Version object is not equal to another.
*
* @param Version $version An instance of SemVer/Version
*
* @return bool True if this Version object is not equal to the comparing
* object, otherwise false
*/
public function neq(Version $version): bool
{
return self::compare($this, $version) !== 0;
}
/**
* Check if this Version object is greater than or equal to another.
*
* @param Version $version An instance of SemVer/Version
*
* @return bool True if this Version object is greater than or equal to the
* comparing object, otherwise false
*/
public function gte(Version $version): bool
{
return self::compare($this, $version) >= 0;
}
/**
* Check if this Version object is less than or equal to another.
*
* @param Version $version An instance of SemVer/Version
*
* @return bool True if this Version object is less than or equal to the
* comparing object, otherwise false
*/
public function lte(Version $version): bool
{
return self::compare($this, $version) <= 0;
}
}
trait Incrementable
{
/**
* Increment the major version value by one.
*
* @return self This Version object
*/
public function incrementMajor(): self
{
$this->setMajor($this->major + 1);
return $this;
}
/**
* Increment the minor version value by one.
*
* @return self This Version object
*/
public function incrementMinor(): self
{
$this->setMinor($this->minor + 1);
return $this;
}
/**
* Increment the pre-release version value by one.
*
* @return self This Version object
*/
public function incrementPreRelease(): self
{
if (empty($this->preRelease)) {
$this->incrementPatch();
$this->setPreRelease('1');
return $this;
}
$identifiers = explode('.', $this->preRelease);
if (!is_numeric(end($identifiers))) {
$this->setPreRelease(implode('.', [$this->preRelease, '1']));
return $this;
}
array_push($identifiers, (string)((int)array_pop($identifiers) + 1));
$this->setPreRelease(implode('.', $identifiers));
return $this;
}
/**
* Increment the patch version value by one.
*
* @return self This Version object
*/
public function incrementPatch(): self
{
$this->setPatch($this->patch + 1);
return $this;
}
}
}
namespace Bones\SemVer {
use Bones\SemVer\Exceptions\InvalidVersionException;
use Bones\SemVer\Traits\Comparable;
use Bones\SemVer\Traits\Incrementable;
/**
* @property int $major Major release number
* @property int $minor Minor release number
* @property int $patch Patch release number
* @property string|null $preRelease Pre-release value
* @property string|null $build Build release value
*/
class Version
{
use Comparable;
use Incrementable;
/** @var int Major release number */
protected $major;
/** @var int Minor release number */
protected $minor;
/** @var int Patch release number */
protected $patch;
/** @var string|null Pre-release value */
protected $preRelease;
/** @var string|null Build release value */
protected $build;
/**
* Class constructor, runs on object creation.
*
* @param string $version Version string
*
* @throws InvalidVersionException
*/
public function __construct(string $version = '0.1.0')
{
$this->setVersion($version);
}
/**
* Set (override) the entire version value.
*
* @param string $version Version string
*
* @return self This Version object
* @throws InvalidVersionException
*
*/
public function setVersion(string $version): self
{
$semverRegex =
'/^v?(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:-(?<pre_release>[0-9A-Za-z-.]+))?(?:\+(?<build>[0-9A-Za-z-.]+)?)?$/';
if (!preg_match($semverRegex, $version, $matches)) {
throw new InvalidVersionException(
'Invalid semantic version string provided'
);
}
$this->major = (int)$matches['major'];
$this->minor = (int)$matches['minor'];
$this->patch = (int)$matches['patch'];
$this->preRelease = $matches['pre_release'] ?? null;
$this->build = $matches['build'] ?? null;
return $this;
}
/**
* Attempt to parse an incomplete version string.
*
* Examples: 'v1', 'v1.2', 'v1-beta.4', 'v1.3+007'
*
* @param string $version Version string
*
* @return self This Version object
* @throws InvalidVersionException
*
*/
public static function parse(string $version): self
{
$semverRegex =
'/^v?(?<major>\d+)(?:\.(?<minor>\d+)(?:\.(?<patch>\d+))?)?(?:-(?<pre_release>[0-9A-Za-z-.]+))?(?:\+(?<build>[0-9A-Za-z-.]+)?)?$/';
if (!preg_match($semverRegex, $version, $matches)) {
throw new InvalidVersionException(
'Invalid semantic version string provided'
);
}
$version = sprintf(
'%s.%s.%s',
$matches['major'],
$matches['minor'] ?? 0,
$matches['patch'] ?? 0
);
if (!empty($matches['pre_release'])) {
$version .= '-' . $matches['pre_release'];
}
if (!empty($matches['build'])) {
$version .= '+' . $matches['build'];
}
return new self($version);
}
/**
* Magic get method; provides access to version properties.
*
* @param string $property Version property
*
* @return mixed Version property value
*/
public function __get(string $property)
{
return $this->$property;
}
/**
* Magic toString method; allows object interaction as if it were a string.
*
* @return string Current version string
*/
public function __toString(): string
{
$version = implode('.', [$this->major, $this->minor, $this->patch]);
if (!empty($this->preRelease)) {
$version .= '-' . $this->preRelease;
}
if (!empty($this->build)) {
$version .= '+' . $this->build;
}
return $version;
}
/**
* Set the major version to a custom value.
*
* @param int $value Positive integer value
*
* @return self This Version object
*/
public function setMajor(int $value): self
{
$this->major = $value;
$this->setMinor(0);
return $this;
}
/**
* Set the minor version to a custom value.
*
* @param int $value Positive integer value
*
* @return self This Version object
*/
public function setMinor(int $value): self
{
$this->minor = $value;
$this->setPatch(0);
return $this;
}
/**
* Set the patch version to a custom value.
*
* @param int $value Positive integer value
*
* @return self This Version object
*/
public function setPatch(int $value): self
{
$this->patch = $value;
$this->setPreRelease(null);
$this->setBuild(null);
return $this;
}
/**
* Set the pre-release string to a custom value.
*
* @param string|null $value A new pre-release value
*
* @return self This Version object
*/
public function setPreRelease($value): self
{
$this->preRelease = $value;
return $this;
}
/**
* Set the build string to a custom value.
*
* @param string|null $value A new build value
*
* @return self This Version object
*/
public function setBuild($value): self
{
$this->build = $value;
return $this;
}
/**
* Get the version string prefixed with a custom string.
*
* @param string $prefix String to prepend to the version string
* (default: 'v')
*
* @return string Prefixed version string
*/
public function prefix(string $prefix = 'v'): string
{
return $prefix . (string)$this;
}
}
}
namespace Bones\Traits {
// Standard Color Definitions
define('WPBONES_COLOR_BLACK', "\033[0;30m");
define('WPBONES_COLOR_RED', "\033[0;31m");
define('WPBONES_COLOR_GREEN', "\033[0;32m");
define('WPBONES_COLOR_YELLOW', "\033[0;33m");
define('WPBONES_COLOR_BLUE', "\033[0;34m");
define('WPBONES_COLOR_MAGENTA', "\033[0;35m");
define('WPBONES_COLOR_CYAN', "\033[0;36m");
define('WPBONES_COLOR_WHITE', "\033[0;37m");
// Definition of bold colors
define('WPBONES_COLOR_BOLD_BLACK', "\033[1;30m");
define('WPBONES_COLOR_BOLD_RED', "\033[1;31m");
define('WPBONES_COLOR_BOLD_GREEN', "\033[1;32m");
define('WPBONES_COLOR_BOLD_YELLOW', "\033[1;33m");
define('WPBONES_COLOR_BOLD_BLUE', "\033[1;34m");
define('WPBONES_COLOR_BOLD_MAGENTA', "\033[1;35m");
define('WPBONES_COLOR_BOLD_CYAN', "\033[1;36m");
define('WPBONES_COLOR_BOLD_WHITE', "\033[1;37m");
// Definition of Light Colors
define('WPBONES_COLOR_LIGHT_BLACK', "\033[0;38;5;240m");
define('WPBONES_COLOR_LIGHT_RED', "\033[0;38;5;203m");
define('WPBONES_COLOR_LIGHT_GREEN', "\033[0;38;5;82m");
define('WPBONES_COLOR_LIGHT_YELLOW', "\033[0;38;5;227m");
define('WPBONES_COLOR_LIGHT_BLUE', "\033[0;38;5;117m");
define('WPBONES_COLOR_LIGHT_MAGENTA', "\033[0;38;5;213m");
define('WPBONES_COLOR_LIGHT_CYAN', "\033[0;38;5;159m");
define('WPBONES_COLOR_LIGHT_WHITE', "\033[0;38;5;15m");
// Definition for color reset
define('WPBONES_COLOR_RESET', "\033[0m");
// Definition for cursor up
define('WPBONES_CURSOR_UP', "\033[1A");
trait Console
{
/**
* Commodity to display a message in the console with color.
*
* @param string $str The message to display.
* @param string $color The color to use. Default is 'white'.
*/
protected function color($str, $color = WPBONES_COLOR_YELLOW)
{
echo $color . $str . WPBONES_COLOR_RESET;
}
/**
* Commodity to display a message in the console.
*
* @param string $str The message to display.
* @param bool $newLine Optional. Whether to add a new line at the end.
*/
protected function info(string $str, $newLine = true)
{
$this->color($str, WPBONES_COLOR_LIGHT_MAGENTA);
echo $newLine ? "\n" : '';
}
/**
* Commodity to display a message in the console.
*
* @param string $str The message to display.
* @param bool $newLine Optional. Whether to add a new line at the end.
*/
protected function line(string $str, $newLine = true)
{
$this->color($str, WPBONES_COLOR_LIGHT_GREEN);
echo $newLine ? "\n" : '';
}
/* Commodity to display an error message in the console. */
protected function error(string $str, $newLine = true)
{
echo "❌ ";
$this->color($str, WPBONES_COLOR_BOLD_RED);
echo $newLine ? "\n" : '';
}
/* Commodity to display an info message in the console. */
protected function warning(string $str, $newLine = true)
{
echo "❗ ";
$this->color($str, WPBONES_COLOR_BOLD_YELLOW);
echo $newLine ? "\n" : '';
}
/* Commodity to display a success message in the console. */
protected function success($str)
{
$this->color("✅ " . $str, WPBONES_COLOR_GREEN);
}
protected function startCommand($str)
{
$this->color("------------------------------------------------" . "\n", WPBONES_COLOR_BLUE);
$this->color("🙌 " . $str . " " . $this->getPluginName() . "\n", WPBONES_COLOR_BLUE);
$this->color("------------------------------------------------" . "\n", WPBONES_COLOR_BLUE);
}
/* Commodity to display a start progress message in the console. */
protected function startProgress($str)
{
$this->color("🚧 " . $str . "\n", WPBONES_COLOR_GREEN);
}
/* Commodity to replace a previous progress message in the console with cursor up. */
protected function endProgress()
{
echo WPBONES_CURSOR_UP . WPBONES_COLOR_GREEN . "✅" . WPBONES_COLOR_RESET . "\n";
}
/* Commodity to display a completed message in the console. */
protected function processCompleted($str)
{
$this->color("✅ " . $str . "\n", WPBONES_COLOR_GREEN);
}
/**
* Get input from console
*
* @param string $str The question to ask
* @param string|null $default The default value
*/
protected function ask(string $str, ?string $default = ''): string
{
$str = WPBONES_COLOR_GREEN .
"❓ $str" .
(empty($default) ? ': ' : " (default: {$default}): ") .
WPBONES_COLOR_RESET;
// Use readline to get the user input
$line = readline($str);
// Trim the input to remove extra spaces or newlines
$line = trim($line);
return $line ?: $default;
}
}
}
/**
* Bones
*
* @package Bones
*/
namespace Bones {
/* The minimum PHP version required to run Bones. */
define('WPBONES_MINIMAL_PHP_VERSION', '7.4');
/* MARK: The WP Bones command line version. */
define('WPBONES_COMMAND_LINE_VERSION', '1.9.0');
use Bones\SemVer\Exceptions\InvalidVersionException;
use Bones\SemVer\Version;
use Exception;
if (!function_exists('semver')) {
/**
* Create a SemVer version object.
*
* @throws InvalidVersionException
*/
function semver(string $string): Version
{
return new Version($string);
}
}
if (version_compare(PHP_VERSION, WPBONES_MINIMAL_PHP_VERSION) < 0) {
echo WPBONES_COLOR_BOLD_RED .
"❌ Error! You must run with PHP version " . WPBONES_MINIMAL_PHP_VERSION . " or greater\n" .
WPBONES_COLOR_RESET;
exit();
}
/**
* @class BonesCommandLine
*/
class BonesCommandLine
{
use Traits\Console;
/**
* WP Bones version
*/
const VERSION = WPBONES_COMMAND_LINE_VERSION;
/**
* Used for additional kernel command.
*
* @var null
*/
protected $kernel = null;
/**
* List of files and folders to skip during the deployment.
*
* @var array
*/
protected array $skipWhenDeploy = [];
/**
* Base folder during the deployment.
*
* @var string
*/
protected string $rootDeploy = '';
/**
* WP-CLI version
*
* @var string|null
* @since 1.6.0
*/
protected ?string $wpCliVersion = null;
public function __construct()
{
$this->boot();
}
/**
* This is a special bootstrap in order to avoid the WordPress and kernel environment
* when we have to rename the plugin and vendor structure.
*/
public function boot()
{
$arguments = $this->arguments();
// load the WordPress environment and the plugin
$this->loadWordPress();
// load the console kernel
$this->loadKernel();
// Check WP-CLI
$output = shell_exec('wp --info 2>&1');
if (strpos($output, 'WP-CLI version') !== false) {
preg_match('/WP-CLI version:\s+([\d\.]+)/', $output, $matches);
$this->wpCliVersion = $matches[1] ?? null;
}
// we won't load the WordPress environment for the following commands
if (empty($arguments) || $this->isCommand('--help')) {
$this->help();
} // rename
elseif ($this->isCommand('rename')) {
$this->rename($this->getCommandParams());
} // install
elseif ($this->isCommand('install')) {
$this->install($this->getCommandParams());
} // Update
elseif ($this->isCommand('update')) {
$this->update();
} // go ahead...
else {
// handle the rest of the commands except rename
$this->handle();
}
}
/**
* Return the arguments after "php bones".
*
* @param int|null $index Optional. Index of argument.
* If NULL will be returned the whole array.
*
* @return mixed|array
*/
protected function arguments($index = null)
{
// Check if 'argv' is set in the server variables
if (!isset($_SERVER['argv'])) {
return null;
}
$argv = $_SERVER['argv'];
if (!is_array($argv)) {
return $argv;
}
// Strip the application name
array_shift($argv);
// If $index is provided, return the specific argument or null if it doesn't exist
if (is_int($index)) {
return $argv[$index] ?? null;
}
// Return all arguments if no index is provided
return $argv;
}
/* Load WordPress core and all environment. */
protected function loadWordPress()
{
try {
// We have to load the WordPress environment.
$currentDir = $_SERVER['PWD'] ?? __DIR__;
$wpLoadPath = dirname(dirname(dirname($currentDir))) . '/wp-load.php';
if (!file_exists($wpLoadPath)) {
$this->error("Error! You need to be inside the wp-content/plugins/ folder");
exit();
}
require $wpLoadPath;
} catch (Exception $e) {
$this->error("Error! Can't load WordPress (" . $e->getMessage() . ")");
}
try {
/**
* --------------------------------------------------------------------------
* Register The Auto Loader
* --------------------------------------------------------------------------
* Composer provides an auto-generated class loader for our app. We just
* need to use it! Requiring it here means we don't have to load classes
* manually. Feels great to relax.
*/
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require __DIR__ . '/vendor/autoload.php';
}
} catch (Exception $e) {
$this->error("Error! Can't load Composer autoload (" . $e->getMessage() . ")");
exit();
}
try {
/**
* --------------------------------------------------------------------------
* Load this plugin env
* --------------------------------------------------------------------------
*/
if (file_exists(__DIR__ . '/bootstrap/plugin.php')) {
require_once __DIR__ . '/bootstrap/plugin.php';
}
} catch (Exception $e) {
$this->error("Error! Can't load the plugin env (" . $e->getMessage() . ")");
exit();
}
}
/** Check and load for console kernel extensions. */
protected function loadKernel()
{
// current plugin name and namespace
$namespace = $this->getNamespace();
$kernelClass = "{$namespace}\\Console\\Kernel";
$WPBonesKernelClass = "{$namespace}\\WPBones\\Foundation\\Console\\Kernel";
try {
if (class_exists($WPBonesKernelClass) && class_exists($kernelClass)) {
$this->kernel = new $kernelClass();
}
} catch (Exception $e) {
$this->error("Error! Can't load kernel console (" . $e->getMessage() . ")");
exit();
}
}
/**
* Commodity function to check if ClassName has been requested.
*
* @param string|null $className Optional. Command to check.
*
* @return string
*/
protected function askClassNameIfEmpty($className = ''): string
{
if (empty($className)) {
$className = $this->ask('ClassName');
if (empty($className)) {
$this->error('ClassName is required');
exit(0);
}
}
return $className;
}
/**
* Ask the user which package manager to use
*
* @since 1.9.0
* @return string|null The name of the package manager to use
*/
protected function askPackageManager()
{
$packageManager = null;
// get the available package manager
$listPackageManagers = $this->getListAvailablePackageManagers();
if ($listPackageManagers) {
// if we found just one package manager, we use it
if (count($listPackageManagers) === 1) {
return $listPackageManagers[0];
}
$availablePackageManagerStr = implode(', ', $listPackageManagers);
$this->info("🔍 Found package managers: " . $availablePackageManagerStr);
// check if "npm" is available and use it by default
if (in_array('npm', $listPackageManagers)) {
$packageManager = 'npm';
}
// ask which package manager to use
$packageManager = $this->ask('Which package manager do you want to use (' . $availablePackageManagerStr . ')', $packageManager);
}
return $packageManager;
}
/**
* Return the params after "php bones [command]".
*
* @param int|null $index Optional. Index of param.
* If NULL will be returned the whole array.
*
* @return array|string
*/
protected function getCommandParams($index = null)
{
$params = $this->arguments();
// strip the command name
array_shift($params);
return !is_null($index) ? $params[$index] ?? null : $params;
}
/* Return the default plugin name and namespace. */
protected function getDefaultPluginNameAndNamespace(): array
{
return ['WP Kirk', 'WPKirk'];
}
/**
* Return the current Plugin namespace defined in the namespace file.
*
* @return string
*/
public function getNamespace(): string
{
[$null, $namespace] = $this->getPluginNameAndNamespace();
return $namespace;
}
/**
* Return the default Plugin filename as snake case from the plugin name.
*
* @param string|null $pluginName
* @return string
*/
public function getMainPluginFile($pluginName = ''): string
{
if (empty($pluginName)) {
$pluginName = $this->getPluginName();
}
return strtolower(str_replace(' ', '-', $pluginName)) . '.php';
}
/**
* Return the current Plugin name defined in the namespace file.
*
* @return string
*/
public function getPluginName(): string
{
[$plugin_name] = $this->getPluginNameAndNamespace();
return $plugin_name;
}
/**
* Return the plugin slug.
*
* @param string|null $str
*/
public function getPluginSlug($str = null): string
{
$str = $this->snakeCasePluginName($str);
return $str . '_slug';
}
/**
* Return the plugin vars.
*
* @param string|null $str
* @return string
*/
public function getPluginVars($str = null): string
{
$str = $this->snakeCasePluginName($str);
return $str . '_vars';
}
/**
* Return the plugin id used for css, js, less and files.
* Currently, it's the sanitized plugin name.
*
* @param string|null $str
*
* @return string
*/
public function getPluginId($str = null): string
{
return $this->sanitizePluginName($str);
}
/**
* Return the current Plugin name and namespace defined in the namespace file.
*
* @return array
*/
public function getPluginNameAndNamespace(): array
{
return explode(',', file_get_contents('namespace'));
}
/**
* Help method to get the Domain Path from the plugin header
*
* @return string|null
*/
protected function getDomainPath()
{
$header = $this->extractPluginHeaderInfo('Domain Path');
if (isset($header['Domain Path'])) {
return $header['Domain Path'];
}
return null;
}
/**
* Returns the first package manager available.
*