UpgradeShell.php 22 KB

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