UpgradeShell.php 21 KB

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