UpgradeShell.php 22 KB

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