浏览代码

Move inflect script to command.

mscherer 5 年之前
父节点
当前提交
02aab77060

+ 7 - 0
docs/Command/Inflect.md

@@ -0,0 +1,7 @@
+# Inflect Command
+
+```
+bin/cake inflect FooBar all
+```
+
+You can also just run specific actions.

+ 3 - 0
docs/README.md

@@ -63,6 +63,9 @@ Entity:
 Utility:
 * [FileLog](Utility/FileLog.md) to log data into custom file(s) with one line
 
+Command:
+* [Inflect](Command/Inflect.md) to test inflection of words.
+
 ## Basic enhancements of the core
 
 ### Model

+ 257 - 0
src/Command/InflectCommand.php

@@ -0,0 +1,257 @@
+<?php
+declare(strict_types = 1);
+
+namespace Tools\Command;
+
+use Cake\Command\Command;
+use Cake\Console\Arguments;
+use Cake\Console\ConsoleIo;
+use Cake\Console\ConsoleOptionParser;
+use Shim\Utility\Inflector;
+
+/**
+ * Inflect the heck out of your words.
+ *
+ * @author Jose Diaz-Gonzalez
+ * @author Mark Scherer
+ * @license http://www.opensource.org/licenses/mit-license.php The MIT License
+ */
+class InflectCommand extends Command {
+
+	/**
+	 * Valid inflection rules
+	 *
+	 * @var string[]
+	 */
+	protected $validActions = [
+		'pluralize', 'singularize', 'camelize',
+		'underscore', 'humanize', 'tableize',
+		'classify', 'variable', 'dasherize',
+	];
+
+	/**
+	 * Valid inflection rules
+	 *
+	 * @var string[]
+	 */
+	protected $validCommands = [
+		'pluralize', 'singularize', 'camelize',
+		'underscore', 'humanize', 'tableize',
+		'classify', 'variable', 'dasherize', 'all', 'quit',
+	];
+
+	/**
+	 * Hook action for defining this command's option parser.
+	 *
+	 * @see https://book.cakephp.org/4/en/console-commands/commands.html#defining-arguments-and-options
+	 * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
+	 * @return \Cake\Console\ConsoleOptionParser The built parser.
+	 */
+	public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser {
+		$parser = parent::buildOptionParser($parser);
+
+		$parser->addArgument('word');
+		$parser->addArgument('action', [
+			'default' => 'all',
+		]);
+
+		return $parser;
+	}
+
+	/**
+	 * Implement this action with your command's logic.
+	 *
+	 * @param \Cake\Console\Arguments $args The command arguments.
+	 * @param \Cake\Console\ConsoleIo $io The console io
+	 * @return int|null|void The exit code or null for success
+	 */
+	public function execute(Arguments $args, ConsoleIo $io) {
+		if ($args->getArguments()) {
+			$arguments = $this->_parseArguments($args->getArguments(), $io);
+		} else {
+			$arguments = $this->_interactive($io);
+		}
+
+		if (!$arguments['action'] || !$arguments['word']) {
+			return static::CODE_SUCCESS;
+		}
+
+		$this->_inflect($arguments['action'], $arguments['word'], $io);
+	}
+
+	/**
+	 * Prompts the user for word
+	 *
+	 * @param \Cake\Console\ConsoleIo $io
+	 *
+	 * @return array
+	 */
+	protected function _interactive(ConsoleIo $io) {
+		$word = $this->_getWord($io);
+		$action = $this->_getAction($io);
+
+		return ['action' => $action, 'word' => $word];
+	}
+
+	/**
+	 * Requests a valid inflection action
+	 *
+	 * @param \Cake\Console\ConsoleIo $io
+	 *
+	 * @return string
+	 */
+	protected function _getAction(ConsoleIo $io) {
+		$validCharacters = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'a', 'q'];
+		$validCommands = array_merge($validCharacters, $this->validCommands);
+
+		$command = null;
+		while (empty($command)) {
+			$io->out('Please type the number or name of the inflection you would like to use');
+			$io->hr();
+			$io->out('[1] Pluralize');
+			$io->out('[2] Singularize');
+			$io->out('[3] Camelize');
+			$io->out('[4] Underscore');
+			$io->out('[5] Humanize');
+			$io->out('[6] Tableize');
+			$io->out('[7] Classify');
+			$io->out('[8] Variable');
+			$io->out('[9] Dasherize');
+			$io->out('[a] All');
+			$io->out('[q] Quit');
+			$temp = $io->ask('What action would you like to perform?', 'q');
+			if (in_array(strtolower($temp), $validCommands)) {
+				$command = strtolower($temp);
+			} else {
+				$io->out('Try again.');
+			}
+		}
+
+		switch ($command) {
+			case '1':
+			case 'pluralize':
+				return 'pluralize';
+			case '2':
+			case 'singularize':
+				return 'singularize';
+			case '3':
+			case 'camelize':
+				return 'camelize';
+			case '4':
+			case 'underscore':
+				return 'underscore';
+			case '5':
+			case 'humanize':
+				return 'humanize';
+			case '6':
+			case 'tableize':
+				return 'tableize';
+			case '7':
+			case 'classify':
+				return 'classify';
+			case '8':
+			case 'variable':
+				return 'variable';
+			case '9':
+			case 'dasherize':
+				return 'dasherize';
+			case 'a':
+			case 'all':
+				return 'all';
+			case 'q':
+			case 'quit':
+			default:
+				$this->abort(static::CODE_SUCCESS);
+		}
+	}
+
+	/**
+	 * Requests word to inflect
+	 *
+	 * @param \Cake\Console\ConsoleIo $io
+	 *
+	 * @return string|null
+	 */
+	protected function _getWord(ConsoleIo $io) {
+		$word = null;
+		while (empty($word)) {
+			$temp = $io->ask('What word would you like to inflect?');
+			if (!empty($temp)) {
+				$word = $temp;
+			} else {
+				$io->out('Try again.');
+			}
+		}
+
+		return $word;
+	}
+
+	/**
+	 * Parse the arguments into the function and the word to be inflected
+	 *
+	 * @param array $arguments
+	 * @param \Cake\Console\ConsoleIo $io
+	 *
+	 * @return array
+	 */
+	protected function _parseArguments($arguments, ConsoleIo $io) {
+		$word = array_shift($arguments);
+
+		if (!$arguments) {
+			$action = $this->_getAction($io);
+		} else {
+			$action = array_shift($arguments);
+		}
+
+		return ['action' => $action, 'word' => $word];
+	}
+
+	/**
+	 * Inflects a set of word based upon the inflection set in the arguments
+	 *
+	 * @param string $function
+	 * @param string $word
+	 * @param \Cake\Console\ConsoleIo $io
+	 *
+	 * @return void
+	 */
+	protected function _inflect($function, $word, ConsoleIo $io) {
+		$io->out($word);
+		if ($function === 'all') {
+			foreach ($this->validActions as $action) {
+				$functionName = $this->_getMessage($action);
+				$io->out("{$functionName}: " . Inflector::$action($word));
+			}
+		} else {
+			$functionName = $this->_getMessage($function);
+			if (!$functionName) {
+				$io->error('Action does not exist');
+			}
+			$io->out("{$functionName}: " . Inflector::$function($word));
+		}
+	}
+
+	/**
+	 * Returns the appropriate message for a given function
+	 *
+	 * @param string $function
+	 * @return string|null
+	 */
+	protected function _getMessage($function) {
+		$messages = [
+			'camelize' => 'CamelCase form             ',
+			'camelBacked' => 'camelBacked form           ',
+			'classify' => 'Cake Model Class form      ',
+			'humanize' => 'Human Readable Group form  ',
+			'singularize' => 'Singular form              ',
+			'dasherize' => 'Dasherized-form            ',
+			'pluralize' => 'Pluralized form            ',
+			'tableize' => 'table_names form           ',
+			'underscore' => 'under_scored_form          ',
+			'variable' => 'variableForm               ',
+		];
+
+		return $messages[$function] ?? null;
+	}
+
+}

