UpgradeShell.php 22 KB

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