UpgradeShell.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. <?php
  2. /**
  3. * Upgrade Shell
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Console.Command
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Folder', 'Utility');
  20. /**
  21. * A shell class to help developers upgrade applications to CakePHP 2.0
  22. *
  23. * @package Cake.Console.Command
  24. */
  25. class UpgradeShell extends Shell {
  26. /**
  27. * Files
  28. *
  29. * @var array
  30. */
  31. protected $_files = array();
  32. /**
  33. * Paths
  34. *
  35. * @var array
  36. */
  37. protected $_paths = array();
  38. /**
  39. * Map
  40. *
  41. * @var array
  42. */
  43. protected $_map = array(
  44. 'Controller' => 'Controller',
  45. 'Component' => 'Controller/Component',
  46. 'Model' => 'Model',
  47. 'Behavior' => 'Model/Behavior',
  48. 'Datasource' => 'Model/Datasource',
  49. 'Dbo' => 'Model/Datasource/Database',
  50. 'View' => 'View',
  51. 'Helper' => 'View/Helper',
  52. 'Shell' => 'Console/Command',
  53. 'Task' => 'Console/Command/Task',
  54. 'Case' => 'Test/Case',
  55. 'Fixture' => 'Test/Fixture',
  56. 'Error' => 'Lib/Error',
  57. );
  58. /**
  59. * Shell startup, prints info message about dry run.
  60. *
  61. * @return void
  62. */
  63. public function startup() {
  64. parent::startup();
  65. if ($this->params['dry-run']) {
  66. $this->out(__d('cake_console', '<warning>Dry-run mode enabled!</warning>'), 1, Shell::QUIET);
  67. }
  68. if ($this->params['git'] && !is_dir('.git')) {
  69. $this->out(__d('cake_console', '<warning>No git repository detected!</warning>'), 1, Shell::QUIET);
  70. }
  71. }
  72. /**
  73. * Run all upgrade steps one at a time
  74. *
  75. * @return void
  76. */
  77. public function all() {
  78. foreach($this->OptionParser->subcommands() as $command) {
  79. $name = $command->name();
  80. if ($name === 'all') {
  81. continue;
  82. }
  83. $this->out(__d('cake_console', 'Running %s', $name));
  84. $this->$name();
  85. }
  86. }
  87. /**
  88. * Update tests.
  89. *
  90. * - Update tests class names to FooTest rather than FooTestCase.
  91. *
  92. * @return void
  93. */
  94. public function tests() {
  95. $this->_paths = array(APP . 'tests' . DS);
  96. if (!empty($this->params['plugin'])) {
  97. $this->_paths = array(App::pluginPath($this->params['plugin']) . 'tests' . DS);
  98. }
  99. $patterns = array(
  100. array(
  101. '*TestCase extends CakeTestCase to *Test extends CakeTestCase',
  102. '/([a-zA-Z]*Test)Case extends CakeTestCase/',
  103. '\1 extends CakeTestCase'
  104. ),
  105. );
  106. $this->_filesRegexpUpdate($patterns);
  107. }
  108. /**
  109. * Move files and folders to their new homes
  110. *
  111. * Moves folders containing files which cannot necessarily be auto-detected (libs and templates)
  112. * and then looks for all php files except vendors, and moves them to where Cake 2.0 expects
  113. * to find them.
  114. *
  115. * @return void
  116. */
  117. public function locations() {
  118. $cwd = getcwd();
  119. if (!empty($this->params['plugin'])) {
  120. chdir(App::pluginPath($this->params['plugin']));
  121. }
  122. if (is_dir('plugins')) {
  123. $Folder = new Folder('plugins');
  124. list($plugins) = $Folder->read();
  125. foreach($plugins as $plugin) {
  126. chdir($cwd . DS . 'plugins' . DS . $plugin);
  127. $this->locations();
  128. }
  129. $this->_files = array();
  130. chdir($cwd);
  131. }
  132. $moves = array(
  133. 'config' => 'Config',
  134. 'Config' . DS . 'schema' => 'Config' . DS . 'Schema',
  135. 'libs' => 'Lib',
  136. 'tests' => 'Test',
  137. 'views' => 'View',
  138. 'models' => 'Model',
  139. 'Model' . DS . 'behaviors' => 'Model' . DS . 'Behavior',
  140. 'Model' . DS . 'datasources' => 'Model' . DS . 'Datasource',
  141. 'Test' . DS . 'cases' => 'Test' . DS . 'Case',
  142. 'Test' . DS . 'fixtures' => 'Test' . DS . 'Fixture',
  143. 'vendors' . DS . 'shells' . DS . 'templates' => 'Console' . DS . 'Templates',
  144. );
  145. foreach($moves as $old => $new) {
  146. if (is_dir($old)) {
  147. $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
  148. if (!$this->params['dry-run']) {
  149. if ($this->params['git']) {
  150. exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
  151. exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
  152. } else {
  153. $Folder = new Folder($old);
  154. $Folder->move($new);
  155. }
  156. }
  157. }
  158. }
  159. $this->_moveViewFiles();
  160. $sourceDirs = array(
  161. '.' => array('recursive' => false),
  162. 'Console',
  163. 'controllers',
  164. 'Controller',
  165. 'Lib' => array('checkFolder' => false),
  166. 'models',
  167. 'Model',
  168. 'tests',
  169. 'Test' => array('regex' => '@class (\S*Test) extends CakeTestCase@'),
  170. 'views',
  171. 'View',
  172. 'vendors/shells',
  173. );
  174. $defaultOptions = array(
  175. 'recursive' => true,
  176. 'checkFolder' => true,
  177. 'regex' => '@class (\S*) .*{@i'
  178. );
  179. foreach($sourceDirs as $dir => $options) {
  180. if (is_numeric($dir)) {
  181. $dir = $options;
  182. $options = array();
  183. }
  184. $options = array_merge($defaultOptions, $options);
  185. $this->_movePhpFiles($dir, $options);
  186. }
  187. }
  188. /**
  189. * Update helpers.
  190. *
  191. * - Converts helpers usage to new format.
  192. *
  193. * @return void
  194. */
  195. public function helpers() {
  196. $this->_paths = array_diff(App::path('views'), App::core('views'));
  197. if (!empty($this->params['plugin'])) {
  198. $this->_paths = array(App::pluginPath($this->params['plugin']) . 'views' . DS);
  199. }
  200. $patterns = array();
  201. App::build(array(
  202. 'View/Helper' => App::core('View/Helper'),
  203. ), App::APPEND);
  204. $helpers = App::objects('helper');
  205. $plugins = App::objects('plugin');
  206. $pluginHelpers = array();
  207. foreach ($plugins as $plugin) {
  208. $pluginHelpers = array_merge(
  209. $pluginHelpers,
  210. App::objects('helper', App::pluginPath($plugin) . DS . 'views' . DS . 'helpers' . DS, false)
  211. );
  212. }
  213. $helpers = array_merge($pluginHelpers, $helpers);
  214. foreach ($helpers as $helper) {
  215. $helper = preg_replace('/Helper$/', '', $helper);
  216. $oldHelper = strtolower(substr($helper, 0, 1)).substr($helper, 1);
  217. $patterns[] = array(
  218. "\${$oldHelper} to \$this->{$helper}",
  219. "/\\\${$oldHelper}->/",
  220. "\\\$this->{$helper}->"
  221. );
  222. }
  223. $this->_filesRegexpUpdate($patterns);
  224. }
  225. /**
  226. * Update i18n.
  227. *
  228. * - Removes extra true param.
  229. * - Add the echo to __*() calls that didn't need them before.
  230. *
  231. * @return void
  232. */
  233. public function i18n() {
  234. $this->_paths = array(
  235. APP
  236. );
  237. if (!empty($this->params['plugin'])) {
  238. $this->_paths = array(App::pluginPath($this->params['plugin']));
  239. }
  240. $patterns = array(
  241. array(
  242. '<?php __*(*) to <?php echo __*(*)',
  243. '/<\?php\s*(__[a-z]*\(.*?\))/',
  244. '<?php echo \1'
  245. ),
  246. array(
  247. '<?php __*(*, true) to <?php echo __*()',
  248. '/<\?php\s*(__[a-z]*\(.*?)(,\s*true)(\))/',
  249. '<?php echo \1\3'
  250. ),
  251. array('__*(*, true) to __*(*)', '/(__[a-z]*\(.*?)(,\s*true)(\))/', '\1\3')
  252. );
  253. $this->_filesRegexpUpdate($patterns);
  254. }
  255. /**
  256. * Upgrade the removed basics functions.
  257. *
  258. * - a(*) -> array(*)
  259. * - e(*) -> echo *
  260. * - ife(*, *, *) -> !empty(*) ? * : *
  261. * - a(*) -> array(*)
  262. * - r(*, *, *) -> str_replace(*, *, *)
  263. * - up(*) -> strtoupper(*)
  264. * - low(*, *, *) -> strtolower(*)
  265. * - getMicrotime() -> microtime(true)
  266. *
  267. * @return void
  268. */
  269. public function basics() {
  270. $this->_paths = array(
  271. APP
  272. );
  273. if (!empty($this->params['plugin'])) {
  274. $this->_paths = array(App::pluginPath($this->params['plugin']));
  275. }
  276. $patterns = array(
  277. array(
  278. 'a(*) -> array(*)',
  279. '/\ba\((.*)\)/',
  280. 'array(\1)'
  281. ),
  282. array(
  283. 'e(*) -> echo *',
  284. '/\be\((.*)\)/',
  285. 'echo \1'
  286. ),
  287. array(
  288. 'ife(*, *, *) -> !empty(*) ? * : *',
  289. '/ife\((.*), (.*), (.*)\)/',
  290. '!empty(\1) ? \2 : \3'
  291. ),
  292. array(
  293. 'r(*, *, *) -> str_replace(*, *, *)',
  294. '/\br\(/',
  295. 'str_replace('
  296. ),
  297. array(
  298. 'up(*) -> strtoupper(*)',
  299. '/\bup\(/',
  300. 'strtoupper('
  301. ),
  302. array(
  303. 'low(*) -> strtolower(*)',
  304. '/\blow\(/',
  305. 'strtolower('
  306. ),
  307. array(
  308. 'getMicrotime() -> microtime(true)',
  309. '/getMicrotime\(\)/',
  310. 'microtime(true)'
  311. ),
  312. );
  313. $this->_filesRegexpUpdate($patterns);
  314. }
  315. /**
  316. * Update the properties moved to CakeRequest.
  317. *
  318. * @return void
  319. */
  320. public function request() {
  321. $views = array_diff(App::path('views'), App::core('views'));
  322. $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
  323. $components = array_diff(App::path('components'), App::core('components'));
  324. $this->_paths = array_merge($views, $controllers, $components);
  325. if (!empty($this->params['plugin'])) {
  326. $pluginPath = App::pluginPath($this->params['plugin']);
  327. $this->_paths = array(
  328. $pluginPath . 'controllers' . DS,
  329. $pluginPath . 'controllers' . DS . 'components' .DS,
  330. $pluginPath . 'views' . DS,
  331. );
  332. }
  333. $patterns = array(
  334. array(
  335. '$this->data -> $this->request->data',
  336. '/(\$this->data\b(?!\())/',
  337. '$this->request->data'
  338. ),
  339. array(
  340. '$this->params -> $this->request->params',
  341. '/(\$this->params\b(?!\())/',
  342. '$this->request->params'
  343. ),
  344. array(
  345. '$this->webroot -> $this->request->webroot',
  346. '/(\$this->webroot\b(?!\())/',
  347. '$this->request->webroot'
  348. ),
  349. array(
  350. '$this->base -> $this->request->base',
  351. '/(\$this->base\b(?!\())/',
  352. '$this->request->base'
  353. ),
  354. array(
  355. '$this->here -> $this->request->here',
  356. '/(\$this->here\b(?!\())/',
  357. '$this->request->here'
  358. ),
  359. array(
  360. '$this->action -> $this->request->action',
  361. '/(\$this->action\b(?!\())/',
  362. '$this->request->action'
  363. ),
  364. );
  365. $this->_filesRegexpUpdate($patterns);
  366. }
  367. /**
  368. * Update Configure::read() calls with no params.
  369. *
  370. * @return void
  371. */
  372. public function configure() {
  373. $this->_paths = array(
  374. APP
  375. );
  376. if (!empty($this->params['plugin'])) {
  377. $this->_paths = array(App::pluginPath($this->params['plugin']));
  378. }
  379. $patterns = array(
  380. array(
  381. "Configure::read() -> Configure::read('debug')",
  382. '/Configure::read\(\)/',
  383. 'Configure::read(\'debug\')'
  384. ),
  385. );
  386. $this->_filesRegexpUpdate($patterns);
  387. }
  388. /**
  389. * constants
  390. *
  391. * @return void
  392. */
  393. public function constants() {
  394. $this->_paths = array(
  395. APP
  396. );
  397. if (!empty($this->params['plugin'])) {
  398. $this->_paths = array(App::pluginPath($this->params['plugin']));
  399. }
  400. $patterns = array(
  401. array(
  402. "LIBS -> CAKE",
  403. '/\bLIBS\b/',
  404. 'CAKE'
  405. ),
  406. array(
  407. "CONFIGS -> APP . 'Config' . DS",
  408. '/\bCONFIGS\b/',
  409. 'APP . \'Config\' . DS'
  410. ),
  411. array(
  412. "CONTROLLERS -> APP . 'Controller' . DS",
  413. '/\bCONTROLLERS\b/',
  414. 'APP . \'Controller\' . DS'
  415. ),
  416. array(
  417. "COMPONENTS -> APP . 'Controller' . DS . 'Component' . DS",
  418. '/\bCOMPONENTS\b/',
  419. 'APP . \'Controller\' . DS . \'Component\''
  420. ),
  421. array(
  422. "MODELS -> APP . 'Model' . DS",
  423. '/\bMODELS\b/',
  424. 'APP . \'Model\' . DS'
  425. ),
  426. array(
  427. "BEHAVIORS -> APP . 'Model' . DS . 'Behavior' . DS",
  428. '/\bBEHAVIORS\b/',
  429. 'APP . \'Model\' . DS . \'Behavior\' . DS'
  430. ),
  431. array(
  432. "VIEWS -> APP . 'View' . DS",
  433. '/\bVIEWS\b/',
  434. 'APP . \'View\' . DS'
  435. ),
  436. array(
  437. "HELPERS -> APP . 'View' . DS . 'Helper' . DS",
  438. '/\bHELPERS\b/',
  439. 'APP . \'View\' . DS . \'Helper\' . DS'
  440. ),
  441. array(
  442. "LAYOUTS -> APP . 'View' . DS . 'Layouts' . DS",
  443. '/\bLAYOUTS\b/',
  444. 'APP . \'View\' . DS . \'Layouts\' . DS'
  445. ),
  446. array(
  447. "ELEMENTS -> APP . 'View' . DS . 'Elements' . DS",
  448. '/\bELEMENTS\b/',
  449. 'APP . \'View\' . DS . \'Elements\' . DS'
  450. ),
  451. array(
  452. "CONSOLE_LIBS -> CAKE . 'Console' . DS",
  453. '/\bCONSOLE_LIBS\b/',
  454. 'CAKE . \'Console\' . DS'
  455. ),
  456. array(
  457. "CAKE_TESTS_LIB -> CAKE . 'TestSuite' . DS",
  458. '/\bCAKE_TESTS_LIB\b/',
  459. 'CAKE . \'TestSuite\' . DS'
  460. ),
  461. array(
  462. "CAKE_TESTS -> CAKE . 'Test' . DS",
  463. '/\bCAKE_TESTS\b/',
  464. 'CAKE . \'Test\' . DS'
  465. )
  466. );
  467. $this->_filesRegexpUpdate($patterns);
  468. }
  469. /**
  470. * Update components.
  471. *
  472. * - Make components that extend Object to extend Component.
  473. *
  474. * @return void
  475. */
  476. public function components() {
  477. $this->_paths = App::Path('Controller/Component');
  478. if (!empty($this->params['plugin'])) {
  479. $this->_paths = App::Path('Controller/Component', $this->params['plugin']);
  480. }
  481. $patterns = array(
  482. array(
  483. '*Component extends Object to *Component extends Component',
  484. '/([a-zA-Z]*Component extends) Object/',
  485. '\1 Component'
  486. ),
  487. );
  488. $this->_filesRegexpUpdate($patterns);
  489. }
  490. /**
  491. * Replace cakeError with built-in exceptions.
  492. * NOTE: this ignores calls where you've passed your own secondary parameters to cakeError().
  493. * @return void
  494. */
  495. public function exceptions() {
  496. $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
  497. $components = array_diff(App::path('components'), App::core('components'));
  498. $this->_paths = array_merge($controllers, $components);
  499. if (!empty($this->params['plugin'])) {
  500. $pluginPath = App::pluginPath($this->params['plugin']);
  501. $this->_paths = array(
  502. $pluginPath . 'controllers' . DS,
  503. $pluginPath . 'controllers' . DS . 'components' .DS,
  504. );
  505. }
  506. $patterns = array(
  507. array(
  508. '$this->cakeError("error400") -> throw new BadRequestException()',
  509. '/(\$this->cakeError\(["\']error400["\']\));/',
  510. 'throw new BadRequestException();'
  511. ),
  512. array(
  513. '$this->cakeError("error404") -> throw new NotFoundException()',
  514. '/(\$this->cakeError\(["\']error404["\']\));/',
  515. 'throw new NotFoundException();'
  516. ),
  517. array(
  518. '$this->cakeError("error500") -> throw new InternalErrorException()',
  519. '/(\$this->cakeError\(["\']error500["\']\));/',
  520. 'throw new InternalErrorException();'
  521. ),
  522. );
  523. $this->_filesRegexpUpdate($patterns);
  524. }
  525. /**
  526. * Move application views files to where they now should be
  527. *
  528. * Find all view files in the folder and determine where cake expects the file to be
  529. *
  530. * @return void
  531. */
  532. protected function _moveViewFiles() {
  533. if (!is_dir('View')) {
  534. return;
  535. }
  536. $dirs = scandir('View');
  537. foreach ($dirs as $old) {
  538. if (!is_dir('View' . DS . $old) || $old === '.' || $old === '..') {
  539. continue;
  540. }
  541. $new = 'View' . DS . Inflector::camelize($old);
  542. $old = 'View' . DS . $old;
  543. if ($new == $old) {
  544. continue;
  545. }
  546. $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
  547. if (!$this->params['dry-run']) {
  548. if ($this->params['git']) {
  549. exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
  550. exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
  551. } else {
  552. $Folder = new Folder($old);
  553. $Folder->move($new);
  554. }
  555. }
  556. }
  557. }
  558. /**
  559. * Move application php files to where they now should be
  560. *
  561. * Find all php files in the folder (honoring recursive) and determine where cake expects the file to be
  562. * If the file is not exactly where cake expects it - move it.
  563. *
  564. * @param mixed $path
  565. * @param mixed $options array(recursive, checkFolder)
  566. * @return void
  567. */
  568. protected function _movePhpFiles($path, $options) {
  569. if (!is_dir($path)) {
  570. return;
  571. }
  572. $paths = $this->_paths;
  573. $this->_paths = array($path);
  574. $this->_files = array();
  575. if ($options['recursive']) {
  576. $this->_findFiles('php');
  577. } else {
  578. $this->_files = scandir($path);
  579. foreach($this->_files as $i => $file) {
  580. if (strlen($file) < 5 || substr($file, -4) !== '.php') {
  581. unset($this->_files[$i]);
  582. }
  583. }
  584. }
  585. $cwd = getcwd();
  586. foreach ($this->_files as &$file) {
  587. $file = $cwd . DS . $file;
  588. $contents = file_get_contents($file);
  589. preg_match($options['regex'], $contents, $match);
  590. if (!$match) {
  591. continue;
  592. }
  593. $class = $match[1];
  594. if (substr($class, 0, 3) === 'Dbo') {
  595. $type = 'Dbo';
  596. } else {
  597. preg_match('@([A-Z][^A-Z]*)$@', $class, $match);
  598. if ($match) {
  599. $type = $match[1];
  600. } else {
  601. $type = 'unknown';
  602. }
  603. }
  604. preg_match('@^.*[\\\/]plugins[\\\/](.*?)[\\\/]@', $file, $match);
  605. $base = $cwd . DS;
  606. $plugin = false;
  607. if ($match) {
  608. $base = $match[0];
  609. $plugin = $match[1];
  610. }
  611. if ($options['checkFolder'] && !empty($this->_map[$type])) {
  612. $folder = str_replace('/', DS, $this->_map[$type]);
  613. $new = $base . $folder . DS . $class . '.php';
  614. } else {
  615. $new = dirname($file) . DS . $class . '.php';
  616. }
  617. if ($file === $new) {
  618. continue;
  619. }
  620. $dir = dirname($new);
  621. if (!is_dir($dir)) {
  622. new Folder($dir, true);
  623. }
  624. $this->out(__d('cake_console', 'Moving %s to %s', $file, $new), 1, Shell::VERBOSE);
  625. if (!$this->params['dry-run']) {
  626. if ($this->params['git']) {
  627. exec('git mv -f ' . escapeshellarg($file) . ' ' . escapeshellarg($file . '__'));
  628. exec('git mv -f ' . escapeshellarg($file. '__') . ' ' . escapeshellarg($new));
  629. } else {
  630. rename($file, $new);
  631. }
  632. }
  633. }
  634. $this->_paths = $paths;
  635. }
  636. /**
  637. * Updates files based on regular expressions.
  638. *
  639. * @param array $patterns Array of search and replacement patterns.
  640. * @return void
  641. */
  642. protected function _filesRegexpUpdate($patterns) {
  643. $this->_findFiles($this->params['ext']);
  644. foreach ($this->_files as $file) {
  645. $this->out(__d('cake_console', 'Updating %s...', $file), 1, Shell::VERBOSE);
  646. $this->_updateFile($file, $patterns);
  647. }
  648. }
  649. /**
  650. * Searches the paths and finds files based on extension.
  651. *
  652. * @param string $extensions
  653. * @return void
  654. */
  655. protected function _findFiles($extensions = '') {
  656. $this->_files = array();
  657. foreach ($this->_paths as $path) {
  658. if (!is_dir($path)) {
  659. continue;
  660. }
  661. $Iterator = new RegexIterator(
  662. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
  663. '/^.+\.(' . $extensions . ')$/i',
  664. RegexIterator::MATCH
  665. );
  666. foreach ($Iterator as $file) {
  667. if ($file->isFile()) {
  668. $this->_files[] = $file->getPathname();
  669. }
  670. }
  671. }
  672. }
  673. /**
  674. * Update a single file.
  675. *
  676. * @param string $file The file to update
  677. * @param array $patterns The replacement patterns to run.
  678. * @return void
  679. */
  680. protected function _updateFile($file, $patterns) {
  681. $contents = file_get_contents($file);
  682. foreach ($patterns as $pattern) {
  683. $this->out(__d('cake_console', ' * Updating %s', $pattern[0]), 1, Shell::VERBOSE);
  684. $contents = preg_replace($pattern[1], $pattern[2], $contents);
  685. }
  686. $this->out(__d('cake_console', 'Done updating %s', $file), 1);
  687. if (!$this->params['dry-run']) {
  688. file_put_contents($file, $contents);
  689. }
  690. }
  691. /**
  692. * get the option parser
  693. *
  694. * @return ConsoleOptionParser
  695. */
  696. public function getOptionParser() {
  697. $subcommandParser = array(
  698. 'options' => array(
  699. 'plugin' => array(
  700. 'short' => 'p',
  701. 'help' => __d('cake_console', 'The plugin to update. Only the specified plugin will be updated.')
  702. ),
  703. 'ext' => array(
  704. 'short' => 'e',
  705. 'help' => __d('cake_console', 'The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
  706. 'default' => 'php|ctp|thtml|inc|tpl'
  707. ),
  708. 'git' => array(
  709. 'short' => 'g',
  710. 'help' => __d('cake_console', 'Use git command for moving files around.'),
  711. 'boolean' => true
  712. ),
  713. 'dry-run'=> array(
  714. 'short' => 'd',
  715. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  716. 'boolean' => true
  717. )
  718. )
  719. );
  720. return parent::getOptionParser()
  721. ->description(__d('cake_console', "A shell to help automate upgrading from CakePHP 1.3 to 2.0. \n" .
  722. "Be sure to have a backup of your application before running these commands."))
  723. ->addSubcommand('all', array(
  724. 'help' => __d('cake_console', 'Run all upgrade commands.'),
  725. 'parser' => $subcommandParser
  726. ))
  727. ->addSubcommand('tests', array(
  728. 'help' => __d('cake_console', 'Update tests class names to FooTest rather than FooTestCase.'),
  729. 'parser' => $subcommandParser
  730. ))
  731. ->addSubcommand('locations', array(
  732. 'help' => __d('cake_console', 'Move files and folders to their new homes.'),
  733. 'parser' => $subcommandParser
  734. ))
  735. ->addSubcommand('i18n', array(
  736. 'help' => __d('cake_console', 'Update the i18n translation method calls.'),
  737. 'parser' => $subcommandParser
  738. ))
  739. ->addSubcommand('helpers', array(
  740. 'help' => __d('cake_console', 'Update calls to helpers.'),
  741. 'parser' => $subcommandParser
  742. ))
  743. ->addSubcommand('basics', array(
  744. 'help' => __d('cake_console', 'Update removed basics functions to PHP native functions.'),
  745. 'parser' => $subcommandParser
  746. ))
  747. ->addSubcommand('request', array(
  748. 'help' => __d('cake_console', 'Update removed request access, and replace with $this->request.'),
  749. 'parser' => $subcommandParser
  750. ))
  751. ->addSubcommand('configure', array(
  752. 'help' => __d('cake_console', "Update Configure::read() to Configure::read('debug')"),
  753. 'parser' => $subcommandParser
  754. ))
  755. ->addSubcommand('constants', array(
  756. 'help' => __d('cake_console', "Replace Obsolete constants"),
  757. 'parser' => $subcommandParser
  758. ))
  759. ->addSubcommand('components', array(
  760. 'help' => __d('cake_console', 'Update components to extend Component class.'),
  761. 'parser' => $subcommandParser
  762. ))
  763. ->addSubcommand('exceptions', array(
  764. 'help' => __d('cake_console', 'Replace use of cakeError with exceptions.'),
  765. 'parser' => $subcommandParser
  766. ));
  767. }
  768. }