+ 0 - 248
src/Shell/InflectShell.php

@@ -1,248 +0,0 @@
-<?php
-
-namespace Tools\Shell;
-
-use Cake\Console\Shell;
-use Shim\Utility\Inflector;
-
-/**
- * Inflect Shell
- *
- * Inflect the heck out of your word(s)
- *
- * @author Jose Diaz-Gonzalez
- * @author Mark Scherer
- * @license http://www.opensource.org/licenses/mit-license.php The MIT License
- */
-class InflectShell extends Shell {
-
-	/**
-	 * Valid inflection rules
-	 *
-	 * @var array
-	 */
-	protected $validMethods = [
-		'pluralize', 'singularize', 'camelize',
-		'underscore', 'humanize', 'tableize',
-		'classify', 'variable', 'dasherize', 'slug',
-	];
-
-	/**
-	 * Valid inflection rules
-	 *
-	 * @var array
-	 */
-	protected $validCommands = [
-		'pluralize', 'singularize', 'camelize',
-		'underscore', 'humanize', 'tableize',
-		'classify', 'variable', 'dasherize', 'slug', 'all', 'quit',
-	];
-
-	/**
-	 * Inflects words
-	 *
-	 * @return bool|int|null|void
-	 */
-	public function main() {
-		if (!empty($this->args)) {
-			$arguments = $this->_parseArguments($this->args);
-		} else {
-			$arguments = $this->_interactive();
-		}
-		$this->_inflect($arguments['method'], $arguments['words']);
-	}
-
-	/**
-	 * Prompts the user for words
-	 *
-	 * @return array
-	 */
-	protected function _interactive() {
-		$method = $this->_getMethod();
-		$words = $this->_getWords();
-
-		return ['method' => $method, 'words' => $words];
-	}
-
-	/**
-	 * Requests a valid inflection method
-	 *
-	 * @return string|null
-	 */
-	protected function _getMethod() {
-		$validCharacters = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'q'];
-		$validCommands = array_merge($validCharacters, $this->validCommands);
-
-		$command = null;
-		while (empty($command)) {
-			$this->out('Please type the number or name of the inflection method you would like to use');
-			$this->hr();
-			$this->out('[1] Pluralize');
-			$this->out('[2] Singularize');
-			$this->out('[3] Camelize');
-			$this->out('[4] Underscore');
-			$this->out('[5] Humanize');
-			$this->out('[6] Tableize');
-			$this->out('[7] Classify');
-			$this->out('[8] Variable');
-			$this->out('[9] Dasherize');
-			$this->out('[10] Slug');
-			$this->out('[q] Quit');
-			$temp = $this->in('What command would you like to perform?', null, 'q');
-			if (in_array(strtolower($temp), $validCommands)) {
-				$command = strtolower($temp);
-			} else {
-				$this->out('Try again.');
-			}
-		}
-
-		switch ($command) {
-			case '1':
-			case 'pluralize':
-				return 'pluralize';
-			case '2':
-			case 'singularize':
-				return 'singularize';
-			case '3':
-			case 'camelize':
-				return 'camelize';
-			case '4':
-			case 'underscore':
-				return 'underscore';
-			case '5':
-			case 'humanize':
-				return 'humanize';
-			case '6':
-			case 'tableize':
-				return 'tableize';
-			case '7':
-			case 'classify':
-				return 'classify';
-			case '8':
-			case 'variable':
-				return 'variable';
-			case '9':
-			case 'dasherize':
-				return 'dasherize';
-			case '10':
-			case 'slug':
-				return 'slug';
-			case 'q':
-			case 'quit':
-			default:
-				$this->out('Exit');
-				$this->_stop();
-
-				return null;
-		}
-	}
-
-	/**
-	 * Requests words to inflect
-	 *
-	 * @return string|null
-	 */
-	protected function _getWords() {
-		$words = null;
-		while (empty($words)) {
-			$temp = $this->in('What word(s) would you like to inflect?');
-			if (!empty($temp)) {
-				$words = $temp;
-			} else {
-				$this->out('Try again.');
-			}
-		}
-
-		return $words;
-	}
-
-	/**
-	 * Parse the arguments into the function and the word(s) to be inflected
-	 *
-	 * @param array $arguments
-	 * @return array
-	 */
-	protected function _parseArguments($arguments) {
-		$words = null;
-		$function = $arguments[0];
-		unset($arguments[0]);
-		if (!in_array($function, array_merge($this->validMethods, ['all']))) {
-			$function = $this->_getMethod();
-		}
-
-		$arguments = array_reverse($arguments);
-		if (count($arguments) === 0) {
-			$words = $this->_getWords();
-		} else {
-			while (count($arguments) > 0) {
-				$words .= array_pop($arguments);
-				if (count($arguments) > 0) {
-					$words .= ' ';
-				}
-			}
-		}
-
-		return ['method' => $function, 'words' => $words];
-	}
-
-	/**
-	 * Inflects a set of words based upon the inflection set in the arguments
-	 *
-	 * @param string $function
-	 * @param string $words
-	 * @return void
-	 */
-	protected function _inflect($function, $words) {
-		$this->out($words);
-		if ($function === 'all') {
-			foreach ($this->validMethods as $method) {
-				$functionName = $this->_getMessage($method);
-				$this->out("{$functionName}: " . Inflector::$method($words));
-			}
-		} else {
-			$functionName = $this->_getMessage($function);
-			$this->out("{$functionName}: " . Inflector::$function($words));
-		}
-	}
-
-	/**
-	 * Returns the appropriate message for a given function
-	 *
-	 * @param string $function
-	 * @return string
-	 */
-	protected function _getMessage($function) {
-		$messages = [
-			'camelize' => 'CamelCase form             ',
-			'classify' => 'Cake Model Class form      ',
-			'humanize' => 'Human Readable Group form  ',
-			'singularize' => 'Singular form              ',
-			'dasherize' => 'Dasherized-form               ',
-			'slug' => 'Slugged-form               ',
-			'pluralize' => 'Pluralized form            ',
-			'tableize' => 'table_names form           ',
-			'underscore' => 'under_scored_form          ',
-			'variable' => 'variableForm               ',
-		];
-
-		return $messages[$function];
-	}
-
-	/**
-	 * Displays help contents
-	 *
-	 * @return void
-	 */
-	public function help() {
-		$this->out('Inflector Shell');
-		$this->out('');
-		$this->out('This shell uses the Inflector class to inflect any word(s) you wish');
-		$this->hr();
-		$this->out('Usage: cake inflect');
-		$this->out('       cake inflect methodName');
-		$this->out('       cake inflect methodName word');
-		$this->out('       cake inflect methodName words to inflect');
-		$this->out('');
-	}
-
-}

