-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.php
More file actions
executable file
·149 lines (125 loc) · 6.31 KB
/
migrate.php
File metadata and controls
executable file
·149 lines (125 loc) · 6.31 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
<?php declare(strict_types=1);
/**
* Class Migrate
* Get some automatization into PHP7 Migration
*/
class Migrate {
/**
* @var array
*/
private $allFiles = [];
/**
* @var string
*/
private $projectDir = '';
/**
* Migrate constructor.
* @param $projectDir
*/
public function __construct($projectDir)
{
$this->projectDir = $projectDir;
}
/**
*
*/
public function processMigration() : void
{
$this->getFiles();
foreach($this->allFiles as $filepath) {
/**
* @var SplFileInfo $filepath
*/
$filecontent = file_get_contents($filepath->getPathname());
$rocnfilecontent = $this->oldClassNames($filecontent);
$mysqlifcontent = $this->oldMySQLDriver($rocnfilecontent, $filepath->getPathname());
$finalcontent = $mysqlifcontent;
if ($finalcontent != $filecontent) {
file_put_contents($filepath->getPathname(), $finalcontent);
}
}
}
/**
*
*/
private function getFiles() : void
{
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->projectDir));
$this->allFiles = array_filter(iterator_to_array($iterator), function($file) {
return $file->isFile() && $file->getExtension() == 'php';
});
}
/**
* @param string $filecontent
* @return array
*/
private function getClassNames($filecontent='') : array
{
$classnames = [];
preg_match_all('/^\s?(abstract\s+)?class (\w+)/m', $filecontent, $matches, PREG_SET_ORDER);
foreach($matches as $match) {
$classnames[] = $match[2];
}
return $classnames;
}
/**
* @param $filecontent
* @return string
*/
private function oldClassNames($filecontent) : string
{
$classnames = $this->getClassNames($filecontent);
if (empty($classnames)) {
return $filecontent;
}
if (!strpos($filecontent, 'function '.$classnames[0])) {
return $filecontent;
}
foreach($classnames as $classname) {
$filecontent = str_replace(
'function '.$classname,
'public function __construct',
$filecontent
);
}
return $filecontent;
}
/**
* @param $filecontent
* @return string
*/
private function oldMySQLDriver($filecontent, $filepath) : string
{
$filecontent = str_replace('mysql_affected_rows', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbAffectedRows', $filecontent);
$filecontent = str_replace('mysql_close', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbClose', $filecontent);
$filecontent = str_replace('mysql_data_seek', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbDataSeek', $filecontent);
$filecontent = str_replace('mysql_errno', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbErrno', $filecontent);
$filecontent = str_replace('mysql_error', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbError', $filecontent);
$filecontent = str_replace('mysql_fetch_array', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbFetchArray', $filecontent);
$filecontent = str_replace('mysql_fetch_assoc', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbFetchAssoc', $filecontent);
$filecontent = str_replace('mysql_fetch_lengths', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbFetchLengths', $filecontent);
$filecontent = str_replace('mysql_fetch_object', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbFetchObject', $filecontent);
$filecontent = str_replace('mysql_fetch_row', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbFetchRow', $filecontent);
$filecontent = str_replace('mysql_field_seek', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbFieldSeek', $filecontent);
$filecontent = str_replace('mysql_free_result', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbFreeResult', $filecontent);
$filecontent = str_replace('mysql_get_client_info', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbGetClientInfo', $filecontent);
$filecontent = str_replace('mysql_get_host_info', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbGetHostInfo', $filecontent);
$filecontent = str_replace('mysql_get_proto_info', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbGetProtoInfo', $filecontent);
$filecontent = str_replace('mysql_get_server_info', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbGetServerInfo', $filecontent);
$filecontent = str_replace('mysql_info', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbInfo', $filecontent);
$filecontent = str_replace('mysql_insert_id', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbInsertId', $filecontent);
$filecontent = str_replace('mysql_num_rows', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbNumRows', $filecontent);
$filecontent = str_replace('mysql_ping', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbPing', $filecontent);
$filecontent = str_replace('mysql_query', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbQuery', $filecontent);
$filecontent = str_replace('mysql_escape_string', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbRealEscapeStr', $filecontent);
$filecontent = str_replace('mysql_real_escape_string', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbRealEscapeStr', $filecontent);
$filecontent = str_replace('mysql_select_db', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbSelectDb', $filecontent);
$filecontent = str_replace('mysql_set_charset', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbSetCharset', $filecontent);
$filecontent = str_replace('mysql_stat', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbStat', $filecontent);
$filecontent = str_replace('mysql_thread_id', '\Debugteam\MySQLiWrapper\MySQLiBase::getInstance()->dbThreadId', $filecontent);
return $filecontent;
}
}
$projectdir = '/var/www/projects/assessmentworks/application/';
$migrate = new Migrate($projectdir);
$migrate->processMigration();