-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rename_it.php
102 lines (85 loc) · 2.91 KB
/
rename_it.php
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
<?php
/**
* Rename script for ModExtra
*
* @param string $new_name Name of new components
* @param string $start Directory for find and replace in files old name to new name
*
*/
$new_name = !empty($_REQUEST['name'])
? $_REQUEST['name']
: (!empty($argv[1])
? $argv[1]
: '');
$new_name_lower = strtolower($new_name);
$start = dirname(__FILE__);
if (empty($new_name)) {
exit("\n" . 'You need to specify a new name for the component as first argument, or send it via $_GET["name"].');
}
// --
$old_name = 'ModExtra';
$old_name_lower = strtolower($old_name);
$dirs = scandir($start);
$tmp = explode(DIRECTORY_SEPARATOR, $start);
array_pop($tmp);
$end = implode(DIRECTORY_SEPARATOR, $tmp) . DIRECTORY_SEPARATOR . $new_name;
@rename($start, $end);
rename_extra($end, array($old_name, $old_name_lower), array($new_name, $new_name_lower));
/**
* Recurvice rename of files and its content
*
* @param string $start_path Where to start
* @param array $find Array with values to rename
* @param array $replace Array with values for replacement
*
* @return void
*/
function rename_extra($start_path, $find = array(), $replace = array())
{
$items = scandir($start_path);
foreach ($items as $item) {
if (strpos($item, '.') === 0) {
continue;
}
if (strpos($item, 'rename_it') !== false) {
continue;
}
$old_path = str_replace(
DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
$start_path . DIRECTORY_SEPARATOR . $item
);
if (strpos($old_path, $find[0]) !== false) {
$new_path = str_replace(
DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
$start_path . DIRECTORY_SEPARATOR . str_replace($find, $replace, $item)
);
if (!rename($old_path, $new_path)) {
exit("\nCould not rename $old_path to $new_path");
}
}elseif (strpos($old_path, $find[1]) !== false) {
$new_path = str_replace(
DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
$start_path . DIRECTORY_SEPARATOR . str_replace($find[1], $replace[1], $item)
);
if (!rename($old_path, $new_path)) {
exit("\nCould not rename $old_path to $new_path");
}
}
else {
$new_path = $old_path;
}
if (is_dir($new_path)) {
rename_extra($new_path, $find, $replace);
} else {
$content = file_get_contents($new_path);
$content = str_replace($find, $replace, $content);
if ($item == 'home.class.php') {
$content = str_replace($replace[0] . 'ManagerController', 'modExtraManagerController', $content);
}
file_put_contents($new_path, $content);
}
}
}