UpgradeShell.php 10 KB

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