+ 52 - 0
tests/TestCase/Command/InflectCommandTest.php

@@ -0,0 +1,52 @@
+<?php
+declare(strict_types = 1);
+
+namespace Tools\Test\TestCase\Command;
+
+use Cake\TestSuite\ConsoleIntegrationTestTrait;
+use Cake\TestSuite\TestCase;
+
+/**
+ * Tools\Command\InflectCommand Test Case
+ *
+ * @uses \Tools\Command\InflectCommand
+ */
+class InflectCommandTest extends TestCase {
+
+	use ConsoleIntegrationTestTrait;
+
+	/**
+	 * setUp method
+	 *
+	 * @return void
+	 */
+	public function setUp(): void {
+		parent::setUp();
+		$this->useCommandRunner();
+	}
+
+	/**
+	 * Test execute method
+	 *
+	 * @return void
+	 */
+	public function testExecute(): void {
+		$this->exec('inflect FooBar all');
+
+		$output = $this->_out->messages();
+		$expected = [
+			0 => 'FooBar',
+			1 => 'Pluralized form            : FooBars',
+			2 => 'Singular form              : FooBar',
+			3 => 'CamelCase form             : FooBar',
+			4 => 'under_scored_form          : foo_bar',
+			5 => 'Human Readable Group form  : FooBar',
+			6 => 'table_names form           : foo_bars',
+			7 => 'Cake Model Class form      : FooBar',
+			8 => 'variableForm               : fooBar',
+			9 => 'Dasherized-form            : foo-bar',
+		];
+		$this->assertSame($expected, $output);
+	}
+
+}

