diff --git a/src/Cli/DefaultNameTrait.php b/src/Cli/DefaultNameTrait.php new file mode 100644 index 0000000..fd027e7 --- /dev/null +++ b/src/Cli/DefaultNameTrait.php @@ -0,0 +1,10 @@ +name(static::class); + } +} diff --git a/src/Cli/FingersCrossedTrait.php b/src/Cli/FingersCrossedTrait.php new file mode 100644 index 0000000..4441cbc --- /dev/null +++ b/src/Cli/FingersCrossedTrait.php @@ -0,0 +1,22 @@ +run([$this, 'fingersCrossed']); + } + + /** + * @param InputParams $inputParams + * @param FingersCrossedOutput $output + * @return int + * @throws Throwable + */ + abstract public function fingersCrossed(InputParams $inputParams, FingersCrossedOutput $output): int; +} diff --git a/src/Cli/InitializeInputParamsTrait.php b/src/Cli/InitializeInputParamsTrait.php new file mode 100644 index 0000000..b3b2d76 --- /dev/null +++ b/src/Cli/InitializeInputParamsTrait.php @@ -0,0 +1,15 @@ +params = InputParams::ofInput($input); + } +} diff --git a/src/Cli/NamespaceRelativeNamer.php b/src/Cli/NamespaceRelativeNamer.php new file mode 100644 index 0000000..7bfffb3 --- /dev/null +++ b/src/Cli/NamespaceRelativeNamer.php @@ -0,0 +1,37 @@ +namespace = $namespace; + } + + public function name(string $className): ?string { + if (false === str_starts_with($className, $this->namespace)) { + return null; + } + + $relativeNamespace = substr($className, strlen($this->namespace) + 1); + $commandSuffix = 'Command'; + if (str_ends_with($relativeNamespace, $commandSuffix)) { + $relativeNamespace = substr($relativeNamespace, 0, -strlen($commandSuffix)); + } + + /** @var string $dashed */ + $dashed = preg_replace('/([a-z])([A-Z])/', '$1-$2', $relativeNamespace); + $dotted = strtr($dashed, ['\\' => ':']); + return strtolower($dotted); + } + +} diff --git a/tests/Cli/NamespaceRelativeNamerTest.php b/tests/Cli/NamespaceRelativeNamerTest.php new file mode 100644 index 0000000..fdc6659 --- /dev/null +++ b/tests/Cli/NamespaceRelativeNamerTest.php @@ -0,0 +1,22 @@ +name($className)); + } + + public function testBailsIfRootNamespaceDoesNotMAtch(): void { + $namespace = 'Acme\\Foo\\Cli'; + $className = 'Acme\\Bar\\Cli\\Category\\AlphaBravoCommand'; + $sut = NamespaceRelativeNamer::ofBaseNamespace($namespace); + self::assertNull($sut->name($className)); + } +}