1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\WP; |
4
|
|
|
|
5
|
|
|
use Composer\Script\Event; |
6
|
|
|
|
7
|
|
|
class ComposerScripts |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Handle the post-install Composer event. |
11
|
|
|
* |
12
|
|
|
* @param \Composer\Script\Event $event |
13
|
|
|
* @return void |
14
|
|
|
*/ |
15
|
|
|
public static function postInstall(Event $event) |
16
|
|
|
{ |
17
|
|
|
require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Handle the post-update Composer event. |
22
|
|
|
* |
23
|
|
|
* @param \Composer\Script\Event $event |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public static function postUpdate(Event $event) |
27
|
|
|
{ |
28
|
|
|
require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Handle the post-autoload-dump Composer event. |
33
|
|
|
* |
34
|
|
|
* @param \Composer\Script\Event $event |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public static function postAutoloadDump(Event $event) |
38
|
|
|
{ |
39
|
|
|
require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php'; |
40
|
|
|
|
41
|
|
|
$dir = $event->getComposer()->getConfig()->get('vendor-dir').'/../'; |
|
|
|
|
42
|
|
|
$root = dirname($event->getComposer()->getConfig()->get('vendor-dir')); |
43
|
|
|
|
44
|
|
|
$vendor_name = strtolower(basename($root)); |
45
|
|
|
$partials = explode('-', $vendor_name); |
46
|
|
|
$camel_case_partials = []; |
47
|
|
|
foreach ($partials as $partial) { |
48
|
|
|
$camel_case_partials[] = ucfirst(strtolower($partial)); |
49
|
|
|
} |
50
|
|
|
$camel_case = implode('_', $camel_case_partials); |
51
|
|
|
$snake_case = implode('_', $partials); |
52
|
|
|
|
53
|
|
|
$files = [ |
54
|
|
|
'/wpb.php', |
55
|
|
|
'/includes/class-wpb-activator.php', |
56
|
|
|
'/includes/class-wpb-deactivator.php', |
57
|
|
|
'/includes/class-wpb-i18n.php', |
58
|
|
|
'/includes/class-wpb-loader.php', |
59
|
|
|
'/includes/class-wpb.php', |
60
|
|
|
'/admin/class-wpb-admin.php', |
61
|
|
|
'/admin/class-wpb-admin-menu.php', |
62
|
|
|
'/admin/class-wpb-admin-submenu.php', |
63
|
|
|
'/admin/partials/wpb-admin-display.php', |
64
|
|
|
'/admin/css/wpb-admin.css', |
65
|
|
|
'/admin/js/wpb-admin.js', |
66
|
|
|
'/public/class-wpb-public.php', |
67
|
|
|
'/public/partials/wpb-public-display.php', |
68
|
|
|
'/public/css/wpb-public.css', |
69
|
|
|
'/public/js/wpb-public.js', |
70
|
|
|
'/resources/js/admin/main.js', |
71
|
|
|
'/resources/js/frontend/main.js', |
72
|
|
|
'/resources/js/spa/main.js', |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
foreach ($files as $file) { |
76
|
|
|
$file = $root.$file; |
77
|
|
|
if(file_exists($file)) { |
78
|
|
|
$contents = file_get_contents($file); |
79
|
|
|
$contents = str_replace('wpb_', $snake_case.'_', $contents); |
80
|
|
|
$contents = str_replace('wpb', $vendor_name, $contents); |
81
|
|
|
$contents = str_replace('WPB_APP_ROOT', strtoupper($camel_case).'_APP_ROOT', $contents); |
82
|
|
|
$contents = str_replace('WPB_FILE', strtoupper($camel_case).'_FILE', $contents); |
83
|
|
|
$contents = str_replace('WPB_PATH', strtoupper($camel_case).'_PATH', $contents); |
84
|
|
|
$contents = str_replace('WPB_INCLUDES', strtoupper($camel_case).'_INCLUDES', $contents); |
85
|
|
|
$contents = str_replace('WPB_URL', strtoupper($camel_case).'_URL', $contents); |
86
|
|
|
$contents = str_replace('WPB_ASSETS', strtoupper($camel_case).'_ASSETS', $contents); |
87
|
|
|
$contents = str_replace('WPB_VERSION', strtoupper($camel_case).'_VERSION', $contents); |
88
|
|
|
$contents = str_replace('WPB', $camel_case, $contents); |
89
|
|
|
file_put_contents( |
90
|
|
|
$file, |
91
|
|
|
$contents |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$dir = dirname($file); |
95
|
|
|
$fileName = basename($file); |
96
|
|
|
$newFileName = str_replace('wpb', $vendor_name, $fileName); |
97
|
|
|
|
98
|
|
|
if($fileName != $newFileName) { |
99
|
|
|
rename($file, $dir.'/'.$newFileName); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->configApp($root, $camel_case); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function configApp($root, $camel_case) |
109
|
|
|
{ |
110
|
|
|
$file = $root.'/bootstrap/app.php'; |
111
|
|
|
if(file_exists($file)) { |
112
|
|
|
$contents = file_get_contents($file); |
113
|
|
|
$contents = str_replace('WPB_APP_ROOT', strtoupper($camel_case).'_APP_ROOT', $contents); |
114
|
|
|
file_put_contents( |
115
|
|
|
$file, |
116
|
|
|
$contents |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.