Skip to content

Commit

Permalink
add base command traits
Browse files Browse the repository at this point in the history
  • Loading branch information
mlebkowski committed Nov 7, 2024
1 parent a05736d commit f1fcd03
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Cli/DefaultNameTrait.php
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);
}
}
22 changes: 22 additions & 0 deletions src/Cli/FingersCrossedTrait.php
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;
}
15 changes: 15 additions & 0 deletions src/Cli/InitializeInputParamsTrait.php
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);
}
}
37 changes: 37 additions & 0 deletions src/Cli/NamespaceRelativeNamer.php
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);
}

}
22 changes: 22 additions & 0 deletions tests/Cli/NamespaceRelativeNamerTest.php
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));
}
}

0 comments on commit f1fcd03

Please sign in to comment.