UpgradeShell.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. array(
  370. '$this->request->onlyAllow() -> $this->request->allowMethod()',
  371. '/\$this->request->onlyAllow\(/',
  372. '$this->request->allowMethod('
  373. )
  374. );
  375. $this->_filesRegexpUpdate($patterns);
  376. }
  377. /**
  378. * Update Configure::read() calls with no params.
  379. *
  380. * @return void
  381. */
  382. public function configure() {
  383. $this->_paths = array(
  384. APP
  385. );
  386. if (!empty($this->params['plugin'])) {
  387. $this->_paths = array(App::pluginPath($this->params['plugin']));
  388. }
  389. $patterns = array(
  390. array(
  391. "Configure::read() -> Configure::read('debug')",
  392. '/Configure::read\(\)/',
  393. 'Configure::read(\'debug\')'
  394. ),
  395. );
  396. $this->_filesRegexpUpdate($patterns);
  397. }
  398. /**
  399. * constants
  400. *
  401. * @return void
  402. */
  403. public function constants() {
  404. $this->_paths = array(
  405. APP
  406. );
  407. if (!empty($this->params['plugin'])) {
  408. $this->_paths = array(App::pluginPath($this->params['plugin']));
  409. }
  410. $patterns = array(
  411. array(
  412. "LIBS -> CAKE",
  413. '/\bLIBS\b/',
  414. 'CAKE'
  415. ),
  416. array(
  417. "CONFIGS -> APP . 'Config' . DS",
  418. '/\bCONFIGS\b/',
  419. 'APP . \'Config\' . DS'
  420. ),
  421. array(
  422. "CONTROLLERS -> APP . 'Controller' . DS",
  423. '/\bCONTROLLERS\b/',
  424. 'APP . \'Controller\' . DS'
  425. ),
  426. array(
  427. "COMPONENTS -> APP . 'Controller' . DS . 'Component' . DS",
  428. '/\bCOMPONENTS\b/',
  429. 'APP . \'Controller\' . DS . \'Component\''
  430. ),
  431. array(
  432. "MODELS -> APP . 'Model' . DS",
  433. '/\bMODELS\b/',
  434. 'APP . \'Model\' . DS'
  435. ),
  436. array(
  437. "BEHAVIORS -> APP . 'Model' . DS . 'Behavior' . DS",
  438. '/\bBEHAVIORS\b/',
  439. 'APP . \'Model\' . DS . \'Behavior\' . DS'
  440. ),
  441. array(
  442. "VIEWS -> APP . 'View' . DS",
  443. '/\bVIEWS\b/',
  444. 'APP . \'View\' . DS'
  445. ),
  446. array(
  447. "HELPERS -> APP . 'View' . DS . 'Helper' . DS",
  448. '/\bHELPERS\b/',
  449. 'APP . \'View\' . DS . \'Helper\' . DS'
  450. ),
  451. array(
  452. "LAYOUTS -> APP . 'View' . DS . 'Layouts' . DS",
  453. '/\bLAYOUTS\b/',
  454. 'APP . \'View\' . DS . \'Layouts\' . DS'
  455. ),
  456. array(
  457. "ELEMENTS -> APP . 'View' . DS . 'Elements' . DS",
  458. '/\bELEMENTS\b/',
  459. 'APP . \'View\' . DS . \'Elements\' . DS'
  460. ),
  461. array(
  462. "CONSOLE_LIBS -> CAKE . 'Console' . DS",
  463. '/\bCONSOLE_LIBS\b/',
  464. 'CAKE . \'Console\' . DS'
  465. ),
  466. array(
  467. "CAKE_TESTS_LIB -> CAKE . 'TestSuite' . DS",
  468. '/\bCAKE_TESTS_LIB\b/',
  469. 'CAKE . \'TestSuite\' . DS'
  470. ),
  471. array(
  472. "CAKE_TESTS -> CAKE . 'Test' . DS",
  473. '/\bCAKE_TESTS\b/',
  474. 'CAKE . \'Test\' . DS'
  475. )
  476. );
  477. $this->_filesRegexpUpdate($patterns);
  478. }
  479. /**
  480. * Update controller redirects.
  481. *
  482. * - Make redirect statements return early.
  483. *
  484. * @return void
  485. */
  486. public function controller_redirects() {
  487. $this->_paths = App::Path('Controller');
  488. if (!empty($this->params['plugin'])) {
  489. $this->_paths = App::Path('Controller', $this->params['plugin']);
  490. }
  491. $patterns = array(
  492. array(
  493. '$this->redirect() to return $this->redirect()',
  494. '/\t\$this-\>redirect\(/',
  495. "\t" . 'return $this->redirect('
  496. ),
  497. );
  498. $this->_filesRegexpUpdate($patterns);
  499. }
  500. /**
  501. * Update components.
  502. *
  503. * - Make components that extend Object to extend Component.
  504. *
  505. * @return void
  506. */
  507. public function components() {
  508. $this->_paths = App::Path('Controller/Component');
  509. if (!empty($this->params['plugin'])) {
  510. $this->_paths = App::Path('Controller/Component', $this->params['plugin']);
  511. }
  512. $patterns = array(
  513. array(
  514. '*Component extends Object to *Component extends Component',
  515. '/([a-zA-Z]*Component extends) Object/',
  516. '\1 Component'
  517. ),
  518. );
  519. $this->_filesRegexpUpdate($patterns);
  520. }
  521. /**
  522. * Replace cakeError with built-in exceptions.
  523. * NOTE: this ignores calls where you've passed your own secondary parameters to cakeError().
  524. * @return void
  525. */
  526. public function exceptions() {
  527. $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
  528. $components = array_diff(App::path('components'), App::core('components'));
  529. $this->_paths = array_merge($controllers, $components);
  530. if (!empty($this->params['plugin'])) {
  531. $pluginPath = App::pluginPath($this->params['plugin']);
  532. $this->_paths = array(
  533. $pluginPath . 'controllers' . DS,
  534. $pluginPath . 'controllers' . DS . 'components' . DS,
  535. );
  536. }
  537. $patterns = array(
  538. array(
  539. '$this->cakeError("error400") -> throw new BadRequestException()',
  540. '/(\$this->cakeError\(["\']error400["\']\));/',
  541. 'throw new BadRequestException();'
  542. ),
  543. array(
  544. '$this->cakeError("error404") -> throw new NotFoundException()',
  545. '/(\$this->cakeError\(["\']error404["\']\));/',
  546. 'throw new NotFoundException();'
  547. ),
  548. array(
  549. '$this->cakeError("error500") -> throw new InternalErrorException()',
  550. '/(\$this->cakeError\(["\']error500["\']\));/',
  551. 'throw new InternalErrorException();'
  552. ),
  553. );
  554. $this->_filesRegexpUpdate($patterns);
  555. }
  556. /**
  557. * Move application views files to where they now should be
  558. *
  559. * Find all view files in the folder and determine where cake expects the file to be
  560. *
  561. * @return void
  562. */
  563. protected function _moveViewFiles() {
  564. if (!is_dir('View')) {
  565. return;
  566. }
  567. $dirs = scandir('View');
  568. foreach ($dirs as $old) {
  569. if (!is_dir('View' . DS . $old) || $old === '.' || $old === '..') {
  570. continue;
  571. }
  572. $new = 'View' . DS . Inflector::camelize($old);
  573. $old = 'View' . DS . $old;
  574. if ($new == $old) {
  575. continue;
  576. }
  577. $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
  578. if (!$this->params['dry-run']) {
  579. if ($this->params['git']) {
  580. exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
  581. exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
  582. } else {
  583. $Folder = new Folder($old);
  584. $Folder->move($new);
  585. }
  586. }
  587. }
  588. }
  589. /**
  590. * Move the AppController, and AppModel classes.
  591. *
  592. * @return void
  593. */
  594. protected function _moveAppClasses() {
  595. $files = array(
  596. APP . 'app_controller.php' => APP . 'Controller' . DS . 'AppController.php',
  597. APP . 'controllers' . DS . 'app_controller.php' => APP . 'Controller' . DS . 'AppController.php',
  598. APP . 'app_model.php' => APP . 'Model' . DS . 'AppModel.php',
  599. APP . 'models' . DS . 'app_model.php' => APP . 'Model' . DS . 'AppModel.php',
  600. );
  601. foreach ($files as $old => $new) {
  602. if (file_exists($old)) {
  603. $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
  604. if ($this->params['dry-run']) {
  605. continue;
  606. }
  607. if ($this->params['git']) {
  608. exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
  609. exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
  610. } else {
  611. rename($old, $new);
  612. }
  613. }
  614. }
  615. }
  616. /**
  617. * Move application php files to where they now should be
  618. *
  619. * Find all php files in the folder (honoring recursive) and determine where CakePHP expects the file to be
  620. * If the file is not exactly where CakePHP expects it - move it.
  621. *
  622. * @param string $path
  623. * @param array $options array(recursive, checkFolder)
  624. * @return void
  625. */
  626. protected function _movePhpFiles($path, $options) {
  627. if (!is_dir($path)) {
  628. return;
  629. }
  630. $paths = $this->_paths;
  631. $this->_paths = array($path);
  632. $this->_files = array();
  633. if ($options['recursive']) {
  634. $this->_findFiles('php');
  635. } else {
  636. $this->_files = scandir($path);
  637. foreach ($this->_files as $i => $file) {
  638. if (strlen($file) < 5 || substr($file, -4) !== '.php') {
  639. unset($this->_files[$i]);
  640. }
  641. }
  642. }
  643. $cwd = getcwd();
  644. foreach ($this->_files as &$file) {
  645. $file = $cwd . DS . $file;
  646. $contents = file_get_contents($file);
  647. preg_match($options['regex'], $contents, $match);
  648. if (!$match) {
  649. continue;
  650. }
  651. $class = $match[1];
  652. if (substr($class, 0, 3) === 'Dbo') {
  653. $type = 'Dbo';
  654. } else {
  655. preg_match('@([A-Z][^A-Z]*)$@', $class, $match);
  656. if ($match) {
  657. $type = $match[1];
  658. } else {
  659. $type = 'unknown';
  660. }
  661. }
  662. preg_match('@^.*[\\\/]plugins[\\\/](.*?)[\\\/]@', $file, $match);
  663. $base = $cwd . DS;
  664. $plugin = false;
  665. if ($match) {
  666. $base = $match[0];
  667. $plugin = $match[1];
  668. }
  669. if ($options['checkFolder'] && !empty($this->_map[$type])) {
  670. $folder = str_replace('/', DS, $this->_map[$type]);
  671. $new = $base . $folder . DS . $class . '.php';
  672. } else {
  673. $new = dirname($file) . DS . $class . '.php';
  674. }
  675. if ($file === $new) {
  676. continue;
  677. }
  678. $dir = dirname($new);
  679. if (!is_dir($dir)) {
  680. new Folder($dir, true);
  681. }
  682. $this->out(__d('cake_console', 'Moving %s to %s', $file, $new), 1, Shell::VERBOSE);
  683. if (!$this->params['dry-run']) {
  684. if ($this->params['git']) {
  685. exec('git mv -f ' . escapeshellarg($file) . ' ' . escapeshellarg($file . '__'));
  686. exec('git mv -f ' . escapeshellarg($file . '__') . ' ' . escapeshellarg($new));
  687. } else {
  688. rename($file, $new);
  689. }
  690. }
  691. }
  692. $this->_paths = $paths;
  693. }
  694. /**
  695. * Updates files based on regular expressions.
  696. *
  697. * @param array $patterns Array of search and replacement patterns.
  698. * @return void
  699. */
  700. protected function _filesRegexpUpdate($patterns) {
  701. $this->_findFiles($this->params['ext']);
  702. foreach ($this->_files as $file) {
  703. $this->out(__d('cake_console', 'Updating %s...', $file), 1, Shell::VERBOSE);
  704. $this->_updateFile($file, $patterns);
  705. }
  706. }
  707. /**
  708. * Searches the paths and finds files based on extension.
  709. *
  710. * @param string $extensions
  711. * @return void
  712. */
  713. protected function _findFiles($extensions = '') {
  714. $this->_files = array();
  715. foreach ($this->_paths as $path) {
  716. if (!is_dir($path)) {
  717. continue;
  718. }
  719. $Iterator = new RegexIterator(
  720. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
  721. '/^.+\.(' . $extensions . ')$/i',
  722. RegexIterator::MATCH
  723. );
  724. foreach ($Iterator as $file) {
  725. if ($file->isFile()) {
  726. $this->_files[] = $file->getPathname();
  727. }
  728. }
  729. }
  730. }
  731. /**
  732. * Update a single file.
  733. *
  734. * @param string $file The file to update
  735. * @param array $patterns The replacement patterns to run.
  736. * @return void
  737. */
  738. protected function _updateFile($file, $patterns) {
  739. $contents = file_get_contents($file);
  740. foreach ($patterns as $pattern) {
  741. $this->out(__d('cake_console', ' * Updating %s', $pattern[0]), 1, Shell::VERBOSE);
  742. $contents = preg_replace($pattern[1], $pattern[2], $contents);
  743. }
  744. $this->out(__d('cake_console', 'Done updating %s', $file), 1);
  745. if (!$this->params['dry-run']) {
  746. file_put_contents($file, $contents);
  747. }
  748. }
  749. /**
  750. * Gets the option parser instance and configures it.
  751. *
  752. * @return ConsoleOptionParser
  753. */
  754. public function getOptionParser() {
  755. $parser = parent::getOptionParser();
  756. $subcommandParser = array(
  757. 'options' => array(
  758. 'plugin' => array(
  759. 'short' => 'p',
  760. 'help' => __d('cake_console', 'The plugin to update. Only the specified plugin will be updated.')
  761. ),
  762. 'ext' => array(
  763. 'short' => 'e',
  764. 'help' => __d('cake_console', 'The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
  765. 'default' => 'php|ctp|thtml|inc|tpl'
  766. ),
  767. 'git' => array(
  768. 'short' => 'g',
  769. 'help' => __d('cake_console', 'Use git command for moving files around.'),
  770. 'boolean' => true
  771. ),
  772. 'dry-run' => array(
  773. 'short' => 'd',
  774. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  775. 'boolean' => true
  776. )
  777. )
  778. );
  779. $parser->description(
  780. __d('cake_console', "A shell to help automate upgrading from CakePHP 1.3 to 2.0. \n" .
  781. "Be sure to have a backup of your application before running these commands."
  782. ))->addSubcommand('all', array(
  783. 'help' => __d('cake_console', 'Run all upgrade commands.'),
  784. 'parser' => $subcommandParser
  785. ))->addSubcommand('tests', array(
  786. 'help' => __d('cake_console', 'Update tests class names to FooTest rather than FooTestCase.'),
  787. 'parser' => $subcommandParser
  788. ))->addSubcommand('locations', array(
  789. 'help' => __d('cake_console', 'Move files and folders to their new homes.'),
  790. 'parser' => $subcommandParser
  791. ))->addSubcommand('i18n', array(
  792. 'help' => __d('cake_console', 'Update the i18n translation method calls.'),
  793. 'parser' => $subcommandParser
  794. ))->addSubcommand('helpers', array(
  795. 'help' => __d('cake_console', 'Update calls to helpers.'),
  796. 'parser' => $subcommandParser
  797. ))->addSubcommand('basics', array(
  798. 'help' => __d('cake_console', 'Update removed basics functions to PHP native functions.'),
  799. 'parser' => $subcommandParser
  800. ))->addSubcommand('request', array(
  801. 'help' => __d('cake_console', 'Update removed request access, and replace with $this->request.'),
  802. 'parser' => $subcommandParser
  803. ))->addSubcommand('configure', array(
  804. 'help' => __d('cake_console', "Update Configure::read() to Configure::read('debug')"),
  805. 'parser' => $subcommandParser
  806. ))->addSubcommand('constants', array(
  807. 'help' => __d('cake_console', "Replace Obsolete constants"),
  808. 'parser' => $subcommandParser
  809. ))->addSubcommand('controller_redirects', array(
  810. 'help' => __d('cake_console', 'Return early on controller redirect calls.'),
  811. 'parser' => $subcommandParser
  812. ))->addSubcommand('components', array(
  813. 'help' => __d('cake_console', 'Update components to extend Component class.'),
  814. 'parser' => $subcommandParser
  815. ))->addSubcommand('exceptions', array(
  816. 'help' => __d('cake_console', 'Replace use of cakeError with exceptions.'),
  817. 'parser' => $subcommandParser
  818. ));
  819. return $parser;
  820. }
  821. }