UpgradeShell.php 23 KB

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