UpgradeShell.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since CakePHP(tm) v 2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console\Command;
  16. use Cake\Console\Shell;
  17. use Cake\Core\App;
  18. use Cake\Core\Plugin;
  19. use Cake\Utility\Folder;
  20. use Cake\Utility\Inflector;
  21. /**
  22. * A shell class to help developers upgrade applications to CakePHP 3.0
  23. *
  24. */
  25. class UpgradeShell extends Shell {
  26. /**
  27. * Files
  28. *
  29. * @var array
  30. */
  31. protected $_files = [];
  32. /**
  33. * Paths
  34. *
  35. * @var array
  36. */
  37. protected $_paths = [];
  38. /**
  39. * Shell startup, prints info message about dry run.
  40. *
  41. * @return void
  42. */
  43. public function startup() {
  44. parent::startup();
  45. if ($this->params['dryRun']) {
  46. $this->out(__d('cake_console', '<warning>Dry-run mode enabled!</warning>'), 1, Shell::QUIET);
  47. }
  48. }
  49. /**
  50. * Run all upgrade steps one at a time.
  51. *
  52. * @return void
  53. */
  54. public function all() {
  55. foreach ($this->OptionParser->subcommands() as $command) {
  56. $name = $command->name();
  57. if ($name === 'all') {
  58. continue;
  59. }
  60. $this->out(__d('cake_console', 'Running %s', $name));
  61. $this->$name();
  62. }
  63. }
  64. /**
  65. * Move files and folders to their new homes.
  66. *
  67. * @return void
  68. */
  69. public function locations() {
  70. $path = $this->_getPath();
  71. $moves = array(
  72. 'Test' . DS . 'Case' => 'Test' . DS . 'TestCase',
  73. 'View' . DS . 'Elements' => 'View' . DS . 'Element',
  74. 'View' . DS . 'Emails' => 'View' . DS . 'Email',
  75. 'View' . DS . 'Layouts' => 'View' . DS . 'Layout',
  76. 'View' . DS . 'Layout' . DS . 'Emails' => 'View' . DS . 'Layout' . DS . 'Email',
  77. 'View' . DS . 'Scaffolds' => 'View' . DS . 'Scaffold',
  78. 'View' . DS . 'Errors' => 'View' . DS . 'Error',
  79. );
  80. $dry = $this->params['dryRun'];
  81. foreach ($moves as $old => $new) {
  82. $old = $path . DS . $old;
  83. $new = $path . DS . $new;
  84. if (!is_dir($old)) {
  85. continue;
  86. }
  87. $this->out(__d('cake_console', '<info>Moving %s to %s</info>', $old, $new));
  88. if ($dry) {
  89. continue;
  90. }
  91. if ($this->params['git']) {
  92. exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($old . '__'));
  93. exec('git mv -f ' . escapeshellarg($old . '__') . ' ' . escapeshellarg($new));
  94. } else {
  95. $Folder = new Folder($old);
  96. $Folder->move($new);
  97. }
  98. }
  99. }
  100. /**
  101. * Rename classes that have moved during 3.0
  102. *
  103. * @return void
  104. */
  105. public function rename_classes() {
  106. $path = $this->_getPath();
  107. $Folder = new Folder($path);
  108. $this->_paths = $Folder->tree(null, false, 'dir');
  109. $this->_findFiles('php');
  110. foreach ($this->_files as $filePath) {
  111. $this->_renameClasses($filePath, $this->params['dryRun']);
  112. }
  113. $this->out(__d('cake_console', '<success>Class names updated.</success>'));
  114. }
  115. /**
  116. * Rename the classes in a given file.
  117. *
  118. * @param string $path The path to operate on.
  119. * @param boolean $dryRun Whether or not dry run is on.
  120. * @return void
  121. */
  122. protected function _renameClasses($path, $dryRun) {
  123. $replacements = [
  124. 'Cake\Network\Http\HttpSocket' => 'Cake\Network\Http\Client',
  125. 'HttpSocket' => 'Client',
  126. 'Cake\Model\ConnectionManager' => 'Cake\Database\ConnectionManager',
  127. 'Cake\Configure\ConfigReaderInterface' => 'Cake\Configure\ConfigEngineInterface',
  128. 'ConfigReaderInterface' => 'ConfigEngineInterface',
  129. 'Cake\Configure\PhpReader' => 'Cake\Configure\Engine\PhpConfig',
  130. 'PhpReader' => 'PhpConfig',
  131. 'Cake\Configure\IniReader' => 'Cake\Configure\Engine\IniConfig',
  132. 'IniReader' => 'IniConfig',
  133. ];
  134. $contents = file_get_contents($path);
  135. $contents = str_replace(
  136. array_keys($replacements),
  137. array_values($replacements),
  138. $contents,
  139. $count
  140. );
  141. if ($count === 0) {
  142. $this->out(
  143. __d('cake_console', '<info>Skip %s as there are no renames to do.</info>', $path),
  144. 1,
  145. Shell::VERBOSE
  146. );
  147. return;
  148. }
  149. $this->_saveFile($path, $contents);
  150. }
  151. /**
  152. * Save a file conditionally depending on dryRun flag.
  153. *
  154. * @param string $path The path to update.
  155. * @param string $contents The contents to put in the file.
  156. * @return boolean
  157. */
  158. protected function _saveFile($path, $contents) {
  159. $result = true;
  160. if (!$this->params['dryRun']) {
  161. $result = file_put_contents($path, $contents);
  162. }
  163. if ($result) {
  164. $this->out(__d('cake_console', '<success>Done updating %s</success>', $path), 1);
  165. return;
  166. }
  167. $this->err(__d(
  168. 'cake_console',
  169. '<error>Error</error> Was unable to update %s',
  170. $path
  171. ));
  172. return $result;
  173. }
  174. /**
  175. * Convert App::uses() to normal use statements.
  176. *
  177. * @return void
  178. */
  179. public function app_uses() {
  180. $path = $this->_getPath();
  181. $Folder = new Folder($path);
  182. $this->_paths = $Folder->tree(null, false, 'dir');
  183. $this->_findFiles('php');
  184. foreach ($this->_files as $filePath) {
  185. $this->_replaceUses($filePath, $this->params['dryRun']);
  186. }
  187. $this->out(__d('cake_console', '<success>App::uses() replaced successfully</success>'));
  188. }
  189. /**
  190. * Replace all the App::uses() calls with `use`.
  191. *
  192. * @param string $file The file to search and replace.
  193. * @param boolean $dryRun Whether or not to do the thing.
  194. */
  195. protected function _replaceUses($file) {
  196. $pattern = '#App::uses\([\'"]([a-z0-9_]+)[\'"],\s*[\'"]([a-z0-9/_]+)(?:\.([a-z0-9/_]+))?[\'"]\)#i';
  197. $contents = file_get_contents($file);
  198. $self = $this;
  199. $replacement = function ($matches) use ($file) {
  200. $matches = $this->_mapClassName($matches);
  201. if (count($matches) === 4) {
  202. $use = $matches[3] . '\\' . $matches[2] . '\\' . $matches[1];
  203. } elseif ($matches[2] == 'Vendor') {
  204. $this->out(
  205. __d('cake_console', '<info>Skip %s as it is a vendor library.</info>', $matches[1]),
  206. 1,
  207. Shell::VERBOSE
  208. );
  209. return $matches[0];
  210. } else {
  211. $use = 'Cake\\' . str_replace('/', '\\', $matches[2]) . '\\' . $matches[1];
  212. }
  213. if (!class_exists($use)) {
  214. $use = 'App\\' . substr($use, 5);
  215. }
  216. return 'use ' . $use;
  217. };
  218. $contents = preg_replace_callback($pattern, $replacement, $contents, -1, $count);
  219. if (!$count) {
  220. $this->out(
  221. __d('cake_console', '<info>Skip %s as there are no App::uses()</info>', $file),
  222. 1,
  223. Shell::VERBOSE
  224. );
  225. return;
  226. }
  227. $this->out(__d('cake_console', '<info> * Updating App::uses()</info>'), 1, Shell::VERBOSE);
  228. $this->_saveFile($file, $contents);
  229. }
  230. /**
  231. * Convert old classnames to new ones.
  232. * Strips the Cake prefix off of classes that no longer have it.
  233. *
  234. * @param array $matches
  235. */
  236. protected function _mapClassName($matches) {
  237. $rename = [
  238. 'CakePlugin',
  239. 'CakeEvent',
  240. 'CakeEventListener',
  241. 'CakeEventManager',
  242. 'CakeValidationRule',
  243. 'CakeSocket',
  244. 'CakeRoute',
  245. 'CakeRequest',
  246. 'CakeResponse',
  247. 'CakeSession',
  248. 'CakeLog',
  249. 'CakeNumber',
  250. 'CakeTime',
  251. 'CakeEmail',
  252. 'CakeLogInterface',
  253. 'CakeSessionHandlerInterface',
  254. ];
  255. if (empty($matches[3])) {
  256. unset($matches[3]);
  257. }
  258. if (in_array($matches[1], $rename)) {
  259. $matches[1] = substr($matches[1], 4);
  260. }
  261. return $matches;
  262. }
  263. /**
  264. * Add namespaces to files.
  265. *
  266. * @return void
  267. */
  268. public function namespaces() {
  269. $path = $this->_getPath();
  270. $ns = $this->params['namespace'];
  271. if ($ns === 'App' && isset($this->params['plugin'])) {
  272. $ns = Inflector::camelize($this->params['plugin']);
  273. }
  274. $Folder = new Folder($path);
  275. $exclude = ['vendor', 'Vendor', 'webroot', 'Plugin', 'tmp'];
  276. if (!empty($this->params['exclude'])) {
  277. $exclude = array_merge($exclude, explode(',', $this->params['exclude']));
  278. }
  279. list($dirs, $files) = $Folder->read(true, true, true);
  280. $this->_paths = $this->_filterPaths($dirs, $exclude);
  281. $this->_findFiles('php', ['index.php', 'test.php', 'cake.php']);
  282. foreach ($this->_files as $filePath) {
  283. $this->_addNamespace($path, $filePath, $ns, $this->params['dryRun']);
  284. }
  285. $this->out(__d('cake_console', '<success>Namespaces added successfully</success>'));
  286. }
  287. /**
  288. * Update fixtures
  289. *
  290. * @return void
  291. */
  292. public function fixtures() {
  293. $path = $this->_getPath();
  294. $app = rtrim(APP, DS);
  295. if ($path === $app || !empty($this->params['plugin'])) {
  296. $path .= DS . 'Test' . DS . 'Fixture' . DS;
  297. }
  298. $this->out(__d('cake_console', 'Processing fixtures on %s', $path));
  299. $this->_paths[] = realpath($path);
  300. $this->_findFiles('php');
  301. foreach ($this->_files as $file) {
  302. $this->out(__d('cake_console', 'Updating %s...', $file), 1, Shell::VERBOSE);
  303. $content = $this->_processFixture(file_get_contents($file));
  304. $this->_saveFile($file, $content);
  305. }
  306. }
  307. /**
  308. * Process fixture content and update it for 3.x
  309. *
  310. * @param string $content Fixture content.
  311. * @return string
  312. */
  313. protected function _processFixture($content) {
  314. // Serializes data from PHP data into PHP code.
  315. // Basically a code style conformant version of var_export()
  316. $export = function ($values) use (&$export) {
  317. $vals = [];
  318. if (!is_array($values)) {
  319. return $vals;
  320. }
  321. foreach ($values as $key => $val) {
  322. if (is_array($val)) {
  323. $vals[] = "'{$key}' => [" . implode(', ', $export($val)) . ']';
  324. } else {
  325. $val = var_export($val, true);
  326. if ($val === 'NULL') {
  327. $val = 'null';
  328. }
  329. if (!is_numeric($key)) {
  330. $vals[] = "'{$key}' => {$val}";
  331. } else {
  332. $vals[] = "{$val}";
  333. }
  334. }
  335. }
  336. return $vals;
  337. };
  338. // Process field property.
  339. $processor = function ($matches) use ($export) {
  340. eval('$data = [' . $matches[2] . '];');
  341. $constraints = [];
  342. $out = [];
  343. foreach ($data as $field => $properties) {
  344. // Move primary key into a constraint
  345. if (isset($properties['key']) && $properties['key'] === 'primary') {
  346. $constraints['primary'] = [
  347. 'type' => 'primary',
  348. 'columns' => [$field]
  349. ];
  350. }
  351. if (isset($properties['key'])) {
  352. unset($properties['key']);
  353. }
  354. if ($field !== 'indexes' && $field !== 'tableParameters') {
  355. $out[$field] = $properties;
  356. }
  357. }
  358. // Process indexes. Unique keys work differently now.
  359. if (isset($data['indexes'])) {
  360. foreach ($data['indexes'] as $index => $indexProps) {
  361. if (isset($indexProps['column'])) {
  362. $indexProps['columns'] = $indexProps['column'];
  363. unset($indexProps['column']);
  364. }
  365. // Move unique indexes over
  366. if (!empty($indexProps['unique'])) {
  367. unset($indexProps['unique']);
  368. $constraints[$index] = ['type' => 'unique'] + $indexProps;
  369. continue;
  370. }
  371. $out['_indexes'][$index] = $indexProps;
  372. }
  373. }
  374. if (count($constraints)) {
  375. $out['_constraints'] = $constraints;
  376. }
  377. // Process table parameters
  378. if (isset($data['tableParameters'])) {
  379. $out['_options'] = $data['tableParameters'];
  380. }
  381. return $matches[1] . "\n\t\t" . implode(",\n\t\t", $export($out)) . "\n\t" . $matches[3];
  382. };
  383. $content = preg_replace_callback(
  384. '/(public \$fields\s+=\s+(?:array\(|\[))(.*?)(\);|\];)/ms',
  385. $processor,
  386. $content,
  387. -1,
  388. $count
  389. );
  390. if ($count) {
  391. $this->out(__d('cake_console', 'Updated $fields property'), 1, Shell::VERBOSE);
  392. }
  393. return $content;
  394. }
  395. /**
  396. * Rename collection classes
  397. *
  398. * @return void
  399. */
  400. public function rename_collections() {
  401. $path = $this->_getPath();
  402. $Folder = new Folder($path);
  403. $this->_paths = $Folder->tree(null, false, 'dir');
  404. $this->_findFiles('php');
  405. foreach ($this->_files as $filePath) {
  406. $patterns = [
  407. [
  408. 'Replace $this->_Collection with $this->_registry',
  409. '#\$this->_Collection#',
  410. '$this->_registry',
  411. ],
  412. [
  413. 'Replace ComponentCollection arguments',
  414. '#ComponentCollection\s+\$collection#',
  415. 'ComponentRegistry $registry',
  416. ],
  417. [
  418. 'Rename ComponentCollection',
  419. '#ComponentCollection#',
  420. 'ComponentRegistry',
  421. ],
  422. [
  423. 'Rename HelperCollection',
  424. '#HelperCollection#',
  425. 'HelperRegistry',
  426. ],
  427. [
  428. 'Rename TaskCollection',
  429. '#TaskCollection#',
  430. 'TaskRegistry',
  431. ],
  432. ];
  433. $this->_updateFileRegexp($filePath, $patterns);
  434. }
  435. $this->out(__d('cake_console', '<success>Collection class uses renamed successfully.</success>'));
  436. }
  437. /**
  438. * Update test case assertion methods.
  439. *
  440. * @return void
  441. */
  442. public function tests() {
  443. $path = $this->_getPath();
  444. $Folder = new Folder($path);
  445. $this->_paths = $Folder->tree(null, false, 'dir');
  446. $this->_findFiles('php');
  447. foreach ($this->_files as $filePath) {
  448. $patterns = [
  449. [
  450. 'Replace assertEqual() with assertEquals()',
  451. '#\$this-\>assertEqual\(\#i',
  452. '$this->assertEquals(',
  453. ],
  454. [
  455. 'Replace assertNotEqual() with assertNotEquals()',
  456. '#\$this-\>assertNotEqual\(\#i',
  457. '$this->assertNotEquals(',
  458. ],
  459. [
  460. 'Replace assertIdentical() with assertSame()',
  461. '#\$this-\>assertIdentical\(\#i',
  462. '$this->assertSame(',
  463. ],
  464. [
  465. 'Replace assertNotIdentical() with assertNotSame()',
  466. '#\$this-\>assertNotIdentical\(\#i',
  467. '$this->assertNotSame(',
  468. ],
  469. [
  470. 'Replace assertPattern() with assertRegExp()',
  471. '#\$this-\>assertPattern\(\#i',
  472. '$this->assertRegExp(',
  473. ],
  474. [
  475. 'Replace assertNoPattern() with assertNotRegExp()',
  476. '#\$this-\>assertNoPattern\(\#i',
  477. '$this->assertNotRegExp(',
  478. ],
  479. [
  480. 'Replace assertReference() with assertSame()',
  481. '#\$this-\>assertReference\(\$(.*?),\s*\'(.*?)\'\)#i',
  482. '$this->assertSame($\1, $\2)',
  483. ],
  484. [
  485. 'Replace assertIsA() with assertInstanceOf()',
  486. '#\$this-\>assertIsA\(\$(.*?),\s*\'(.*?)\'\)#i',
  487. '$this->assertInstanceOf(\'\2\', $\1)',
  488. ],
  489. [
  490. 'Replace assert*($is, $expected) with assert*($expected, $is) - except for assertTags()',
  491. '/\bassert((?!tags)\w+)\(\$(\w+),\s*\$expected\)/i',
  492. 'assert\1($expected, $\2)'
  493. ]
  494. ];
  495. $this->_updateFileRegexp($filePath, $patterns);
  496. }
  497. $this->out(__d('cake_console', '<success>Assertion methods renamed successfully.</success>'));
  498. }
  499. /**
  500. * Filter paths to remove webroot, Plugin, tmp directories.
  501. *
  502. * @return array
  503. */
  504. protected function _filterPaths($paths, $directories) {
  505. return array_filter($paths, function ($path) use ($directories) {
  506. foreach ($directories as $dir) {
  507. if (strpos($path, DS . $dir) !== false) {
  508. return false;
  509. }
  510. }
  511. return true;
  512. });
  513. }
  514. /**
  515. * Adds the namespace to a given file.
  516. *
  517. * @param string $filePath The file to add a namespace to.
  518. * @param string $ns The base namespace to use.
  519. * @param boolean $dry Whether or not to operate in dry-run mode.
  520. * @return void
  521. */
  522. protected function _addNamespace($path, $filePath, $ns, $dry) {
  523. $result = true;
  524. $shortPath = str_replace($path, '', $filePath);
  525. $contents = file_get_contents($filePath);
  526. if (preg_match('/namespace\s+[a-z0-9\\\]+;/', $contents)) {
  527. $this->out(__d(
  528. 'cake_console',
  529. '<warning>Skipping %s as it already has a namespace.</warning>',
  530. $shortPath
  531. ));
  532. return;
  533. }
  534. $namespace = trim($ns . str_replace(DS, '\\', dirname($shortPath)), '\\');
  535. $patterns = [
  536. [
  537. 'namespace to ' . $namespace,
  538. '#^(<\?(?:php)?\s+(?:\/\*.*?\*\/\s{0,1})?)#s',
  539. "\\1namespace " . $namespace . ";\n",
  540. ]
  541. ];
  542. $this->_updateFileRegexp($filePath, $patterns);
  543. }
  544. /**
  545. * Updates files based on regular expressions.
  546. *
  547. * @param array $patterns Array of search and replacement patterns.
  548. * @return void
  549. */
  550. protected function _filesRegexpUpdate($patterns) {
  551. $this->_findFiles($this->params['ext']);
  552. foreach ($this->_files as $file) {
  553. $this->out(__d('cake_console', 'Updating %s...', $file), 1, Shell::VERBOSE);
  554. $this->_updateFileRegexp($file, $patterns);
  555. }
  556. }
  557. /**
  558. * Searches the paths and finds files based on extension.
  559. *
  560. * @param string $extensions
  561. * @return void
  562. */
  563. protected function _findFiles($extensions = '', $exclude = []) {
  564. $this->_files = array();
  565. foreach ($this->_paths as $path) {
  566. if (!is_dir($path)) {
  567. continue;
  568. }
  569. $Iterator = new \RegexIterator(
  570. new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)),
  571. '/^.+\.(' . $extensions . ')$/i',
  572. \RegexIterator::MATCH
  573. );
  574. foreach ($Iterator as $file) {
  575. if ($file->isFile() && !in_array($file->getFilename(), $exclude)) {
  576. $this->_files[] = $file->getPathname();
  577. }
  578. }
  579. }
  580. }
  581. /**
  582. * Update a single file with an number of pcre pattern replacements.
  583. *
  584. * @param string $file The file to update
  585. * @param array $patterns The replacement patterns to run.
  586. * @return void
  587. */
  588. protected function _updateFileRegexp($file, $patterns) {
  589. $contents = file_get_contents($file);
  590. foreach ($patterns as $pattern) {
  591. $this->out(__d('cake_console', '<info> * Updating %s</info>', $pattern[0]), 1, Shell::VERBOSE);
  592. $contents = preg_replace($pattern[1], $pattern[2], $contents);
  593. }
  594. $this->_saveFile($file, $contents);
  595. }
  596. /**
  597. * Get the path to operate on. Uses either the first argument,
  598. * or the plugin parameter if its set.
  599. *
  600. * @return string
  601. */
  602. protected function _getPath() {
  603. $path = isset($this->args[0]) ? $this->args[0] : APP;
  604. if (isset($this->params['plugin'])) {
  605. $path = Plugin::path($this->params['plugin']);
  606. }
  607. return rtrim($path, DS);
  608. }
  609. /**
  610. * Get the option parser.
  611. *
  612. * @return ConsoleOptionParser
  613. */
  614. public function getOptionParser() {
  615. $plugin = [
  616. 'short' => 'p',
  617. 'help' => __d('cake_console', 'The plugin to update. Only the specified plugin will be updated.')
  618. ];
  619. $dryRun = [
  620. 'short' => 'd',
  621. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  622. 'boolean' => true
  623. ];
  624. $git = [
  625. 'help' => __d('cake_console', 'Perform git operations. eg. git mv instead of just moving files.'),
  626. 'boolean' => true
  627. ];
  628. $namespace = [
  629. 'help' => __d('cake_console', 'Set the base namespace you want to use. Defaults to App or the plugin name.'),
  630. 'default' => 'App',
  631. ];
  632. $exclude = [
  633. 'help' => __d('cake_console', 'Comma separated list of top level diretories to exclude.'),
  634. 'default' => '',
  635. ];
  636. $path = [
  637. 'help' => __d('cake_console', 'The path to operate on. Will default to APP or the plugin option.'),
  638. 'required' => false,
  639. ];
  640. return parent::getOptionParser()
  641. ->description(__d('cake_console', "A shell to help automate upgrading from CakePHP 3.0 to 2.x. \n" .
  642. "Be sure to have a backup of your application before running these commands."))
  643. ->addSubcommand('all', [
  644. 'help' => __d('cake_console', 'Run all upgrade commands.'),
  645. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  646. ])
  647. ->addSubcommand('locations', [
  648. 'help' => __d('cake_console', 'Move files/directories around. Run this *before* adding namespaces with the namespaces command.'),
  649. 'parser' => ['options' => compact('plugin', 'dryRun', 'git'), 'arguments' => compact('path')]
  650. ])
  651. ->addSubcommand('namespaces', [
  652. 'help' => __d('cake_console', 'Add namespaces to files based on their file path. Only run this *after* you have moved files with locations.'),
  653. 'parser' => ['options' => compact('plugin', 'dryRun', 'namespace', 'exclude'), 'arguments' => compact('path')]
  654. ])
  655. ->addSubcommand('app_uses', [
  656. 'help' => __d('cake_console', 'Replace App::uses() with use statements'),
  657. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  658. ])
  659. ->addSubcommand('rename_classes', [
  660. 'help' => __d('cake_console', 'Rename classes that have been moved/renamed. Run after replacing App::uses().'),
  661. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  662. ])
  663. ->addSubcommand('fixtures', [
  664. 'help' => __d('cake_console', 'Update fixtures to use new index/constraint features. This is necessary before running tests.'),
  665. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')],
  666. ])
  667. ->addSubcommand('rename_collections', [
  668. 'help' => __d('cake_console', 'Rename HelperCollection, ComponentCollection, and TaskCollection. Will also rename component constructor arguments and _Collection properties on all objects.'),
  669. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  670. ])
  671. ->addSubcommand('tests', [
  672. 'help' => __d('cake_console', 'Rename test case assertion methods.'),
  673. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  674. ]);
  675. }
  676. }