PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Console/Command/UpgradeShell.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 825 lines | 610 code | 55 blank | 160 comment | 51 complexity | babd0ed45a95dea8530432d1bffd9fbf MD5 | raw file
  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. CakePlugin::load($plugin);
  210. $pluginHelpers = array_merge(
  211. $pluginHelpers,
  212. App::objects('helper', App::pluginPath($plugin) . DS . 'views' . DS . 'helpers' . DS, false)
  213. );
  214. }
  215. $helpers = array_merge($pluginHelpers, $helpers);
  216. foreach ($helpers as $helper) {
  217. $helper = preg_replace('/Helper$/', '', $helper);
  218. $oldHelper = strtolower(substr($helper, 0, 1)).substr($helper, 1);
  219. $patterns[] = array(
  220. "\${$oldHelper} to \$this->{$helper}",
  221. "/\\\${$oldHelper}->/",
  222. "\\\$this->{$helper}->"
  223. );
  224. }
  225. $this->_filesRegexpUpdate($patterns);
  226. }
  227. /**
  228. * Update i18n.
  229. *
  230. * - Removes extra true param.
  231. * - Add the echo to __*() calls that didn't need them before.
  232. *
  233. * @return void
  234. */
  235. public function i18n() {
  236. $this->_paths = array(
  237. APP
  238. );
  239. if (!empty($this->params['plugin'])) {
  240. $this->_paths = array(App::pluginPath($this->params['plugin']));
  241. }
  242. $patterns = array(
  243. array(
  244. '<?php __*(*) to <?php echo __*(*)',
  245. '/<\?php\s*(__[a-z]*\(.*?\))/',
  246. '<?php echo \1'
  247. ),
  248. array(
  249. '<?php __*(*, true) to <?php echo __*()',
  250. '/<\?php\s*(__[a-z]*\(.*?)(,\s*true)(\))/',
  251. '<?php echo \1\3'
  252. ),
  253. array('__*(*, true) to __*(*)', '/(__[a-z]*\(.*?)(,\s*true)(\))/', '\1\3')
  254. );
  255. $this->_filesRegexpUpdate($patterns);
  256. }
  257. /**
  258. * Upgrade the removed basics functions.
  259. *
  260. * - a(*) -> array(*)
  261. * - e(*) -> echo *
  262. * - ife(*, *, *) -> !empty(*) ? * : *
  263. * - a(*) -> array(*)
  264. * - r(*, *, *) -> str_replace(*, *, *)
  265. * - up(*) -> strtoupper(*)
  266. * - low(*, *, *) -> strtolower(*)
  267. * - getMicrotime() -> microtime(true)
  268. *
  269. * @return void
  270. */
  271. public function basics() {
  272. $this->_paths = array(
  273. APP
  274. );
  275. if (!empty($this->params['plugin'])) {
  276. $this->_paths = array(App::pluginPath($this->params['plugin']));
  277. }
  278. $patterns = array(
  279. array(
  280. 'a(*) -> array(*)',
  281. '/\ba\((.*)\)/',
  282. 'array(\1)'
  283. ),
  284. array(
  285. 'e(*) -> echo *',
  286. '/\be\((.*)\)/',
  287. 'echo \1'
  288. ),
  289. array(
  290. 'ife(*, *, *) -> !empty(*) ? * : *',
  291. '/ife\((.*), (.*), (.*)\)/',
  292. '!empty(\1) ? \2 : \3'
  293. ),
  294. array(
  295. 'r(*, *, *) -> str_replace(*, *, *)',
  296. '/\br\(/',
  297. 'str_replace('
  298. ),
  299. array(
  300. 'up(*) -> strtoupper(*)',
  301. '/\bup\(/',
  302. 'strtoupper('
  303. ),
  304. array(
  305. 'low(*) -> strtolower(*)',
  306. '/\blow\(/',
  307. 'strtolower('
  308. ),
  309. array(
  310. 'getMicrotime() -> microtime(true)',
  311. '/getMicrotime\(\)/',
  312. 'microtime(true)'
  313. ),
  314. );
  315. $this->_filesRegexpUpdate($patterns);
  316. }
  317. /**
  318. * Update the properties moved to CakeRequest.
  319. *
  320. * @return void
  321. */
  322. public function request() {
  323. $views = array_diff(App::path('views'), App::core('views'));
  324. $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
  325. $components = array_diff(App::path('components'), App::core('components'));
  326. $this->_paths = array_merge($views, $controllers, $components);
  327. if (!empty($this->params['plugin'])) {
  328. $pluginPath = App::pluginPath($this->params['plugin']);
  329. $this->_paths = array(
  330. $pluginPath . 'controllers' . DS,
  331. $pluginPath . 'controllers' . DS . 'components' .DS,
  332. $pluginPath . 'views' . DS,
  333. );
  334. }
  335. $patterns = array(
  336. array(
  337. '$this->data -> $this->request->data',
  338. '/(\$this->data\b(?!\())/',
  339. '$this->request->data'
  340. ),
  341. array(
  342. '$this->params -> $this->request->params',
  343. '/(\$this->params\b(?!\())/',
  344. '$this->request->params'
  345. ),
  346. array(
  347. '$this->webroot -> $this->request->webroot',
  348. '/(\$this->webroot\b(?!\())/',
  349. '$this->request->webroot'
  350. ),
  351. array(
  352. '$this->base -> $this->request->base',
  353. '/(\$this->base\b(?!\())/',
  354. '$this->request->base'
  355. ),
  356. array(
  357. '$this->here -> $this->request->here',
  358. '/(\$this->here\b(?!\())/',
  359. '$this->request->here'
  360. ),
  361. array(
  362. '$this->action -> $this->request->action',
  363. '/(\$this->action\b(?!\())/',
  364. '$this->request->action'
  365. ),
  366. );
  367. $this->_filesRegexpUpdate($patterns);
  368. }
  369. /**
  370. * Update Configure::read() calls with no params.
  371. *
  372. * @return void
  373. */
  374. public function configure() {
  375. $this->_paths = array(
  376. APP
  377. );
  378. if (!empty($this->params['plugin'])) {
  379. $this->_paths = array(App::pluginPath($this->params['plugin']));
  380. }
  381. $patterns = array(
  382. array(
  383. "Configure::read() -> Configure::read('debug')",
  384. '/Configure::read\(\)/',
  385. 'Configure::read(\'debug\')'
  386. ),
  387. );
  388. $this->_filesRegexpUpdate($patterns);
  389. }
  390. /**
  391. * constants
  392. *
  393. * @return void
  394. */
  395. public function constants() {
  396. $this->_paths = array(
  397. APP
  398. );
  399. if (!empty($this->params['plugin'])) {
  400. $this->_paths = array(App::pluginPath($this->params['plugin']));
  401. }
  402. $patterns = array(
  403. array(
  404. "LIBS -> CAKE",
  405. '/\bLIBS\b/',
  406. 'CAKE'
  407. ),
  408. array(
  409. "CONFIGS -> APP . 'Config' . DS",
  410. '/\bCONFIGS\b/',
  411. 'APP . \'Config\' . DS'
  412. ),
  413. array(
  414. "CONTROLLERS -> APP . 'Controller' . DS",
  415. '/\bCONTROLLERS\b/',
  416. 'APP . \'Controller\' . DS'
  417. ),
  418. array(
  419. "COMPONENTS -> APP . 'Controller' . DS . 'Component' . DS",
  420. '/\bCOMPONENTS\b/',
  421. 'APP . \'Controller\' . DS . \'Component\''
  422. ),
  423. array(
  424. "MODELS -> APP . 'Model' . DS",
  425. '/\bMODELS\b/',
  426. 'APP . \'Model\' . DS'
  427. ),
  428. array(
  429. "BEHAVIORS -> APP . 'Model' . DS . 'Behavior' . DS",
  430. '/\bBEHAVIORS\b/',
  431. 'APP . \'Model\' . DS . \'Behavior\' . DS'
  432. ),
  433. array(
  434. "VIEWS -> APP . 'View' . DS",
  435. '/\bVIEWS\b/',
  436. 'APP . \'View\' . DS'
  437. ),
  438. array(
  439. "HELPERS -> APP . 'View' . DS . 'Helper' . DS",
  440. '/\bHELPERS\b/',
  441. 'APP . \'View\' . DS . \'Helper\' . DS'
  442. ),
  443. array(
  444. "LAYOUTS -> APP . 'View' . DS . 'Layouts' . DS",
  445. '/\bLAYOUTS\b/',
  446. 'APP . \'View\' . DS . \'Layouts\' . DS'
  447. ),
  448. array(
  449. "ELEMENTS -> APP . 'View' . DS . 'Elements' . DS",
  450. '/\bELEMENTS\b/',
  451. 'APP . \'View\' . DS . \'Elements\' . DS'
  452. ),
  453. array(
  454. "CONSOLE_LIBS -> CAKE . 'Console' . DS",
  455. '/\bCONSOLE_LIBS\b/',
  456. 'CAKE . \'Console\' . DS'
  457. ),
  458. array(
  459. "CAKE_TESTS_LIB -> CAKE . 'TestSuite' . DS",
  460. '/\bCAKE_TESTS_LIB\b/',
  461. 'CAKE . \'TestSuite\' . DS'
  462. ),
  463. array(
  464. "CAKE_TESTS -> CAKE . 'Test' . DS",
  465. '/\bCAKE_TESTS\b/',
  466. 'CAKE . \'Test\' . DS'
  467. )
  468. );
  469. $this->_filesRegexpUpdate($patterns);
  470. }
  471. /**
  472. * Update components.
  473. *
  474. * - Make components that extend Object to extend Component.
  475. *
  476. * @return void
  477. */
  478. public function components() {
  479. $this->_paths = App::Path('Controller/Component');
  480. if (!empty($this->params['plugin'])) {
  481. $this->_paths = App::Path('Controller/Component', $this->params['plugin']);
  482. }
  483. $patterns = array(
  484. array(
  485. '*Component extends Object to *Component extends Component',
  486. '/([a-zA-Z]*Component extends) Object/',
  487. '\1 Component'
  488. ),
  489. );
  490. $this->_filesRegexpUpdate($patterns);
  491. }
  492. /**
  493. * Replace cakeError with built-in exceptions.
  494. * NOTE: this ignores calls where you've passed your own secondary parameters to cakeError().
  495. * @return void
  496. */
  497. public function exceptions() {
  498. $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
  499. $components = array_diff(App::path('components'), App::core('components'));
  500. $this->_paths = array_merge($controllers, $components);
  501. if (!empty($this->params['plugin'])) {
  502. $pluginPath = App::pluginPath($this->params['plugin']);
  503. $this->_paths = array(
  504. $pluginPath . 'controllers' . DS,
  505. $pluginPath . 'controllers' . DS . 'components' .DS,
  506. );
  507. }
  508. $patterns = array(
  509. array(
  510. '$this->cakeError("error400") -> throw new BadRequestException()',
  511. '/(\$this->cakeError\(["\']error400["\']\));/',
  512. 'throw new BadRequestException();'
  513. ),
  514. array(
  515. '$this->cakeError("error404") -> throw new NotFoundException()',
  516. '/(\$this->cakeError\(["\']error404["\']\));/',
  517. 'throw new NotFoundException();'
  518. ),
  519. array(
  520. '$this->cakeError("error500") -> throw new InternalErrorException()',
  521. '/(\$this->cakeError\(["\']error500["\']\));/',
  522. 'throw new InternalErrorException();'
  523. ),
  524. );
  525. $this->_filesRegexpUpdate($patterns);
  526. }
  527. /**
  528. * Move application views files to where they now should be
  529. *
  530. * Find all view files in the folder and determine where cake expects the file to be
  531. *
  532. * @return void
  533. */
  534. protected function _moveViewFiles() {
  535. if (!is_dir('View')) {
  536. return;
  537. }
  538. $dirs = scandir('View');
  539. foreach ($dirs as $old) {
  540. if (!is_dir('View' . DS . $old) || $old === '.' || $old === '..') {
  541. continue;
  542. }
  543. $new = 'View' . DS . Inflector::camelize($old);
  544. $old = 'View' . DS . $old;
  545. if ($new == $old) {
  546. continue;
  547. }
  548. $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
  549. if (!$this->params['dry-run']) {
  550. if ($this->params['git']) {
  551. exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
  552. exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
  553. } else {
  554. $Folder = new Folder($old);
  555. $Folder->move($new);
  556. }
  557. }
  558. }
  559. }
  560. /**
  561. * Move application php files to where they now should be
  562. *
  563. * Find all php files in the folder (honoring recursive) and determine where cake expects the file to be
  564. * If the file is not exactly where cake expects it - move it.
  565. *
  566. * @param mixed $path
  567. * @param mixed $options array(recursive, checkFolder)
  568. * @return void
  569. */
  570. protected function _movePhpFiles($path, $options) {
  571. if (!is_dir($path)) {
  572. return;
  573. }
  574. $paths = $this->_paths;
  575. $this->_paths = array($path);
  576. $this->_files = array();
  577. if ($options['recursive']) {
  578. $this->_findFiles('php');
  579. } else {
  580. $this->_files = scandir($path);
  581. foreach ($this->_files as $i => $file) {
  582. if (strlen($file) < 5 || substr($file, -4) !== '.php') {
  583. unset($this->_files[$i]);
  584. }
  585. }
  586. }
  587. $cwd = getcwd();
  588. foreach ($this->_files as &$file) {
  589. $file = $cwd . DS . $file;
  590. $contents = file_get_contents($file);
  591. preg_match($options['regex'], $contents, $match);
  592. if (!$match) {
  593. continue;
  594. }
  595. $class = $match[1];
  596. if (substr($class, 0, 3) === 'Dbo') {
  597. $type = 'Dbo';
  598. } else {
  599. preg_match('@([A-Z][^A-Z]*)$@', $class, $match);
  600. if ($match) {
  601. $type = $match[1];
  602. } else {
  603. $type = 'unknown';
  604. }
  605. }
  606. preg_match('@^.*[\\\/]plugins[\\\/](.*?)[\\\/]@', $file, $match);
  607. $base = $cwd . DS;
  608. $plugin = false;
  609. if ($match) {
  610. $base = $match[0];
  611. $plugin = $match[1];
  612. }
  613. if ($options['checkFolder'] && !empty($this->_map[$type])) {
  614. $folder = str_replace('/', DS, $this->_map[$type]);
  615. $new = $base . $folder . DS . $class . '.php';
  616. } else {
  617. $new = dirname($file) . DS . $class . '.php';
  618. }
  619. if ($file === $new) {
  620. continue;
  621. }
  622. $dir = dirname($new);
  623. if (!is_dir($dir)) {
  624. new Folder($dir, true);
  625. }
  626. $this->out(__d('cake_console', 'Moving %s to %s', $file, $new), 1, Shell::VERBOSE);
  627. if (!$this->params['dry-run']) {
  628. if ($this->params['git']) {
  629. exec('git mv -f ' . escapeshellarg($file) . ' ' . escapeshellarg($file . '__'));
  630. exec('git mv -f ' . escapeshellarg($file. '__') . ' ' . escapeshellarg($new));
  631. } else {
  632. rename($file, $new);
  633. }
  634. }
  635. }
  636. $this->_paths = $paths;
  637. }
  638. /**
  639. * Updates files based on regular expressions.
  640. *
  641. * @param array $patterns Array of search and replacement patterns.
  642. * @return void
  643. */
  644. protected function _filesRegexpUpdate($patterns) {
  645. $this->_findFiles($this->params['ext']);
  646. foreach ($this->_files as $file) {
  647. $this->out(__d('cake_console', 'Updating %s...', $file), 1, Shell::VERBOSE);
  648. $this->_updateFile($file, $patterns);
  649. }
  650. }
  651. /**
  652. * Searches the paths and finds files based on extension.
  653. *
  654. * @param string $extensions
  655. * @return void
  656. */
  657. protected function _findFiles($extensions = '') {
  658. $this->_files = array();
  659. foreach ($this->_paths as $path) {
  660. if (!is_dir($path)) {
  661. continue;
  662. }
  663. $Iterator = new RegexIterator(
  664. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
  665. '/^.+\.(' . $extensions . ')$/i',
  666. RegexIterator::MATCH
  667. );
  668. foreach ($Iterator as $file) {
  669. if ($file->isFile()) {
  670. $this->_files[] = $file->getPathname();
  671. }
  672. }
  673. }
  674. }
  675. /**
  676. * Update a single file.
  677. *
  678. * @param string $file The file to update
  679. * @param array $patterns The replacement patterns to run.
  680. * @return void
  681. */
  682. protected function _updateFile($file, $patterns) {
  683. $contents = file_get_contents($file);
  684. foreach ($patterns as $pattern) {
  685. $this->out(__d('cake_console', ' * Updating %s', $pattern[0]), 1, Shell::VERBOSE);
  686. $contents = preg_replace($pattern[1], $pattern[2], $contents);
  687. }
  688. $this->out(__d('cake_console', 'Done updating %s', $file), 1);
  689. if (!$this->params['dry-run']) {
  690. file_put_contents($file, $contents);
  691. }
  692. }
  693. /**
  694. * get the option parser
  695. *
  696. * @return ConsoleOptionParser
  697. */
  698. public function getOptionParser() {
  699. $subcommandParser = array(
  700. 'options' => array(
  701. 'plugin' => array(
  702. 'short' => 'p',
  703. 'help' => __d('cake_console', 'The plugin to update. Only the specified plugin will be updated.')
  704. ),
  705. 'ext' => array(
  706. 'short' => 'e',
  707. 'help' => __d('cake_console', 'The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
  708. 'default' => 'php|ctp|thtml|inc|tpl'
  709. ),
  710. 'git' => array(
  711. 'short' => 'g',
  712. 'help' => __d('cake_console', 'Use git command for moving files around.'),
  713. 'boolean' => true
  714. ),
  715. 'dry-run' => array(
  716. 'short' => 'd',
  717. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  718. 'boolean' => true
  719. )
  720. )
  721. );
  722. return parent::getOptionParser()
  723. ->description(__d('cake_console', "A shell to help automate upgrading from CakePHP 1.3 to 2.0. \n" .
  724. "Be sure to have a backup of your application before running these commands."))
  725. ->addSubcommand('all', array(
  726. 'help' => __d('cake_console', 'Run all upgrade commands.'),
  727. 'parser' => $subcommandParser
  728. ))
  729. ->addSubcommand('tests', array(
  730. 'help' => __d('cake_console', 'Update tests class names to FooTest rather than FooTestCase.'),
  731. 'parser' => $subcommandParser
  732. ))
  733. ->addSubcommand('locations', array(
  734. 'help' => __d('cake_console', 'Move files and folders to their new homes.'),
  735. 'parser' => $subcommandParser
  736. ))
  737. ->addSubcommand('i18n', array(
  738. 'help' => __d('cake_console', 'Update the i18n translation method calls.'),
  739. 'parser' => $subcommandParser
  740. ))
  741. ->addSubcommand('helpers', array(
  742. 'help' => __d('cake_console', 'Update calls to helpers.'),
  743. 'parser' => $subcommandParser
  744. ))
  745. ->addSubcommand('basics', array(
  746. 'help' => __d('cake_console', 'Update removed basics functions to PHP native functions.'),
  747. 'parser' => $subcommandParser
  748. ))
  749. ->addSubcommand('request', array(
  750. 'help' => __d('cake_console', 'Update removed request access, and replace with $this->request.'),
  751. 'parser' => $subcommandParser
  752. ))
  753. ->addSubcommand('configure', array(
  754. 'help' => __d('cake_console', "Update Configure::read() to Configure::read('debug')"),
  755. 'parser' => $subcommandParser
  756. ))
  757. ->addSubcommand('constants', array(
  758. 'help' => __d('cake_console', "Replace Obsolete constants"),
  759. 'parser' => $subcommandParser
  760. ))
  761. ->addSubcommand('components', array(
  762. 'help' => __d('cake_console', 'Update components to extend Component class.'),
  763. 'parser' => $subcommandParser
  764. ))
  765. ->addSubcommand('exceptions', array(
  766. 'help' => __d('cake_console', 'Replace use of cakeError with exceptions.'),
  767. 'parser' => $subcommandParser
  768. ));
  769. }
  770. }