From d4b56cda77d75ac2a6f1547a5e5cad1c8a16fa45 Mon Sep 17 00:00:00 2001 From: Victor Ukam Date: Wed, 27 May 2026 06:27:29 +0100 Subject: [PATCH 1/3] Create Trait for resolving prompt versions --- src/Concerns/ResolvesVersion.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/Concerns/ResolvesVersion.php diff --git a/src/Concerns/ResolvesVersion.php b/src/Concerns/ResolvesVersion.php new file mode 100644 index 0000000..9cc1baa --- /dev/null +++ b/src/Concerns/ResolvesVersion.php @@ -0,0 +1,29 @@ + Date: Wed, 27 May 2026 06:28:42 +0100 Subject: [PATCH 2/3] Use `ResolvesVersion` trait for version resolution in prompt retrieval and activation --- .../Commands/ActivatePromptCommand.php | 21 +++---------------- src/Exceptions/InvalidVersionException.php | 4 ++-- src/Facades/Deck.php | 2 +- src/PromptManager.php | 20 +++++++++++++----- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/Console/Commands/ActivatePromptCommand.php b/src/Console/Commands/ActivatePromptCommand.php index b8903e5..ae3b2d2 100644 --- a/src/Console/Commands/ActivatePromptCommand.php +++ b/src/Console/Commands/ActivatePromptCommand.php @@ -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}'; @@ -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]; - } } diff --git a/src/Exceptions/InvalidVersionException.php b/src/Exceptions/InvalidVersionException.php index 54989e6..a4b5014 100644 --- a/src/Exceptions/InvalidVersionException.php +++ b/src/Exceptions/InvalidVersionException.php @@ -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."); } diff --git a/src/Facades/Deck.php b/src/Facades/Deck.php index e21ffe8..cff01a3 100644 --- a/src/Facades/Deck.php +++ b/src/Facades/Deck.php @@ -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) diff --git a/src/PromptManager.php b/src/PromptManager.php index 6e548ed..2650802 100644 --- a/src/PromptManager.php +++ b/src/PromptManager.php @@ -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; @@ -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. @@ -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}"; From a780f8e68607028abb07c8d5725be56478af9d98 Mon Sep 17 00:00:00 2001 From: Victor Ukam Date: Wed, 27 May 2026 10:09:31 +0100 Subject: [PATCH 3/3] Update `CHANGELOG.md` to include changes for v0.4.2 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57b9beb..6d98f83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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