UpgradeShell.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * A shell class to help developers upgrade applications to CakePHP 2.0
  4. *
  5. * @package cake.console/shells
  6. */
  7. class UpgradeShell extends Shell {
  8. protected $_files = array();
  9. protected $_paths = array();
  10. /**
  11. * Shell startup, prints info message about dry run.
  12. *
  13. * @return void
  14. */
  15. function startup() {
  16. parent::startup();
  17. if ($this->params['dry-run']) {
  18. $this->out('<warning>Dry-run mode enabled!</warning>', 1, Shell::QUIET);
  19. }
  20. }
  21. /**
  22. * Update helpers.
  23. *
  24. * - Converts helpers usage to new format.
  25. *
  26. * @return void
  27. */
  28. function helpers() {
  29. $this->_paths = array_diff(App::path('views'), App::core('views'));
  30. if (!empty($this->params['plugin'])) {
  31. $this->_paths = array(App::pluginPath($this->params['plugin']) . 'views' . DS);
  32. }
  33. $patterns = array();
  34. $helpers = App::objects('helper');
  35. $plugins = App::objects('plugin');
  36. $pluginHelpers = array();
  37. foreach ($plugins as $plugin) {
  38. $pluginHelpers = array_merge(
  39. $pluginHelpers,
  40. App::objects('helper', App::pluginPath($plugin) . DS . 'views' . DS . 'helpers' . DS, false)
  41. );
  42. }
  43. $helpers = array_merge($pluginHelpers, $helpers);
  44. foreach ($helpers as $helper) {
  45. $oldHelper = strtolower(substr($helper, 0, 1)).substr($helper, 1);
  46. $patterns[] = array(
  47. "\${$oldHelper} to \$this->{$helper}",
  48. "/\\\${$oldHelper}->/",
  49. "\\\$this->{$helper}->"
  50. );
  51. }
  52. $this->_filesRegexpUpdate($patterns);
  53. }
  54. /**
  55. * Update i18n.
  56. *
  57. * - Removes extra true param.
  58. * - Add the echo to __*() calls that didn't need them before.
  59. *
  60. * @return void
  61. */
  62. function i18n() {
  63. $this->_paths = array(
  64. APP
  65. );
  66. if (!empty($this->params['plugin'])) {
  67. $this->_paths = array(App::pluginPath($this->params['plugin']));
  68. }
  69. $patterns = array(
  70. array(
  71. '<?php __*(*) to <?php echo __*(*)',
  72. '/<\?php\s*(__[a-z]*\(.*?\))/',
  73. '<?php echo \1'
  74. ),
  75. array(
  76. '<?php __*(*, true) to <?php echo __*()',
  77. '/<\?php\s*(__[a-z]*\(.*?)(,\s*true)(\))/',
  78. '<?php echo \1\3'
  79. ),
  80. array('__*(*, true) to __*(*)', '/(__[a-z]*\(.*?)(,\s*true)(\))/', '\1\3')
  81. );
  82. $this->_filesRegexpUpdate($patterns);
  83. }
  84. /**
  85. * Upgrade the removed basics functions.
  86. *
  87. * - a(*) -> array(*)
  88. * - e(*) -> echo *
  89. * - ife(*, *, *) -> empty(*) ? * : *
  90. * - a(*) -> array(*)
  91. * - r(*, *, *) -> str_replace(*, *, *)
  92. * - up(*) -> strtoupper(*)
  93. * - low(*, *, *) -> strtolower(*)
  94. * - getMicrotime() -> microtime(true)
  95. *
  96. * @return void
  97. */
  98. public function basics() {
  99. $this->_paths = array(
  100. APP
  101. );
  102. if (!empty($this->params['plugin'])) {
  103. $this->_paths = array(App::pluginPath($this->params['plugin']));
  104. }
  105. $patterns = array(
  106. array(
  107. 'a(*) -> array(*)',
  108. '/\ba\((.*)\)/',
  109. 'array(\1)'
  110. ),
  111. array(
  112. 'e(*) -> echo *',
  113. '/\be\((.*)\)/',
  114. 'echo \1'
  115. ),
  116. array(
  117. 'ife(*, *, *) -> empty(*) ? * : *',
  118. '/ife\((.*), (.*), (.*)\)/',
  119. 'empty(\1) ? \2 : \3'
  120. ),
  121. array(
  122. 'r(*, *, *) -> str_replace(*, *, *)',
  123. '/\br\(/',
  124. 'str_replace('
  125. ),
  126. array(
  127. 'up(*) -> strtoupper(*)',
  128. '/\bup\(/',
  129. 'strtoupper('
  130. ),
  131. array(
  132. 'low(*) -> strtolower(*)',
  133. '/\blow\(/',
  134. 'strtolower('
  135. ),
  136. array(
  137. 'getMicrotime() -> microtime(true)',
  138. '/getMicrotime\(\)/',
  139. 'microtime(true)'
  140. ),
  141. );
  142. $this->_filesRegexpUpdate($patterns);
  143. }
  144. /**
  145. * Update the properties moved to CakeRequest.
  146. *
  147. * @return void
  148. */
  149. public function request() {
  150. $core = App::core();
  151. $views = array_diff(App::path('views'), App::core('views'));
  152. $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
  153. $components = array_diff(App::path('components'), App::core('components'));
  154. $this->_paths = array_merge($views, $controllers, $components);
  155. if (!empty($this->params['plugin'])) {
  156. $pluginPath = App::pluginPath($this->params['plugin']);
  157. $this->_paths = array(
  158. $pluginPath . 'controllers' . DS,
  159. $pluginPath . 'controllers' . DS . 'components' .DS,
  160. $pluginPath . 'views' . DS,
  161. );
  162. }
  163. $patterns = array(
  164. array(
  165. '$this->data -> $this->request->data',
  166. '/(\$this->data\b(?!\())/',
  167. '$this->request->data'
  168. ),
  169. array(
  170. '$this->params -> $this->request->params',
  171. '/(\$this->params\b(?!\())/',
  172. '$this->request->params'
  173. ),
  174. array(
  175. '$this->webroot -> $this->request->webroot',
  176. '/(\$this->webroot\b(?!\())/',
  177. '$this->request->webroot'
  178. ),
  179. array(
  180. '$this->base -> $this->request->base',
  181. '/(\$this->base\b(?!\())/',
  182. '$this->request->base'
  183. ),
  184. array(
  185. '$this->here -> $this->request->here',
  186. '/(\$this->here\b(?!\())/',
  187. '$this->request->here'
  188. ),
  189. array(
  190. '$this->action -> $this->request->action',
  191. '/(\$this->action\b(?!\())/',
  192. '$this->request->action'
  193. ),
  194. );
  195. $this->_filesRegexpUpdate($patterns);
  196. }
  197. /**
  198. * Update Configure::read() calls with no params.
  199. *
  200. * @return void
  201. */
  202. public function configure() {
  203. $this->_paths = array(
  204. APP
  205. );
  206. if (!empty($this->params['plugin'])) {
  207. $this->_paths = array(App::pluginPath($this->params['plugin']));
  208. }
  209. $patterns = array(
  210. array(
  211. "Configure::read() -> Configure::read('debug')",
  212. '/Configure::read\(\)/',
  213. 'Configure::read(\'debug\')'
  214. ),
  215. );
  216. $this->_filesRegexpUpdate($patterns);
  217. }
  218. /**
  219. * Updates files based on regular expressions.
  220. *
  221. * @param array $patterns Array of search and replacement patterns.
  222. * @return void
  223. */
  224. protected function _filesRegexpUpdate($patterns) {
  225. $this->_findFiles($this->params['ext']);
  226. foreach ($this->_files as $file) {
  227. $this->out('Updating ' . $file . '...', 1, Shell::VERBOSE);
  228. $this->_updateFile($file, $patterns);
  229. }
  230. }
  231. /**
  232. * Searches the paths and finds files based on extension.
  233. *
  234. * @param string $extensions
  235. * @return void
  236. */
  237. protected function _findFiles($extensions = '') {
  238. foreach ($this->_paths as $path) {
  239. if (!is_dir($path)) {
  240. continue;
  241. }
  242. $files = array();
  243. $Iterator = new RegexIterator(
  244. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
  245. '/^.+\.(' . $extensions . ')$/i',
  246. RegexIterator::MATCH
  247. );
  248. foreach ($Iterator as $file) {
  249. if ($file->isFile()) {
  250. $files[] = $file->getPathname();
  251. }
  252. }
  253. $this->_files = array_merge($this->_files, $files);
  254. }
  255. }
  256. /**
  257. * Update a single file.
  258. *
  259. * @param string $file The file to update
  260. * @param array $patterns The replacement patterns to run.
  261. * @return void
  262. */
  263. protected function _updateFile($file, $patterns) {
  264. $contents = file_get_contents($file);
  265. foreach ($patterns as $pattern) {
  266. $this->out(' * Updating ' . $pattern[0], 1, Shell::VERBOSE);
  267. $contents = preg_replace($pattern[1], $pattern[2], $contents);
  268. }
  269. $this->out('Done updating ' . $file, 1);
  270. if (!$this->params['dry-run']) {
  271. file_put_contents($file, $contents);
  272. }
  273. }
  274. /**
  275. * get the option parser
  276. *
  277. * @return ConsoleOptionParser
  278. */
  279. function getOptionParser() {
  280. $subcommandParser = array(
  281. 'options' => array(
  282. 'plugin' => array(
  283. 'short' => 'p',
  284. 'help' => __('The plugin to update. Only the specified plugin will be updated.'
  285. )),
  286. 'ext' => array(
  287. 'short' => 'e',
  288. 'help' => __('The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
  289. 'default' => 'php|ctp|thtml|inc|tpl'
  290. ),
  291. 'dry-run'=> array(
  292. 'short' => 'd',
  293. 'help' => __('Dry run the update, no files will actually be modified.'),
  294. 'boolean' => true
  295. )
  296. )
  297. );
  298. return parent::getOptionParser()
  299. ->description("A shell to help automate upgrading from CakePHP 1.3 to 2.0. \n" .
  300. "Be sure to have a backup of your application before running these commands.")
  301. ->addSubcommand('i18n', array(
  302. 'help' => 'Update the i18n translation method calls.',
  303. 'parser' => $subcommandParser
  304. ))
  305. ->addSubcommand('helpers', array(
  306. 'help' => 'Update calls to helpers.',
  307. 'parser' => $subcommandParser
  308. ))
  309. ->addSubcommand('basics', array(
  310. 'help' => 'Update removed basics functions to PHP native functions.',
  311. 'parser' => $subcommandParser
  312. ))
  313. ->addSubcommand('request', array(
  314. 'help' => 'Update removed request access, and replace with $this->request.',
  315. 'parser' => $subcommandParser
  316. ))
  317. ->addSubcommand('configure', array(
  318. 'help' => "Update Configure::read() to Configure::read('debug')",
  319. 'parser' => $subcommandParser
  320. ));
  321. }
  322. }