UpgradeShell.php 22 KB

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