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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

## [0.4.2] - 2026-05-27

### Fixed

- Refactored `ActivatePromptCommand` and `PromptManager` to Use `ResolvesVersion` trait for version resolution in prompt retrieval and activation and support mixed types for version signature on `get` and `activate` methods.

## [0.4.1] - 2026-05-17

### Fixed
Expand Down
29 changes: 29 additions & 0 deletions src/Concerns/ResolvesVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PromptPHP\Deck\Concerns;

/**
* Trait for resolving prompt versions.
*/
trait ResolvesVersion
{
/**
* Parse the version input and return the version number as an integer.
*
* @param string $value The version input, e.g. "1" or "v1".
*
* @return int|null The parsed version number, or null if invalid.
*/
public function parseVersion(string $value): ?int
{
$value = trim($value);

if (! preg_match('/^v?([1-9]\d*)$/i', $value, $matches)) {
return null;
}

return (int) $matches[1];
}
}
21 changes: 3 additions & 18 deletions src/Console/Commands/ActivatePromptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace PromptPHP\Deck\Console\Commands;

use Illuminate\Console\Command;
use PromptPHP\Deck\Concerns\ResolvesVersion;
use PromptPHP\Deck\PromptManager;

class ActivatePromptCommand extends Command
{
use ResolvesVersion;

protected $signature = 'prompt:activate {name : The prompt name}
{version : The version number to activate, e.g. 1 or v1}';

Expand Down Expand Up @@ -47,22 +50,4 @@ public function handle(): int
return Command::FAILURE;
}
}

/**
* Parse the version input and return the version number as an integer.
*
* @param string $value The version input, e.g. "1" or "v1".
*
* @return int|null The parsed version number, or null if invalid.
*/
protected function parseVersion(string $value): ?int
{
$value = trim($value);

if (! preg_match('/^v?([1-9]\d*)$/i', $value, $matches)) {
return null;
}

return (int) $matches[1];
}
}
4 changes: 2 additions & 2 deletions src/Exceptions/InvalidVersionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class InvalidVersionException extends DeckException
* * Create an exception for a non-existent version.
*
* @param string $name The name of the prompt.
* @param int $version The version number that was not found.
* @param ?int $version The version number that was not found.
*/
public static function forPrompt(string $name, int $version): self
public static function forPrompt(string $name, ?int $version): self
{
return new self("Version {$version} for prompt [{$name}] does not exist.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/Deck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static \PromptPHP\Deck\PromptTemplate get(string $name, ?int $version = null)
* @method static \PromptPHP\Deck\PromptTemplate get(string $name, string|int|null $version = null)
* @method static \PromptPHP\Deck\PromptTemplate active(string $name)
* @method static array versions(string $name)
* @method static bool activate(string $name, int $version)
Expand Down
20 changes: 15 additions & 5 deletions src/PromptManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\DB;
use PromptPHP\Deck\Concerns\ResolvesVersion;
use PromptPHP\Deck\Exceptions\InvalidVersionException;
use PromptPHP\Deck\Exceptions\PromptNotFoundException;

class PromptManager
{
use ResolvesVersion;

protected Filesystem $files;

protected string $basePath;
Expand All @@ -39,12 +42,19 @@ public function __construct(string $basePath, string $extension, Cache $cache, C
* Get a prompt instance by name and optional version.
* If version is not provided, the active version will be used.
*
* Deck::get('order-summary') // active version.
* Deck::get('order-summary', 2) // specific version.
* Deck::get('order-summary') // active version.
* Deck::get('order-summary', 'v2') // specific version.
* Deck::get('order-summary', 2) // specific version.
*/
public function get(string $name, ?int $version = null): PromptTemplate
public function get(string $name, string|int|null $version = null): PromptTemplate
{
$version ??= $this->getActiveVersion($name);
if ($version === null) {
$version = $this->getActiveVersion($name);
} else {
$versionInput = (string) $version;
$version = $this->parseVersion($versionInput);
}

$cacheKey = $this->config->get('deck.cache.prefix', 'deck:')."{$name}.v{$version}";

// Attempt to load from cache.
Expand Down Expand Up @@ -233,7 +243,7 @@ protected function getActiveVersion(string $name): int
* version directory, so any role scaffolded by make:prompt is
* automatically available at runtime.
*/
protected function loadFromFiles(string $name, int $version): array
protected function loadFromFiles(string $name, ?int $version): array
{
$versionPath = "{$this->basePath}/{$name}/v{$version}";

Expand Down