UpgradeShell.php 22 KB

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