UpgradeShell.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. //@codingStandardsIgnoreStart
  341. eval('$data = [' . $matches[2] . '];');
  342. //@codingStandardsIgnoreEnd
  343. $constraints = [];
  344. $out = [];
  345. foreach ($data as $field => $properties) {
  346. // Move primary key into a constraint
  347. if (isset($properties['key']) && $properties['key'] === 'primary') {
  348. $constraints['primary'] = [
  349. 'type' => 'primary',
  350. 'columns' => [$field]
  351. ];
  352. }
  353. if (isset($properties['key'])) {
  354. unset($properties['key']);
  355. }
  356. if ($field !== 'indexes' && $field !== 'tableParameters') {
  357. $out[$field] = $properties;
  358. }
  359. }
  360. // Process indexes. Unique keys work differently now.
  361. if (isset($data['indexes'])) {
  362. foreach ($data['indexes'] as $index => $indexProps) {
  363. if (isset($indexProps['column'])) {
  364. $indexProps['columns'] = $indexProps['column'];
  365. unset($indexProps['column']);
  366. }
  367. // Move unique indexes over
  368. if (!empty($indexProps['unique'])) {
  369. unset($indexProps['unique']);
  370. $constraints[$index] = ['type' => 'unique'] + $indexProps;
  371. continue;
  372. }
  373. $out['_indexes'][$index] = $indexProps;
  374. }
  375. }
  376. if (count($constraints)) {
  377. $out['_constraints'] = $constraints;
  378. }
  379. // Process table parameters
  380. if (isset($data['tableParameters'])) {
  381. $out['_options'] = $data['tableParameters'];
  382. }
  383. return $matches[1] . "\n\t\t" . implode(",\n\t\t", $export($out)) . "\n\t" . $matches[3];
  384. };
  385. $content = preg_replace_callback(
  386. '/(public \$fields\s+=\s+(?:array\(|\[))(.*?)(\);|\];)/ms',
  387. $processor,
  388. $content,
  389. -1,
  390. $count
  391. );
  392. if ($count) {
  393. $this->out(__d('cake_console', 'Updated $fields property'), 1, Shell::VERBOSE);
  394. }
  395. return $content;
  396. }
  397. /**
  398. * Rename collection classes
  399. *
  400. * @return void
  401. */
  402. public function rename_collections() {
  403. $path = $this->_getPath();
  404. $Folder = new Folder($path);
  405. $this->_paths = $Folder->tree(null, false, 'dir');
  406. $this->_findFiles('php');
  407. foreach ($this->_files as $filePath) {
  408. $patterns = [
  409. [
  410. 'Replace $this->_Collection with $this->_registry',
  411. '#\$this->_Collection#',
  412. '$this->_registry',
  413. ],
  414. [
  415. 'Replace ComponentCollection arguments',
  416. '#ComponentCollection\s+\$collection#',
  417. 'ComponentRegistry $registry',
  418. ],
  419. [
  420. 'Rename ComponentCollection',
  421. '#ComponentCollection#',
  422. 'ComponentRegistry',
  423. ],
  424. [
  425. 'Rename HelperCollection',
  426. '#HelperCollection#',
  427. 'HelperRegistry',
  428. ],
  429. [
  430. 'Rename TaskCollection',
  431. '#TaskCollection#',
  432. 'TaskRegistry',
  433. ],
  434. ];
  435. $this->_updateFileRegexp($filePath, $patterns);
  436. }
  437. $this->out(__d('cake_console', '<success>Collection class uses renamed successfully.</success>'));
  438. }
  439. /**
  440. * Update test case assertion methods.
  441. *
  442. * @return void
  443. */
  444. public function tests() {
  445. $path = $this->_getPath();
  446. $Folder = new Folder($path);
  447. $this->_paths = $Folder->tree(null, false, 'dir');
  448. $this->_findFiles('php');
  449. foreach ($this->_files as $filePath) {
  450. $patterns = [
  451. [
  452. 'Replace assertEqual() with assertEquals()',
  453. '#\$this-\>assertEqual\(\#i',
  454. '$this->assertEquals(',
  455. ],
  456. [
  457. 'Replace assertNotEqual() with assertNotEquals()',
  458. '#\$this-\>assertNotEqual\(\#i',
  459. '$this->assertNotEquals(',
  460. ],
  461. [
  462. 'Replace assertIdentical() with assertSame()',
  463. '#\$this-\>assertIdentical\(\#i',
  464. '$this->assertSame(',
  465. ],
  466. [
  467. 'Replace assertNotIdentical() with assertNotSame()',
  468. '#\$this-\>assertNotIdentical\(\#i',
  469. '$this->assertNotSame(',
  470. ],
  471. [
  472. 'Replace assertPattern() with assertRegExp()',
  473. '#\$this-\>assertPattern\(\#i',
  474. '$this->assertRegExp(',
  475. ],
  476. [
  477. 'Replace assertNoPattern() with assertNotRegExp()',
  478. '#\$this-\>assertNoPattern\(\#i',
  479. '$this->assertNotRegExp(',
  480. ],
  481. [
  482. 'Replace assertReference() with assertSame()',
  483. '#\$this-\>assertReference\(\$(.*?),\s*\'(.*?)\'\)#i',
  484. '$this->assertSame($\1, $\2)',
  485. ],
  486. [
  487. 'Replace assertIsA() with assertInstanceOf()',
  488. '#\$this-\>assertIsA\(\$(.*?),\s*\'(.*?)\'\)#i',
  489. '$this->assertInstanceOf(\'\2\', $\1)',
  490. ],
  491. [
  492. 'Replace assert*($is, $expected) with assert*($expected, $is) - except for assertTags()',
  493. '/\bassert((?!tags)\w+)\(\$(\w+),\s*\$expected\)/i',
  494. 'assert\1($expected, $\2)'
  495. ]
  496. ];
  497. $this->_updateFileRegexp($filePath, $patterns);
  498. }
  499. $this->out(__d('cake_console', '<success>Assertion methods renamed successfully.</success>'));
  500. }
  501. /**
  502. * Filter paths to remove webroot, Plugin, tmp directories.
  503. *
  504. * @return array
  505. */
  506. protected function _filterPaths($paths, $directories) {
  507. return array_filter($paths, function ($path) use ($directories) {
  508. foreach ($directories as $dir) {
  509. if (strpos($path, DS . $dir) !== false) {
  510. return false;
  511. }
  512. }
  513. return true;
  514. });
  515. }
  516. /**
  517. * Adds the namespace to a given file.
  518. *
  519. * @param string $filePath The file to add a namespace to.
  520. * @param string $ns The base namespace to use.
  521. * @param boolean $dry Whether or not to operate in dry-run mode.
  522. * @return void
  523. */
  524. protected function _addNamespace($path, $filePath, $ns, $dry) {
  525. $result = true;
  526. $shortPath = str_replace($path, '', $filePath);
  527. $contents = file_get_contents($filePath);
  528. if (preg_match('/namespace\s+[a-z0-9\\\]+;/', $contents)) {
  529. $this->out(__d(
  530. 'cake_console',
  531. '<warning>Skipping %s as it already has a namespace.</warning>',
  532. $shortPath
  533. ));
  534. return;
  535. }
  536. $namespace = trim($ns . str_replace(DS, '\\', dirname($shortPath)), '\\');
  537. $patterns = [
  538. [
  539. 'namespace to ' . $namespace,
  540. '#^(<\?(?:php)?\s+(?:\/\*.*?\*\/\s{0,1})?)#s',
  541. "\\1namespace " . $namespace . ";\n",
  542. ]
  543. ];
  544. $this->_updateFileRegexp($filePath, $patterns);
  545. }
  546. /**
  547. * Updates files based on regular expressions.
  548. *
  549. * @param array $patterns Array of search and replacement patterns.
  550. * @return void
  551. */
  552. protected function _filesRegexpUpdate($patterns) {
  553. $this->_findFiles($this->params['ext']);
  554. foreach ($this->_files as $file) {
  555. $this->out(__d('cake_console', 'Updating %s...', $file), 1, Shell::VERBOSE);
  556. $this->_updateFileRegexp($file, $patterns);
  557. }
  558. }
  559. /**
  560. * Searches the paths and finds files based on extension.
  561. *
  562. * @param string $extensions
  563. * @return void
  564. */
  565. protected function _findFiles($extensions = '', $exclude = []) {
  566. $this->_files = array();
  567. foreach ($this->_paths as $path) {
  568. if (!is_dir($path)) {
  569. continue;
  570. }
  571. $Iterator = new \RegexIterator(
  572. new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)),
  573. '/^.+\.(' . $extensions . ')$/i',
  574. \RegexIterator::MATCH
  575. );
  576. foreach ($Iterator as $file) {
  577. if ($file->isFile() && !in_array($file->getFilename(), $exclude)) {
  578. $this->_files[] = $file->getPathname();
  579. }
  580. }
  581. }
  582. }
  583. /**
  584. * Update a single file with an number of pcre pattern replacements.
  585. *
  586. * @param string $file The file to update
  587. * @param array $patterns The replacement patterns to run.
  588. * @return void
  589. */
  590. protected function _updateFileRegexp($file, $patterns) {
  591. $contents = file_get_contents($file);
  592. foreach ($patterns as $pattern) {
  593. $this->out(__d('cake_console', '<info> * Updating %s</info>', $pattern[0]), 1, Shell::VERBOSE);
  594. $contents = preg_replace($pattern[1], $pattern[2], $contents);
  595. }
  596. $this->_saveFile($file, $contents);
  597. }
  598. /**
  599. * Get the path to operate on. Uses either the first argument,
  600. * or the plugin parameter if its set.
  601. *
  602. * @return string
  603. */
  604. protected function _getPath() {
  605. $path = isset($this->args[0]) ? $this->args[0] : APP;
  606. if (isset($this->params['plugin'])) {
  607. $path = Plugin::path($this->params['plugin']);
  608. }
  609. return rtrim($path, DS);
  610. }
  611. /**
  612. * Get the option parser.
  613. *
  614. * @return ConsoleOptionParser
  615. */
  616. public function getOptionParser() {
  617. $plugin = [
  618. 'short' => 'p',
  619. 'help' => __d('cake_console', 'The plugin to update. Only the specified plugin will be updated.')
  620. ];
  621. $dryRun = [
  622. 'short' => 'd',
  623. 'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
  624. 'boolean' => true
  625. ];
  626. $git = [
  627. 'help' => __d('cake_console', 'Perform git operations. eg. git mv instead of just moving files.'),
  628. 'boolean' => true
  629. ];
  630. $namespace = [
  631. 'help' => __d('cake_console', 'Set the base namespace you want to use. Defaults to App or the plugin name.'),
  632. 'default' => 'App',
  633. ];
  634. $exclude = [
  635. 'help' => __d('cake_console', 'Comma separated list of top level diretories to exclude.'),
  636. 'default' => '',
  637. ];
  638. $path = [
  639. 'help' => __d('cake_console', 'The path to operate on. Will default to APP or the plugin option.'),
  640. 'required' => false,
  641. ];
  642. return parent::getOptionParser()
  643. ->description(__d('cake_console', "A shell to help automate upgrading from CakePHP 3.0 to 2.x. \n" .
  644. "Be sure to have a backup of your application before running these commands."))
  645. ->addSubcommand('all', [
  646. 'help' => __d('cake_console', 'Run all upgrade commands.'),
  647. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  648. ])
  649. ->addSubcommand('locations', [
  650. 'help' => __d('cake_console', 'Move files/directories around. Run this *before* adding namespaces with the namespaces command.'),
  651. 'parser' => ['options' => compact('plugin', 'dryRun', 'git'), 'arguments' => compact('path')]
  652. ])
  653. ->addSubcommand('namespaces', [
  654. 'help' => __d('cake_console', 'Add namespaces to files based on their file path. Only run this *after* you have moved files with locations.'),
  655. 'parser' => ['options' => compact('plugin', 'dryRun', 'namespace', 'exclude'), 'arguments' => compact('path')]
  656. ])
  657. ->addSubcommand('app_uses', [
  658. 'help' => __d('cake_console', 'Replace App::uses() with use statements'),
  659. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  660. ])
  661. ->addSubcommand('rename_classes', [
  662. 'help' => __d('cake_console', 'Rename classes that have been moved/renamed. Run after replacing App::uses().'),
  663. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  664. ])
  665. ->addSubcommand('fixtures', [
  666. 'help' => __d('cake_console', 'Update fixtures to use new index/constraint features. This is necessary before running tests.'),
  667. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')],
  668. ])
  669. ->addSubcommand('rename_collections', [
  670. 'help' => __d('cake_console', 'Rename HelperCollection, ComponentCollection, and TaskCollection. Will also rename component constructor arguments and _Collection properties on all objects.'),
  671. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  672. ])
  673. ->addSubcommand('tests', [
  674. 'help' => __d('cake_console', 'Rename test case assertion methods.'),
  675. 'parser' => ['options' => compact('plugin', 'dryRun'), 'arguments' => compact('path')]
  676. ]);
  677. }
  678. }