UpgradeShell.php 16 KB

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