UpgradeShell.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. protected $_map = array(
  12. 'Controller' => 'Controller',
  13. 'Component' => 'Controller/Component',
  14. 'Model' => 'Model',
  15. 'Behavior' => 'Model/Behavior',
  16. 'Datasource' => 'Model/Datasource',
  17. 'Dbo' => 'Model/Datasource/Database',
  18. 'View' => 'View',
  19. 'Helper' => 'View/Helper',
  20. 'Shell' => 'Console/Command',
  21. 'Task' => 'Console/Command/Task',
  22. 'Case' => 'tests/Case',
  23. 'Fixture' => 'tests/Fixture',
  24. 'Error' => 'Lib/Error',
  25. );
  26. /**
  27. * Shell startup, prints info message about dry run.
  28. *
  29. * @return void
  30. */
  31. public function startup() {
  32. parent::startup();
  33. if ($this->params['dry-run']) {
  34. $this->out('<warning>Dry-run mode enabled!</warning>', 1, Shell::QUIET);
  35. }
  36. if ($this->params['git'] && !is_dir('.git')) {
  37. $this->out('<warning>No git repository detected!</warning>', 1, Shell::QUIET);
  38. }
  39. }
  40. /**
  41. * Run all upgrade steps one at a time
  42. *
  43. * @access public
  44. * @return void
  45. */
  46. public function all() {
  47. foreach($this->OptionParser->subcommands() as $command) {
  48. $name = $command->name();
  49. if ($name === 'all') {
  50. continue;
  51. }
  52. $this->out('Running ' . $name);
  53. $this->$name();
  54. }
  55. }
  56. /**
  57. * Move files and folders to their new homes
  58. *
  59. * Moves folders containing files which cannot necessarily be autodetected (libs and templates)
  60. * and then looks for all php files except vendors, and moves them to where Cake 2.0 expects
  61. * to find them.
  62. *
  63. * @access public
  64. * @return void
  65. */
  66. public function locations() {
  67. $cwd = getcwd();
  68. if (is_dir('plugins')) {
  69. $Folder = new Folder('plugins');
  70. list($plugins) = $Folder->read();
  71. foreach($plugins as $plugin) {
  72. chdir($cwd . DS . 'plugins' . DS . $plugin);
  73. $this->locations();
  74. }
  75. $this->_files = array();
  76. chdir($cwd);
  77. }
  78. $moves = array(
  79. 'libs' => 'Lib',
  80. 'vendors' . DS . 'shells' . DS . 'templates' => 'Console' . DS . 'Templates',
  81. );
  82. foreach($moves as $old => $new) {
  83. if (is_dir($old)) {
  84. $this->out("Moving $old to $new");
  85. if (!$this->params['dry-run']) {
  86. $Folder = new Folder($old);
  87. $Folder->move($new);
  88. }
  89. if ($this->params['git']) {
  90. exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($new));
  91. }
  92. }
  93. }
  94. $sourceDirs = array(
  95. '.' => array('recursive' => false),
  96. 'Console',
  97. 'Controller',
  98. 'controllers',
  99. 'Lib' => array('checkFolder' => false),
  100. 'Model',
  101. 'models',
  102. 'tests',
  103. 'View',
  104. 'views',
  105. 'vendors/shells',
  106. );
  107. $defaultOptions = array(
  108. 'recursive' => true,
  109. 'checkFolder' => true,
  110. );
  111. foreach($sourceDirs as $dir => $options) {
  112. if (is_numeric($dir)) {
  113. $dir = $options;
  114. $options = array();
  115. }
  116. $options = array_merge($defaultOptions, $options);
  117. $this->_movePhpFiles($dir, $options);
  118. }
  119. }
  120. /**
  121. * Update helpers.
  122. *
  123. * - Converts helpers usage to new format.
  124. *
  125. * @return void
  126. */
  127. public function helpers() {
  128. $this->_paths = array_diff(App::path('views'), App::core('views'));
  129. if (!empty($this->params['plugin'])) {
  130. $this->_paths = array(App::pluginPath($this->params['plugin']) . 'views' . DS);
  131. }
  132. $patterns = array();
  133. $helpers = App::objects('helper');
  134. $plugins = App::objects('plugin');
  135. $pluginHelpers = array();
  136. foreach ($plugins as $plugin) {
  137. $pluginHelpers = array_merge(
  138. $pluginHelpers,
  139. App::objects('helper', App::pluginPath($plugin) . DS . 'views' . DS . 'helpers' . DS, false)
  140. );
  141. }
  142. $helpers = array_merge($pluginHelpers, $helpers);
  143. foreach ($helpers as $helper) {
  144. $oldHelper = strtolower(substr($helper, 0, 1)).substr($helper, 1);
  145. $patterns[] = array(
  146. "\${$oldHelper} to \$this->{$helper}",
  147. "/\\\${$oldHelper}->/",
  148. "\\\$this->{$helper}->"
  149. );
  150. }
  151. $this->_filesRegexpUpdate($patterns);
  152. }
  153. /**
  154. * Update i18n.
  155. *
  156. * - Removes extra true param.
  157. * - Add the echo to __*() calls that didn't need them before.
  158. *
  159. * @return void
  160. */
  161. public function i18n() {
  162. $this->_paths = array(
  163. APP
  164. );
  165. if (!empty($this->params['plugin'])) {
  166. $this->_paths = array(App::pluginPath($this->params['plugin']));
  167. }
  168. $patterns = array(
  169. array(
  170. '<?php __*(*) to <?php echo __*(*)',
  171. '/<\?php\s*(__[a-z]*\(.*?\))/',
  172. '<?php echo \1'
  173. ),
  174. array(
  175. '<?php __*(*, true) to <?php echo __*()',
  176. '/<\?php\s*(__[a-z]*\(.*?)(,\s*true)(\))/',
  177. '<?php echo \1\3'
  178. ),
  179. array('__*(*, true) to __*(*)', '/(__[a-z]*\(.*?)(,\s*true)(\))/', '\1\3')
  180. );
  181. $this->_filesRegexpUpdate($patterns);
  182. }
  183. /**
  184. * Upgrade the removed basics functions.
  185. *
  186. * - a(*) -> array(*)
  187. * - e(*) -> echo *
  188. * - ife(*, *, *) -> empty(*) ? * : *
  189. * - a(*) -> array(*)
  190. * - r(*, *, *) -> str_replace(*, *, *)
  191. * - up(*) -> strtoupper(*)
  192. * - low(*, *, *) -> strtolower(*)
  193. * - getMicrotime() -> microtime(true)
  194. *
  195. * @return void
  196. */
  197. public function basics() {
  198. $this->_paths = array(
  199. APP
  200. );
  201. if (!empty($this->params['plugin'])) {
  202. $this->_paths = array(App::pluginPath($this->params['plugin']));
  203. }
  204. $patterns = array(
  205. array(
  206. 'a(*) -> array(*)',
  207. '/\ba\((.*)\)/',
  208. 'array(\1)'
  209. ),
  210. array(
  211. 'e(*) -> echo *',
  212. '/\be\((.*)\)/',
  213. 'echo \1'
  214. ),
  215. array(
  216. 'ife(*, *, *) -> empty(*) ? * : *',
  217. '/ife\((.*), (.*), (.*)\)/',
  218. 'empty(\1) ? \2 : \3'
  219. ),
  220. array(
  221. 'r(*, *, *) -> str_replace(*, *, *)',
  222. '/\br\(/',
  223. 'str_replace('
  224. ),
  225. array(
  226. 'up(*) -> strtoupper(*)',
  227. '/\bup\(/',
  228. 'strtoupper('
  229. ),
  230. array(
  231. 'low(*) -> strtolower(*)',
  232. '/\blow\(/',
  233. 'strtolower('
  234. ),
  235. array(
  236. 'getMicrotime() -> microtime(true)',
  237. '/getMicrotime\(\)/',
  238. 'microtime(true)'
  239. ),
  240. );
  241. $this->_filesRegexpUpdate($patterns);
  242. }
  243. /**
  244. * Update the properties moved to CakeRequest.
  245. *
  246. * @return void
  247. */
  248. public function request() {
  249. $views = array_diff(App::path('views'), App::core('views'));
  250. $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
  251. $components = array_diff(App::path('components'), App::core('components'));
  252. $this->_paths = array_merge($views, $controllers, $components);
  253. if (!empty($this->params['plugin'])) {
  254. $pluginPath = App::pluginPath($this->params['plugin']);
  255. $this->_paths = array(
  256. $pluginPath . 'controllers' . DS,
  257. $pluginPath . 'controllers' . DS . 'components' .DS,
  258. $pluginPath . 'views' . DS,
  259. );
  260. }
  261. $patterns = array(
  262. array(
  263. '$this->data -> $this->request->data',
  264. '/(\$this->data\b(?!\())/',
  265. '$this->request->data'
  266. ),
  267. array(
  268. '$this->params -> $this->request->params',
  269. '/(\$this->params\b(?!\())/',
  270. '$this->request->params'
  271. ),
  272. array(
  273. '$this->webroot -> $this->request->webroot',
  274. '/(\$this->webroot\b(?!\())/',
  275. '$this->request->webroot'
  276. ),
  277. array(
  278. '$this->base -> $this->request->base',
  279. '/(\$this->base\b(?!\())/',
  280. '$this->request->base'
  281. ),
  282. array(
  283. '$this->here -> $this->request->here',
  284. '/(\$this->here\b(?!\())/',
  285. '$this->request->here'
  286. ),
  287. array(
  288. '$this->action -> $this->request->action',
  289. '/(\$this->action\b(?!\())/',
  290. '$this->request->action'
  291. ),
  292. );
  293. $this->_filesRegexpUpdate($patterns);
  294. }
  295. /**
  296. * Update Configure::read() calls with no params.
  297. *
  298. * @return void
  299. */
  300. public function configure() {
  301. $this->_paths = array(
  302. APP
  303. );
  304. if (!empty($this->params['plugin'])) {
  305. $this->_paths = array(App::pluginPath($this->params['plugin']));
  306. }
  307. $patterns = array(
  308. array(
  309. "Configure::read() -> Configure::read('debug')",
  310. '/Configure::read\(\)/',
  311. 'Configure::read(\'debug\')'
  312. ),
  313. );
  314. $this->_filesRegexpUpdate($patterns);
  315. }
  316. /**
  317. * constants
  318. *
  319. * @access public
  320. * @return void
  321. */
  322. public function constants() {
  323. $this->_paths = array(
  324. APP
  325. );
  326. if (!empty($this->params['plugin'])) {
  327. $this->_paths = array(App::pluginPath($this->params['plugin']));
  328. }
  329. $patterns = array(
  330. array(
  331. "LIBS -> CAKE",
  332. '/\bLIBS\b/',
  333. 'CAKE'
  334. ),
  335. array(
  336. "CONFIGS -> APP . 'Config' . DS",
  337. '/\bCONFIGS\b/',
  338. 'APP . \'Config\' . DS'
  339. ),
  340. array(
  341. "CONTROLLERS -> APP . 'Controller' . DS",
  342. '/\bCONTROLLERS\b/',
  343. 'APP . \'Controller\' . DS'
  344. ),
  345. array(
  346. "COMPONENTS -> APP . 'Controller' . DS . 'Component' . DS",
  347. '/\bCOMPONENTS\b/',
  348. 'APP . \'Controller\' . DS . \'Component\''
  349. ),
  350. array(
  351. "MODELS -> APP . 'Model' . DS",
  352. '/\bMODELS\b/',
  353. 'APP . \'Model\' . DS'
  354. ),
  355. array(
  356. "BEHAVIORS -> APP . 'Model' . DS . 'Behavior' . DS",
  357. '/\bBEHAVIORS\b/',
  358. 'APP . \'Model\' . DS . \'Behavior\' . DS'
  359. ),
  360. array(
  361. "VIEWS -> APP . 'View' . DS",
  362. '/\bVIEWS\b/',
  363. 'APP . \'View\' . DS'
  364. ),
  365. array(
  366. "HELPERS -> APP . 'View' . DS . 'Helper' . DS",
  367. '/\bHELPERS\b/',
  368. 'APP . \'View\' . DS . \'Helper\' . DS'
  369. ),
  370. array(
  371. "LAYOUTS -> APP . 'View' . DS . 'Layouts' . DS",
  372. '/\bLAYOUTS\b/',
  373. 'APP . \'View\' . DS . \'Layouts\' . DS'
  374. ),
  375. array(
  376. "ELEMENTS -> APP . 'View' . DS . 'Elements' . DS",
  377. '/\bELEMENTS\b/',
  378. 'APP . \'View\' . DS . \'Elements\' . DS'
  379. ),
  380. array(
  381. "CONSOLE_LIBS -> CAKE . 'Console' . DS",
  382. '/\bCONSOLE_LIBS\b/',
  383. 'CAKE . \'Console\' . DS'
  384. ),
  385. array(
  386. "CAKE_TESTS_LIB -> CAKE . 'TestSuite' . DS",
  387. '/\bCAKE_TESTS_LIB\b/',
  388. 'CAKE . \'TestSuite\' . DS'
  389. ),
  390. array(
  391. "CAKE_TESTS -> CAKE . 'Test' . DS",
  392. '/\bCAKE_TESTS\b/',
  393. 'CAKE . \'Test\' . DS'
  394. )
  395. );
  396. $this->_filesRegexpUpdate($patterns);
  397. }
  398. /**
  399. * Move application php files to where they now should be
  400. *
  401. * Find all php files in the folder (honoring recursive) and determine where cake expects the file to be
  402. * If the file is not exactly where cake expects it - move it.
  403. *
  404. * @param mixed $path
  405. * @param mixed $options array(recursive, checkFolder)
  406. * @access protected
  407. * @return void
  408. */
  409. protected function _movePhpFiles($path, $options) {
  410. if (!is_dir($path)) {
  411. return;
  412. }
  413. $paths = $this->_paths;
  414. $this->_paths = array($path);
  415. $this->_files = array();
  416. if ($options['recursive']) {
  417. $this->_findFiles('php');
  418. } else {
  419. $this->_files = scandir($path);
  420. foreach($this->_files as $i => $file) {
  421. if (strlen($file) < 5 || substr($file, -4) !== '.php') {
  422. unset($this->_files[$i]);
  423. }
  424. }
  425. }
  426. $cwd = getcwd();
  427. foreach ($this->_files as &$file) {
  428. $file = $cwd . DS . $file;
  429. $contents = file_get_contents($file);
  430. preg_match('@class (\S*) .*{@', $contents, $match);
  431. if (!$match) {
  432. continue;
  433. }
  434. $class = $match[1];
  435. if (substr($class, 0, 3) === 'Dbo') {
  436. $type = 'Dbo';
  437. } else {
  438. preg_match('@([A-Z][^A-Z]*)$@', $class, $match);
  439. if ($match) {
  440. $type = $match[1];
  441. } else {
  442. $type = 'unknown';
  443. }
  444. }
  445. preg_match('@^.*[\\\/]plugins[\\\/](.*?)[\\\/]@', $file, $match);
  446. $base = $cwd . DS;
  447. $plugin = false;
  448. if ($match) {
  449. $base = $match[0];
  450. $plugin = $match[1];
  451. }
  452. if ($options['checkFolder'] && !empty($this->_map[$type])) {
  453. $folder = str_replace('/', DS, $this->_map[$type]);
  454. $new = $base . $folder . DS . $class . '.php';
  455. } else {
  456. $new = dirname($file) . DS . $class . '.php';
  457. }
  458. if ($file === $new) {
  459. continue;
  460. }
  461. $dir = dirname($new);
  462. if (!is_dir($dir)) {
  463. new Folder($dir, true);
  464. }
  465. $this->out('Moving ' . $file . ' to ' . $new, 1, Shell::VERBOSE);
  466. if (!$this->params['dry-run']) {
  467. if ($this->params['git']) {
  468. exec('git mv -f ' . escapeshellarg($file) . ' ' . escapeshellarg($new));
  469. } else {
  470. rename($file, $new);
  471. }
  472. }
  473. }
  474. $this->_paths = $paths;
  475. }
  476. /**
  477. * Updates files based on regular expressions.
  478. *
  479. * @param array $patterns Array of search and replacement patterns.
  480. * @return void
  481. */
  482. protected function _filesRegexpUpdate($patterns) {
  483. $this->_findFiles($this->params['ext']);
  484. foreach ($this->_files as $file) {
  485. $this->out('Updating ' . $file . '...', 1, Shell::VERBOSE);
  486. $this->_updateFile($file, $patterns);
  487. }
  488. }
  489. /**
  490. * Searches the paths and finds files based on extension.
  491. *
  492. * @param string $extensions
  493. * @return void
  494. */
  495. protected function _findFiles($extensions = '') {
  496. foreach ($this->_paths as $path) {
  497. if (!is_dir($path)) {
  498. continue;
  499. }
  500. $files = array();
  501. $Iterator = new RegexIterator(
  502. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
  503. '/^.+\.(' . $extensions . ')$/i',
  504. RegexIterator::MATCH
  505. );
  506. foreach ($Iterator as $file) {
  507. if ($file->isFile()) {
  508. $files[] = $file->getPathname();
  509. }
  510. }
  511. $this->_files = array_merge($this->_files, $files);
  512. }
  513. }
  514. /**
  515. * Update a single file.
  516. *
  517. * @param string $file The file to update
  518. * @param array $patterns The replacement patterns to run.
  519. * @return void
  520. */
  521. protected function _updateFile($file, $patterns) {
  522. $contents = file_get_contents($file);
  523. foreach ($patterns as $pattern) {
  524. $this->out(' * Updating ' . $pattern[0], 1, Shell::VERBOSE);
  525. $contents = preg_replace($pattern[1], $pattern[2], $contents);
  526. }
  527. $this->out('Done updating ' . $file, 1);
  528. if (!$this->params['dry-run']) {
  529. file_put_contents($file, $contents);
  530. }
  531. }
  532. /**
  533. * get the option parser
  534. *
  535. * @return ConsoleOptionParser
  536. */
  537. public function getOptionParser() {
  538. $subcommandParser = array(
  539. 'options' => array(
  540. 'plugin' => array(
  541. 'short' => 'p',
  542. 'help' => __('The plugin to update. Only the specified plugin will be updated.'
  543. )),
  544. 'ext' => array(
  545. 'short' => 'e',
  546. 'help' => __('The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
  547. 'default' => 'php|ctp|thtml|inc|tpl'
  548. ),
  549. 'git'=> array(
  550. 'help' => __('use git command for moving files around.'),
  551. 'default' => 0
  552. ),
  553. 'dry-run'=> array(
  554. 'short' => 'd',
  555. 'help' => __('Dry run the update, no files will actually be modified.'),
  556. 'boolean' => true
  557. )
  558. )
  559. );
  560. return parent::getOptionParser()
  561. ->description("A shell to help automate upgrading from CakePHP 1.3 to 2.0. \n" .
  562. "Be sure to have a backup of your application before running these commands.")
  563. ->addSubcommand('all', array(
  564. 'help' => 'Run all upgrade commands.',
  565. 'parser' => $subcommandParser
  566. ))
  567. ->addSubcommand('locations', array(
  568. 'help' => 'Move files and folders to their new homes.',
  569. 'parser' => $subcommandParser
  570. ))
  571. ->addSubcommand('i18n', array(
  572. 'help' => 'Update the i18n translation method calls.',
  573. 'parser' => $subcommandParser
  574. ))
  575. ->addSubcommand('helpers', array(
  576. 'help' => 'Update calls to helpers.',
  577. 'parser' => $subcommandParser
  578. ))
  579. ->addSubcommand('basics', array(
  580. 'help' => 'Update removed basics functions to PHP native functions.',
  581. 'parser' => $subcommandParser
  582. ))
  583. ->addSubcommand('request', array(
  584. 'help' => 'Update removed request access, and replace with $this->request.',
  585. 'parser' => $subcommandParser
  586. ))
  587. ->addSubcommand('configure', array(
  588. 'help' => "Update Configure::read() to Configure::read('debug')",
  589. 'parser' => $subcommandParser
  590. ))
  591. ->addSubcommand('constants', array(
  592. 'help' => "Replace Obsolete constants",
  593. 'parser' => $subcommandParser
  594. ));
  595. }
  596. }