UpgradeShell.php 8.5 KB

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