UpgradeShell.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 += $defaultOptions;
  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. *
  525. * @return void
  526. */
  527. public function exceptions() {
  528. $controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
  529. $components = array_diff(App::path('components'), App::core('components'));
  530. $this->_paths = array_merge($controllers, $components);
  531. if (!empty($this->params['plugin'])) {
  532. $pluginPath = App::pluginPath($this->params['plugin']);
  533. $this->_paths = array(
  534. $pluginPath . 'controllers' . DS,
  535. $pluginPath . 'controllers' . DS . 'components' . DS,
  536. );
  537. }
  538. $patterns = array(
  539. array(
  540. '$this->cakeError("error400") -> throw new BadRequestException()',
  541. '/(\$this->cakeError\(["\']error400["\']\));/',
  542. 'throw new BadRequestException();'
  543. ),
  544. array(
  545. '$this->cakeError("error404") -> throw new NotFoundException()',
  546. '/(\$this->cakeError\(["\']error404["\']\));/',
  547. 'throw new NotFoundException();'
  548. ),
  549. array(
  550. '$this->cakeError("error500") -> throw new InternalErrorException()',
  551. '/(\$this->cakeError\(["\']error500["\']\));/',
  552. 'throw new InternalErrorException();'
  553. ),
  554. );
  555. $this->_filesRegexpUpdate($patterns);
  556. }
  557. /**
  558. * Move application views files to where they now should be
  559. *
  560. * Find all view files in the folder and determine where cake expects the file to be
  561. *
  562. * @return void
  563. */
  564. protected function _moveViewFiles() {
  565. if (!is_dir('View')) {
  566. return;
  567. }
  568. $dirs = scandir('View');
  569. foreach ($dirs as $old) {
  570. if (!is_dir('View' . DS . $old) || $old === '.' || $old === '..') {
  571. continue;
  572. }
  573. $new = 'View' . DS . Inflector::camelize($old);
  574. $old = 'View' . DS . $old;
  575. if ($new === $old) {
  576. continue;
  577. }
  578. $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
  579. if (!$this->params['dry-run']) {
  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. $Folder = new Folder($old);
  585. $Folder->move($new);
  586. }
  587. }
  588. }
  589. }
  590. /**
  591. * Move the AppController, and AppModel classes.
  592. *
  593. * @return void
  594. */
  595. protected function _moveAppClasses() {
  596. $files = array(
  597. APP . 'app_controller.php' => APP . 'Controller' . DS . 'AppController.php',
  598. APP . 'controllers' . DS . 'app_controller.php' => APP . 'Controller' . DS . 'AppController.php',
  599. APP . 'app_model.php' => APP . 'Model' . DS . 'AppModel.php',
  600. APP . 'models' . DS . 'app_model.php' => APP . 'Model' . DS . 'AppModel.php',
  601. );
  602. foreach ($files as $old => $new) {
  603. if (file_exists($old)) {
  604. $this->out(__d('cake_console', 'Moving %s to %s', $old, $new));
  605. if ($this->params['dry-run']) {
  606. continue;
  607. }
  608. if ($this->params['git']) {
  609. exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
  610. exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
  611. } else {
  612. rename($old, $new);
  613. }
  614. }
  615. }
  616. }
  617. /**
  618. * Move application php files to where they now should be
  619. *
  620. * Find all php files in the folder (honoring recursive) and determine where CakePHP expects the file to be
  621. * If the file is not exactly where CakePHP expects it - move it.
  622. *
  623. * @param string $path The path to move files in.
  624. * @param array $options array(recursive, checkFolder)
  625. * @return void
  626. */
  627. protected function _movePhpFiles($path, $options) {
  628. if (!is_dir($path)) {
  629. return;
  630. }
  631. $paths = $this->_paths;
  632. $this->_paths = array($path);
  633. $this->_files = array();
  634. if ($options['recursive']) {
  635. $this->_findFiles('php');
  636. } else {
  637. $this->_files = scandir($path);
  638. foreach ($this->_files as $i => $file) {
  639. if (strlen($file) < 5 || substr($file, -4) !== '.php') {
  640. unset($this->_files[$i]);
  641. }
  642. }
  643. }
  644. $cwd = getcwd();
  645. foreach ($this->_files as &$file) {
  646. $file = $cwd . DS . $file;
  647. $contents = file_get_contents($file);
  648. preg_match($options['regex'], $contents, $match);
  649. if (!$match) {
  650. continue;
  651. }
  652. $class = $match[1];
  653. if (substr($class, 0, 3) === 'Dbo') {
  654. $type = 'Dbo';
  655. } else {
  656. preg_match('@([A-Z][^A-Z]*)$@', $class, $match);
  657. if ($match) {
  658. $type = $match[1];
  659. } else {
  660. $type = 'unknown';
  661. }
  662. }
  663. preg_match('@^.*[\\\/]plugins[\\\/](.*?)[\\\/]@', $file, $match);
  664. $base = $cwd . DS;
  665. $plugin = false;
  666. if ($match) {
  667. $base = $match[0];
  668. $plugin = $match[1];
  669. }
  670. if ($options['checkFolder'] && !empty($this->_map[$type])) {
  671. $folder = str_replace('/', DS, $this->_map[$type]);
  672. $new = $base . $folder . DS . $class . '.php';
  673. } else {
  674. $new = dirname($file) . DS . $class . '.php';
  675. }
  676. if ($file === $new) {
  677. continue;
  678. }
  679. $dir = dirname($new);
  680. if (!is_dir($dir)) {
  681. new Folder($dir, true);
  682. }
  683. $this->out(__d('cake_console', 'Moving %s to %s', $file, $new), 1, Shell::VERBOSE);
  684. if (!$this->params['dry-run']) {
  685. if ($this->params['git']) {
  686. exec('git mv -f ' . escapeshellarg($file) . ' ' . escapeshellarg($file . '__'));
  687. exec('git mv -f ' . escapeshellarg($file . '__') . ' ' . escapeshellarg($new));
  688. } else {
  689. rename($file, $new);
  690. }
  691. }
  692. }
  693. $this->_paths = $paths;
  694. }
  695. /**
  696. * Updates files based on regular expressions.
  697. *
  698. * @param array $patterns Array of search and replacement patterns.
  699. * @return void
  700. */
  701. protected function _filesRegexpUpdate($patterns) {
  702. $this->_findFiles($this->params['ext']);
  703. foreach ($this->_files as $file) {
  704. $this->out(__d('cake_console', 'Updating %s...', $file), 1, Shell::VERBOSE);
  705. $this->_updateFile($file, $patterns);
  706. }
  707. }
  708. /**
  709. * Searches the paths and finds files based on extension.
  710. *
  711. * @param string $extensions The extensions to include. Defaults to none.
  712. * @return void
  713. */
  714. protected function _findFiles($extensions = '') {
  715. $this->_files = array();
  716. foreach ($this->_paths as $path) {
  717. if (!is_dir($path)) {
  718. continue;
  719. }
  720. $Iterator = new RegexIterator(
  721. new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
  722. '/^.+\.(' . $extensions . ')$/i',
  723. RegexIterator::MATCH
  724. );
  725. foreach ($Iterator as $file) {
  726. if ($file->isFile()) {
  727. $this->_files[] = $file->getPathname();
  728. }
  729. }
  730. }
  731. }
  732. /**
  733. * Update a single file.
  734. *
  735. * @param string $file The file to update
  736. * @param array $patterns The replacement patterns to run.
  737. * @return void
  738. */
  739. protected function _updateFile($file, $patterns) {
  740. $contents = file_get_contents($file);
  741. foreach ($patterns as $pattern) {
  742. $this->out(__d('cake_console', ' * Updating %s', $pattern[0]), 1, Shell::VERBOSE);
  743. $contents = preg_replace($pattern[1], $pattern[2], $contents);
  744. }
  745. $this->out(__d('cake_console', 'Done updating %s', $file), 1);
  746. if (!$this->params['dry-run']) {
  747. file_put_contents($file, $contents);
  748. }
  749. }
  750. /**
  751. * Gets the option parser instance and configures it.
  752. *
  753. * @return ConsoleOptionParser
  754. */
  755. public function getOptionParser() {
  756. $parser = parent::getOptionParser();
  757. $subcommandParser = array(
  758. 'options' => array(
  759. 'plugin' => array(
  760. 'short' => 'p',
  761. 'help' => __d('cake_console', 'The plugin to update. Only the specified plugin will be updated.')
  762. ),
  763. 'ext' => array(
  764. 'short' => 'e',
  765. 'help' => __d('cake_console', 'The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
  766. 'default' => 'php|ctp|thtml|inc|tpl'
  767. ),
  768. 'git' => array(
  769. 'short' => 'g',
  770. 'help' => __d('cake_console', 'Use git command for moving files around.'),
  771. 'boolean' => true
  772. ),
  773. 'dry-run' => array(
  774. 'short' => 'd',
  775. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  776. 'boolean' => true
  777. )
  778. )
  779. );
  780. $parser->description(
  781. __d('cake_console', "A tool to help automate upgrading an application or plugin " .
  782. "from CakePHP 1.3 to 2.0. Be sure to have a backup of your application before " .
  783. "running these commands."
  784. ))->addSubcommand('all', array(
  785. 'help' => __d('cake_console', 'Run all upgrade commands.'),
  786. 'parser' => $subcommandParser
  787. ))->addSubcommand('tests', array(
  788. 'help' => __d('cake_console', 'Update tests class names to FooTest rather than FooTestCase.'),
  789. 'parser' => $subcommandParser
  790. ))->addSubcommand('locations', array(
  791. 'help' => __d('cake_console', 'Move files and folders to their new homes.'),
  792. 'parser' => $subcommandParser
  793. ))->addSubcommand('i18n', array(
  794. 'help' => __d('cake_console', 'Update the i18n translation method calls.'),
  795. 'parser' => $subcommandParser
  796. ))->addSubcommand('helpers', array(
  797. 'help' => __d('cake_console', 'Update calls to helpers.'),
  798. 'parser' => $subcommandParser
  799. ))->addSubcommand('basics', array(
  800. 'help' => __d('cake_console', 'Update removed basics functions to PHP native functions.'),
  801. 'parser' => $subcommandParser
  802. ))->addSubcommand('request', array(
  803. 'help' => __d('cake_console', 'Update removed request access, and replace with $this->request.'),
  804. 'parser' => $subcommandParser
  805. ))->addSubcommand('configure', array(
  806. 'help' => __d('cake_console', "Update Configure::read() to Configure::read('debug')"),
  807. 'parser' => $subcommandParser
  808. ))->addSubcommand('constants', array(
  809. 'help' => __d('cake_console', "Replace Obsolete constants"),
  810. 'parser' => $subcommandParser
  811. ))->addSubcommand('controller_redirects', array(
  812. 'help' => __d('cake_console', 'Return early on controller redirect calls.'),
  813. 'parser' => $subcommandParser
  814. ))->addSubcommand('components', array(
  815. 'help' => __d('cake_console', 'Update components to extend Component class.'),
  816. 'parser' => $subcommandParser
  817. ))->addSubcommand('exceptions', array(
  818. 'help' => __d('cake_console', 'Replace use of cakeError with exceptions.'),
  819. 'parser' => $subcommandParser
  820. ));
  821. return $parser;
  822. }
  823. }