generated from wpbones/WPKirk-Boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bones
executable file
·2841 lines (2387 loc) · 82.5 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;
}
}
}
/**
* 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.8.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 "\n\033[33;5;82mWarning!!\n";
echo "\n\033[38;5;82m\t" .
'You must run with PHP version ' .
WPBONES_MINIMAL_PHP_VERSION .
' or greater';
echo "\033[0m\n\n";
exit();
}
/**
* @class BonesCommandLine
*/
class BonesCommandLine
{
/**
* 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->commandParams());
} // install
elseif ($this->isCommand('install')) {
$this->install($this->commandParams());
} // 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(int $index = null): ?array
{
// 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 {
/**
* MARK: Load WordPress
* We have to load the WordPress environment.
*/
$currentDir = $_SERVER['PWD'] ?? __DIR__;
$wpLoadPath = dirname(dirname(dirname($currentDir))) . '/wp-load.php';
if (!file_exists($wpLoadPath)) {
echo "\n\033[33;5;82mWarning!!\n";
echo "\n\033[38;5;82m\t" .
'You must be inside "wp-content/plugins/" folders';
echo "\033[0m\n\n";
exit();
}
require $wpLoadPath;
} catch (Exception $e) {
echo "\n\033[33;5;82mWarning!!\n";
echo "\n\033[38;5;82m\t" . 'Can\'t load WordPress';
echo "\033[0m\n\n";
}
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) {
echo "\n\033[33;5;82mWarning!!\n";
echo "\n\033[38;5;82m\t" . 'Can\'t load Composer';
}
try {
/**
* --------------------------------------------------------------------------
* Load this plugin env
* --------------------------------------------------------------------------
*/
if (file_exists(__DIR__ . '/bootstrap/plugin.php')) {
require_once __DIR__ . '/bootstrap/plugin.php';
}
} catch (Exception $e) {
echo "\n\033[33;5;82mWarning!!\n";
echo "\n\033[38;5;82m\t" . 'Can\'t load this plugin env';
}
}
/** 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) {
echo "\n\033[33;5;82mWarning!!\n";
echo "\n\033[38;5;82m\t" . 'Can\'t load console kernel';
}
}
/**
* Return the current Plugin namespace defined in the namespace file.
*
* @return string
*/
public function getNamespace(): string
{
[$null, $namespace] = $this->getPluginNameAndNamespace();
return $namespace;
}
/**
* Return the current Plugin name and namespace defined in the namespace file.
*
* @return array
*/
public function getPluginNameAndNamespace(): array
{
return explode(',', file_get_contents('namespace'));
}
/**
* Return TRUE if the command is in console argument.
*
* @param string $command Bones command to check.
* @return bool
*/
protected function isCommand(string $command): bool
{
$arguments = $this->arguments();
return $command === ($arguments[0] ?? '');
}
/** Display the WP-CLI version in the bones console. */
protected function wpCliInfoHelp()
{
if ($this->wpCliVersion) {
$this->info("→ WP-CLI version: {$this->wpCliVersion}");
} else {
$this->warning("⚠️ WP-CLI not found: we recommend to install it globally - https://wp-cli.org/#installing\n");
}
}
/** Display the full help. */
protected function help()
{
$colorCodes = [
[51, 45, 39, 33, 27, 21],
[249, 248, 247, 246, 245, 244],
[88, 89, 90, 91, 92, 93],
[76, 82, 46, 40, 34, 28],
[201, 200, 199, 198, 197, 196],
[243, 242, 241, 240, 239, 238]
];
$colorCode = $colorCodes[rand(0, count($colorCodes) - 1)];
echo "\n\033[38;5;{$colorCode[0]}m██╗ ██╗██████╗ ██████╗ ██████╗ ███╗ ██╗███████╗███████╗\033[0m\n";
echo "\033[38;5;{$colorCode[1]}m██║ ██║██╔══██╗ ██╔══██╗██╔═══██╗████╗ ██║██╔════╝██╔════╝\033[0m\n";
echo "\033[38;5;{$colorCode[2]}m██║ █╗ ██║██████╔╝ ██████╔╝██║ ██║██╔██╗ ██║█████╗ ███████╗\033[0m\n";
echo "\033[38;5;{$colorCode[3]}m██║███╗██║██╔═══╝ ██╔══██╗██║ ██║██║╚██╗██║██╔══╝ ╚════██║\033[0m\n";
echo "\033[38;5;{$colorCode[4]}m╚███╔███╔╝██║ ██████╔╝╚██████╔╝██║ ╚████║███████╗███████║\033[0m\n";
echo "\033[38;5;{$colorCode[5]}m ╚══╝╚══╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚══════╝╚══════╝\033[0m\n";
$this->line(" Bones Version " . self::VERSION . "\n");
$this->wpCliInfoHelp();
$this->info('→ Current Plugin Filename, Name, Namespace:', false);
$this->line(" '{$this->getMainPluginFile()}', '{$this->getPluginName()}', '{$this->getNamespace()}'\n");
$this->info('Usage:');
$this->line(" command [options] [arguments]\n");
$this->info('Available commands:');
$this->line(' deploy Create a deploy version');
$this->line(' install Install a new WP Bones plugin');
$this->line(' optimize Run composer dump-autoload with -o option');
$this->line(' plugin Perform plugin operations');
$this->line(' rename Rename the plugin name and the namespace');
$this->line(' require Install a WP Bones package');
$this->line(' tinker Interact with your application');
$this->line(' update Update the Framework');
$this->line(' version Update the Plugin version');
$this->info('migrate');
$this->line(' migrate:create Create a new Migration');
$this->info('make');
$this->line(' make:ajax Create a new Ajax service provider class');
$this->line(' make:api Create a new API controller class');
$this->line(' make:console Create a new Bones command');
$this->line(' make:controller Create a new controller class');
$this->line(' make:cpt Create a new Custom Post Type service provider class');
$this->line(' make:ctt Create a new Custom Taxonomy Type service provider class');
$this->line(' make:eloquent-model Create a new Eloquent database model class');
$this->line(' make:model Create a new database model class');
$this->line(' make:schedule Create a new schedule (cron) service provider class');
$this->line(' make:shortcode Create a new Shortcode service provider class');
$this->line(' make:provider Create a new service provider class');
$this->line(' make:widget Create a new Widget service provider class');
if ($this->kernel && $this->kernel->hasCommands()) {
$this->info('Extensions');
$this->kernel->displayHelp();
}
echo "\n\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 info(string $str, $newLine = true)
{
echo "\033[38;5;213m" . $str;
echo "\033[0m";
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)
{
echo "\033[38;5;82m" . $str;
echo "\033[0m";
echo $newLine ? "\n" : '';
}
/* Commodity to display an error message in the console. */
protected function error(string $str, $newLine = true)
{
echo "\033[41m";
echo $newLine ? "\n" : '';
echo "\033[41;255m" . $str;
echo $newLine ? "\n" : '';
echo "\033[0m";
echo $newLine ? "\n" : '';
}
/* Commodity to display an info message in the console. */
protected function warning(string $str, $newLine = true)
{
echo "\e[31m";
echo $newLine ? "\n" : '';
echo $str;
echo $newLine ? "\n" : '';
echo "\e[0m";
echo $newLine ? "\n" : '';
}
/**
* Return the default Plugin filename as snake case from the plugin name.
*
* @param string|null $pluginName
* @return string
*/
public function getMainPluginFile(?string $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;
}
/**
* This is the most important function of WP Bones.
* Here we will rename all occurrences of the plugin name and namespace.
* The default plugin name is 'WP Kirk' and the default namespace is 'WPKirk'.
* Here we will create also the slug used in the plugin.
*
* For example, if the plugin name is 'My WP Plugin'
*
* My WP Plugin Name of plugin
* MyWPPlugin Namespace, see [PSR-4 autoload standard](http://www.php-fig.org/psr/psr-4/)
* my_wp_plugin_slug Plugin slug
* my_wp_plugin_vars Plugin vars used for CPT, taxonomy, etc.
* my-wp-plugin Internal id used for css, js and less files
*
* As you can see we're going to create all namespace/id from the plugin name.
*
* @brief Rename the plugin
*
*/
protected function rename($args)
{
if ($this->isHelp()) {
$this->info('Usage:');
$this->line(' php bones rename [options] <Plugin Name> <Namespace>');
$this->info('Available options:');
$this->line(' --reset Reset the plugin name and namespace');
$this->line(' --update Rename after an update. For example after install a new package');
exit();
}
$arg_option_plugin_name = $args[0] ?? null;
switch ($arg_option_plugin_name) {
case '--reset':
$this->resetPluginNameAndNamespace();
break;
case '--update':
$this->updatePluginNameAndNamespace();
break;
default:
[$search_plugin_name, $search_namespace, $plugin_name, $namespace, $mainPluginFile] = $this->getAskPluginNameAndNamespace($args);
$this->info("\nThe new plugin filename, name and namespace will be '{$mainPluginFile}', '{$plugin_name}', '{$namespace}'");
$yesno = $this->ask('Continue (y/n)', 'n');
if (strtolower($yesno) != 'y') {
return;
}
$this->setPluginNameAndNamespace($search_plugin_name, $search_namespace, $plugin_name, $namespace);
$this->optimize();
break;
}
}
/**
* Commodity function to check if help has been requested.
*
* @param string|null $str Optional. Command to check.
*
* @return bool
*/
protected function isHelp(string $str = null): bool
{
if (!is_null($str)) {
return empty($str) || $str === '--help';
}
$param = $this->commandParams()[0] ?? null;
return !empty($param) && $param === '--help';
}
/**
* 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 commandParams(int $index = null)
{
$params = $this->arguments();
// strip the command name
array_shift($params);
return !is_null($index) ? $params[$index] ?? null : $params;
}
/* Reset the plugin name and namespace to the original values */
protected function resetPluginNameAndNamespace()
{
// use the current plugin name and namespace from the namespace file
$search_plugin_name = $this->getPluginName();
$search_namespace = $this->getNamespace();
[$plugin_name, $namespace] = $this->getDefaultPluginNameAndNamespace();
$this->setPluginNameAndNamespace($search_plugin_name, $search_namespace, $plugin_name, $namespace);
}
/* Return the default plugin name and namespace. */
protected function getDefaultPluginNameAndNamespace(): array
{
return ['WP Kirk', 'WPKirk'];
}
/**
* Update the plugin name and namespace
*
* @param string $search_plugin_name The previous plugin name
* @param string $search_namespace The previous namespace
* @param string $plugin_name The new plugin name
* @param string $namespace The new namespace
*/
protected function setPluginNameAndNamespace(string $search_plugin_name, string $search_namespace, string $plugin_name, string $namespace)
{
$mainPluginFile = $this->getMainPluginFile($plugin_name);
$currentMainPluginFile = $this->getMainPluginFile($search_plugin_name);
// check if "index.php" exists
if (file_exists('index.php') && !file_exists($currentMainPluginFile)) {
$this->info("The file '{$currentMainPluginFile}' doesn't exists. Maybe you are updating from a < 1.5 version.");
$currentMainPluginFile = 'index.php';
}
@rename($currentMainPluginFile, $mainPluginFile);
// start scan everything
$files = array_filter(
array_map(function ($e) {
// exclude node_modules and bones executable
if (
false !== strpos($e, 'node_modules') ||
false !== strpos($e, 'vendor/wpbones/wpbones/src/Console/bin/bones')
) {
return false;
}
return $e;
}, $this->recursiveScan('*'))
);