Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Registering a command outside WP-CLI throws the named `WpCliUnavailable` exception instead of a bare `\RuntimeException`. It extends `\RuntimeException`, and the message keeps its `WP-CLI is not available` prefix, followed by how to avoid it
- `WpCliService` now depends on the new `WpCliAdapterInterface` contract (implemented by `WpCliAdapter`) instead of the concrete adapter, so that the application layer no longer depends on the infrastructure layer. Passing a `WpCliAdapter` keeps working
- Registering a command outside WP-CLI throws the named `WpCliUnavailableException` exception instead of a bare `\RuntimeException`. It extends `\RuntimeException`, and the message keeps its `WP-CLI is not available` prefix, followed by how to avoid it

## [0.1.1] - 2026-10-13

Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Architecture">
<file>tests/Arch.php</file>
</testsuite>
</testsuites>
<source>
<include>
Expand Down
4 changes: 2 additions & 2 deletions src/Application/Services/WpCliService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Pollora\WpCli\Application\Services;

use Pollora\WpCli\Infrastructure\Adapters\WpCliAdapter;
use Pollora\WpCli\Domain\Contracts\WpCliAdapterInterface;
use Psr\Log\LoggerInterface;
use WP_CLI;

Expand All @@ -23,7 +23,7 @@ class WpCliService
private array $registeredCommands = [];

public function __construct(
private readonly WpCliAdapter $wpCliAdapter,
private readonly WpCliAdapterInterface $wpCliAdapter,
private readonly ?LoggerInterface $logger = null
) {}

Expand Down
28 changes: 28 additions & 0 deletions src/Domain/Contracts/WpCliAdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Pollora\WpCli\Domain\Contracts;

/**
* Port to WP-CLI: what the application needs to register its commands.
*/
interface WpCliAdapterInterface
{
/**
* Check if WP-CLI is available.
*/
public function isAvailable(): bool;

/**
* Register a command with WP-CLI.
*
* @param string $name The command name
* @param string|array|object $handler The command handler
* @param array<string, mixed> $args Additional arguments for WP-CLI
*
* @throws \RuntimeException If WP-CLI is not available
* @throws \InvalidArgumentException If the handler is invalid
*/
public function addCommand(string $name, string|array|object $handler, array $args = []): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Thrown when a command is registered outside a WP-CLI process.
*/
final class WpCliUnavailable extends RuntimeException
final class WpCliUnavailableException extends RuntimeException
{
public static function whenRegisteringACommand(): self
{
Expand Down
7 changes: 4 additions & 3 deletions src/Infrastructure/Adapters/WpCliAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace Pollora\WpCli\Infrastructure\Adapters;

use Pollora\WpCli\Domain\Exceptions\WpCliUnavailable;
use Pollora\WpCli\Domain\Contracts\WpCliAdapterInterface;
use Pollora\WpCli\Domain\Exceptions\WpCliUnavailableException;
use WP_CLI;

/**
Expand All @@ -14,7 +15,7 @@
* providing a clean interface that respects hexagonal architecture
* by isolating external dependencies in the infrastructure layer.
*/
final class WpCliAdapter
final class WpCliAdapter implements WpCliAdapterInterface
{
/**
* Check if WP-CLI is available.
Expand All @@ -37,7 +38,7 @@ public function isAvailable(): bool
public function addCommand(string $name, string|array|object $handler, array $args = []): void
{
if (! $this->isAvailable()) {
throw WpCliUnavailable::whenRegisteringACommand();
throw WpCliUnavailableException::whenRegisteringACommand();
}

$this->validateHandler($handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

use Pollora\WpCli\Domain\Exceptions\WpCliUnavailable;
use Pollora\WpCli\Domain\Exceptions\WpCliUnavailableException;

it('says WP-CLI is missing and what to do about it', function (): void {
$exception = WpCliUnavailable::whenRegisteringACommand();
$exception = WpCliUnavailableException::whenRegisteringACommand();

expect($exception)->toBeInstanceOf(RuntimeException::class)
->and($exception->getMessage())->toStartWith('WP-CLI is not available')
Expand Down
Loading