/
upgrade.php
142 lines (129 loc) · 3.11 KB
/
upgrade.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
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
<?php
/**
* A shell class to help developers upgrade applications to CakePHP 2.0
*
* @package cake.console/shells
*/
class UpgradeShell extends Shell {
protected $_files = array();
protected $_paths = array();
/**
* Update helpers.
*
* - Converts helpers usage to new format.
*
* @return void
*/
function helpers() {
if (!empty($this->params['plugin'])) {
$this->_paths = array(App::pluginPath($this->params['plugin']));
} else {
$this->_paths = array(
VIEWS
);
}
$patterns = array();
foreach(App::objects('helper') as $helper) {
$oldHelper = strtolower(substr($helper, 0, 1)).substr($helper, 1);
$patterns[] = array(
"\${$oldHelper} to \$this->{$helper}",
"/\\\${$oldHelper}->/",
"\\\$this->{$helper}->"
);
}
$this->_findFiles();
foreach ($this->_files as $file) {
$this->out('Updating ' . $file . '...', 1, Shell::VERBOSE);
$this->_updateFile($file, $patterns);
}
}
/**
* Update i18n.
*
* - Removes extra true param.
*
* @return void
*/
function i18n() {
if (!empty($this->params['plugin'])) {
$this->_paths = array(App::pluginPath($this->params['plugin']));
} else {
$this->_paths = array(
CONTROLLERS,
MODELS,
VIEWS
);
}
$patterns = array(
array(
'<?php __*(*) to <?php echo __*(*)',
'/<\?php\s*(__[a-z]*\(.*?\))/',
'<?php echo \1'
),
array(
'<?php __*(*, true) to <?php echo __*()',
'/<\?php\s*(__[a-z]*\(.*?)(,\s*true)(\))/',
'<?php echo \1\3'
),
array('__*(*, true) to __*(*)', '/(__[a-z]*\(.*?)(,\s*true)(\))/', '\1\3')
);
$this->_findFiles();
foreach ($this->_files as $file) {
$this->out('Updating ' . $file . '...', 1, Shell::VERBOSE);
$this->_updateFile($file, $patterns);
}
}
protected function _findFiles($pattern = '') {
foreach ($this->_paths as $path) {
$Folder = new Folder($path);
$files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
if (!empty($pattern)) {
foreach ($files as $i => $file) {
if (preg_match($pattern, $file)) {
unset($files[$i]);
}
}
$files = array_values($files);
}
$this->_files = array_merge($this->_files, $files);
}
}
/**
* Update a single file.
*
* @return void
*/
protected function _updateFile($file, $patterns) {
$contents = file_get_contents($file);
foreach ($patterns as $pattern) {
$this->out(' * Updating ' . $pattern[0], 1, Shell::VERBOSE);
$contents = preg_replace($pattern[1], $pattern[2], $contents);
}
$this->out('Done updating ' . $file, 1);
file_put_contents($file, $contents);
}
/**
* get the option parser
*
* @return void
*/
function getOptionParser() {
return parent::getOptionParser()
->addSubcommand('i18n', array(
'help' => 'Update the i18n translation method calls.',
'parser' => array(
'options' => array(
'plugin' => array('short' => 'p', 'help' => __('The plugin to update.'))
)
)
))
->addSubcommand('helpers', array(
'help' => 'Update calls to helpers.',
'parser' => array(
'options' => array(
'plugin' => array('short' => 'p', 'help' => __('The plugin to update.'))
)
)
));
}
}