-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a05736d
commit f1fcd03
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WonderNetwork\SlimKernel\Cli; | ||
|
||
trait DefaultNameTrait { | ||
protected static function getNameRelativeToNamespace(string $namespace): ?string { | ||
return NamespaceRelativeNamer::ofBaseNamespace($namespace)->name(static::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WonderNetwork\SlimKernel\Cli; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Throwable; | ||
|
||
trait FingersCrossedTrait { | ||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
return FingersCrossedHandler::of($input, $output)->run([$this, 'fingersCrossed']); | ||
} | ||
|
||
/** | ||
* @param InputParams $inputParams | ||
* @param FingersCrossedOutput $output | ||
* @return int | ||
* @throws Throwable | ||
*/ | ||
abstract public function fingersCrossed(InputParams $inputParams, FingersCrossedOutput $output): int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WonderNetwork\SlimKernel\Cli; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
trait InitializeInputParamsTrait { | ||
protected InputParams $params; | ||
|
||
protected function initialize(InputInterface $input, OutputInterface $output): void { | ||
$this->params = InputParams::ofInput($input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WonderNetwork\SlimKernel\Cli; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class NamespaceRelativeNamer { | ||
private string $namespace; | ||
|
||
public static function ofBaseNamespace(string $namespace): self { | ||
return new self($namespace); | ||
} | ||
|
||
private function __construct(string $namespace) { | ||
$this->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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace WonderNetwork\SlimKernel\Cli; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class NamespaceRelativeNamerTest extends TestCase { | ||
public function testRelativeName(): void { | ||
$namespace = 'Acme\\Foo\\Cli'; | ||
$className = 'Acme\\Foo\\Cli\\Category\\AlphaBravoCommand'; | ||
$sut = NamespaceRelativeNamer::ofBaseNamespace($namespace); | ||
self::assertSame("category:alpha-bravo", $sut->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)); | ||
} | ||
} |