UpgradeShell.php 13 KB

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