UpgradeShell.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. * Update components.
  400. *
  401. * - Make components that extend Object to extend Component.
  402. *
  403. * @return void
  404. */
  405. public function components() {
  406. $this->_paths = App::Path('Controller/Component');
  407. if (!empty($this->params['plugin'])) {
  408. $this->_paths = App::Path('Controller/Component', $this->params['plugin']);
  409. }
  410. $patterns = array(
  411. array(
  412. '*Component extends Object to *Component extends Component',
  413. '/([a-zA-Z]*Component extends) Object/',
  414. '\1 Component'
  415. ),
  416. );
  417. $this->_filesRegexpUpdate($patterns);
  418. }
  419. /**
  420. * Move application php files to where they now should be
  421. *
  422. * Find all php files in the folder (honoring recursive) and determine where cake expects the file to be
  423. * If the file is not exactly where cake expects it - move it.
  424. *
  425. * @param mixed $path
  426. * @param mixed $options array(recursive, checkFolder)
  427. * @access protected
  428. * @return void
  429. */
  430. protected function _movePhpFiles($path, $options) {
  431. if (!is_dir($path)) {
  432. return;
  433. }
  434. $paths = $this->_paths;
  435. $this->_paths = array($path);
  436. $this->_files = array();
  437. if ($options['recursive']) {
  438. $this->_findFiles('php');
  439. } else {
  440. $this->_files = scandir($path);
  441. foreach($this->_files as $i => $file) {
  442. if (strlen($file) < 5 || substr($file, -4) !== '.php') {
  443. unset($this->_files[$i]);
  444. }
  445. }
  446. }
  447. $cwd = getcwd();
  448. foreach ($this->_files as &$file) {
  449. $file = $cwd . DS . $file;
  450. $contents = file_get_contents($file);
  451. preg_match('@class (\S*) .*{@', $contents, $match);
  452. if (!$match) {
  453. continue;
  454. }
  455. $class = $match[1];
  456. if (substr($class, 0, 3) === 'Dbo') {
  457. $type = 'Dbo';
  458. } else {
  459. preg_match('@([A-Z][^A-Z]*)$@', $class, $match);
  460. if ($match) {
  461. $type = $match[1];
  462. } else {
  463. $type = 'unknown';
  464. }
  465. }
  466. preg_match('@^.*[\\\/]plugins[\\\/](.*?)[\\\/]@', $file, $match);
  467. $base = $cwd . DS;
  468. $plugin = false;
  469. if ($match) {
  470. $base = $match[0];
  471. $plugin = $match[1];
  472. }
  473. if ($options['checkFolder'] && !empty($this->_map[$type])) {
  474. $folder = str_replace('/', DS, $this->_map[$type]);
  475. $new = $base . $folder . DS . $class . '.php';
  476. } else {
  477. $new = dirname($file) . DS . $class . '.php';
  478. }
  479. if ($file === $new) {
  480. continue;
  481. }
  482. $dir = dirname($new);
  483. if (!is_dir($dir)) {
  484. new Folder($dir, true);
  485. }
  486. $this->out('Moving ' . $file . ' to ' . $new, 1, Shell::VERBOSE);
  487. if (!$this->params['dry-run']) {
  488. if ($this->params['git']) {
  489. exec('git mv -f ' . escapeshellarg($file) . ' ' . escapeshellarg($new));
  490. } else {
  491. rename($file, $new);
  492. }
  493. }
  494. }
  495. $this->_paths = $paths;
  496. }
  497. /**
  498. * Updates files based on regular expressions.
  499. *
  500. * @param array $patterns Array of search and replacement patterns.
  501. * @return void
  502. */
  503. protected function _filesRegexpUpdate($patterns) {
  504. $this->_findFiles($this->params['ext']);
  505. foreach ($this->_files as $file) {
  506. $this->out('Updating ' . $file . '...', 1, Shell::VERBOSE);
  507. $this->_updateFile($file, $patterns);
  508. }
  509. }
  510. /**
  511. * Searches the paths and finds files based on extension.
  512. *
  513. * @param string $extensions
  514. * @return void
  515. */
  516. protected function _findFiles($extensions = '') {
  517. foreach ($this->_paths as $path) {
  518. if (!is_dir($path)) {
  519. continue;
  520. }
  521. $files = array();
  522. $Iterator = new RegexIterator(
  523. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
  524. '/^.+\.(' . $extensions . ')$/i',
  525. RegexIterator::MATCH
  526. );
  527. foreach ($Iterator as $file) {
  528. if ($file->isFile()) {
  529. $files[] = $file->getPathname();
  530. }
  531. }
  532. $this->_files = array_merge($this->_files, $files);
  533. }
  534. }
  535. /**
  536. * Update a single file.
  537. *
  538. * @param string $file The file to update
  539. * @param array $patterns The replacement patterns to run.
  540. * @return void
  541. */
  542. protected function _updateFile($file, $patterns) {
  543. $contents = file_get_contents($file);
  544. foreach ($patterns as $pattern) {
  545. $this->out(' * Updating ' . $pattern[0], 1, Shell::VERBOSE);
  546. $contents = preg_replace($pattern[1], $pattern[2], $contents);
  547. }
  548. $this->out('Done updating ' . $file, 1);
  549. if (!$this->params['dry-run']) {
  550. file_put_contents($file, $contents);
  551. }
  552. }
  553. /**
  554. * get the option parser
  555. *
  556. * @return ConsoleOptionParser
  557. */
  558. public function getOptionParser() {
  559. $subcommandParser = array(
  560. 'options' => array(
  561. 'plugin' => array(
  562. 'short' => 'p',
  563. 'help' => __('The plugin to update. Only the specified plugin will be updated.'
  564. )),
  565. 'ext' => array(
  566. 'short' => 'e',
  567. 'help' => __('The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
  568. 'default' => 'php|ctp|thtml|inc|tpl'
  569. ),
  570. 'git'=> array(
  571. 'help' => __('use git command for moving files around.'),
  572. 'default' => 0
  573. ),
  574. 'dry-run'=> array(
  575. 'short' => 'd',
  576. 'help' => __('Dry run the update, no files will actually be modified.'),
  577. 'boolean' => true
  578. )
  579. )
  580. );
  581. return parent::getOptionParser()
  582. ->description("A shell to help automate upgrading from CakePHP 1.3 to 2.0. \n" .
  583. "Be sure to have a backup of your application before running these commands.")
  584. ->addSubcommand('all', array(
  585. 'help' => 'Run all upgrade commands.',
  586. 'parser' => $subcommandParser
  587. ))
  588. ->addSubcommand('locations', array(
  589. 'help' => 'Move files and folders to their new homes.',
  590. 'parser' => $subcommandParser
  591. ))
  592. ->addSubcommand('i18n', array(
  593. 'help' => 'Update the i18n translation method calls.',
  594. 'parser' => $subcommandParser
  595. ))
  596. ->addSubcommand('helpers', array(
  597. 'help' => 'Update calls to helpers.',
  598. 'parser' => $subcommandParser
  599. ))
  600. ->addSubcommand('basics', array(
  601. 'help' => 'Update removed basics functions to PHP native functions.',
  602. 'parser' => $subcommandParser
  603. ))
  604. ->addSubcommand('request', array(
  605. 'help' => 'Update removed request access, and replace with $this->request.',
  606. 'parser' => $subcommandParser
  607. ))
  608. ->addSubcommand('configure', array(
  609. 'help' => "Update Configure::read() to Configure::read('debug')",
  610. 'parser' => $subcommandParser
  611. ))
  612. ->addSubcommand('constants', array(
  613. 'help' => "Replace Obsolete constants",
  614. 'parser' => $subcommandParser
  615. ))
  616. ->addSubcommand('components', array(
  617. 'help' => 'Update components to extend Component class.',
  618. 'parser' => $subcommandParser
  619. ));
  620. }
  621. }