UpgradeShell.php 10 KB

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