UpgradeShell.php 9.5 KB

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