+ 0 - 95
tests/TestCase/Shell/InflectShellTest.php

@@ -1,95 +0,0 @@
-<?php
-
-namespace Tools\Test\TestCase\Shell;
-
-use Cake\Console\ConsoleIo;
-use Shim\TestSuite\ConsoleOutput;
-use Shim\TestSuite\TestCase;
-use Tools\Shell\InflectShell;
-
-class InflectShellTest extends TestCase {
-
-	/**
-	 * @var \Tools\Shell\InflectShell
-	 */
-	protected $Shell;
-
-	/**
-	 * @var \Shim\TestSuite\ConsoleOutput
-	 */
-	protected $out;
-
-	/**
-	 * @var \Shim\TestSuite\ConsoleOutput
-	 */
-	protected $err;
-
-	/**
-	 * @return void
-	 */
-	public function setUp(): void {
-		parent::setUp();
-
-		$this->out = new ConsoleOutput();
-		$this->err = new ConsoleOutput();
-		$io = new ConsoleIo($this->out, $this->err);
-
-		$this->Shell = $this->getMockBuilder(InflectShell::class)
-			->setMethods(['in', '_stop'])
-			->setConstructorArgs([$io])
-			->getMock();
-	}
-
-	/**
-	 * @return void
-	 */
-	public function tearDown(): void {
-		parent::tearDown();
-		unset($this->Shell);
-	}
-
-	/**
-	 * @return void
-	 */
-	public function testMain() {
-		$this->Shell->expects($this->any())->method('in')
-			->will($this->returnValue('FooBar'));
-
-		$this->Shell->runCommand(['pluralize']);
-		$output = $this->out->output();
-		$expected = 'FooBars';
-		$this->assertStringContainsString($expected, $output);
-
-		$this->Shell->runCommand(['underscore']);
-		$output = $this->out->output();
-		$expected = 'foo_bar';
-		$this->assertStringContainsString($expected, $output);
-
-		$this->Shell->runCommand(['dasherize']);
-		$output = $this->out->output();
-		$expected = 'foo-bar';
-		$this->assertStringContainsString($expected, $output);
-
-		$this->Shell->runCommand(['slug']);
-		$output = $this->out->output();
-		$expected = 'foo-bar';
-		$this->assertStringContainsString($expected, $output);
-
-		$this->Shell->runCommand(['tableize']);
-		$output = $this->out->output();
-		$expected = 'foo_bar';
-		$this->assertStringContainsString($expected, $output);
-	}
-
-	/**
-	 * @return void
-	 */
-	public function testMainAll() {
-		$this->Shell->runCommand(['all', 'Foo Bar']);
-		$output = $this->out->output();
-
-		$this->assertStringContainsString('Pluralized form', $output);
-		$this->assertStringContainsString('Slugged-form', $output);
-	}
-
-}