From 99cf6a0a5bbd99460ef9698ba1684521cc54e163 Mon Sep 17 00:00:00 2001 From: m1rm Date: Sat, 11 Apr 2026 11:07:07 +0200 Subject: [PATCH] feat: improve deployer singleton handling by accounting for uninitialized state --- src/Deployer.php | 19 ++++++++++++++++++- src/Host/Host.php | 2 +- tests/src/Host/HostTest.php | 6 ++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Deployer.php b/src/Deployer.php index 436299568..bdb3c5e1e 100755 --- a/src/Deployer.php +++ b/src/Deployer.php @@ -66,7 +66,7 @@ */ class Deployer extends Container { - private static Deployer $instance; + private static ?self $instance = null; public function __construct(Application $console) { @@ -167,9 +167,26 @@ public function __construct(Application $console) public static function get(): self { + if (self::$instance === null) { + throw new \RuntimeException('Deployer is not initialized.'); + } + return self::$instance; } + public static function hasInstance(): bool + { + return self::$instance !== null; + } + + /** + * @internal For tests that need a clean Deployer singleton between cases. + */ + public static function resetInstance(): void + { + self::$instance = null; + } + public function init(): void { $this->addTaskCommands(); diff --git a/src/Host/Host.php b/src/Host/Host.php index 093794ae0..fd90d0823 100644 --- a/src/Host/Host.php +++ b/src/Host/Host.php @@ -29,7 +29,7 @@ class Host public function __construct(string $hostname) { $parent = null; - if (Deployer::get()) { + if (Deployer::hasInstance()) { $parent = Deployer::get()->config; } $this->config = new Configuration($parent); diff --git a/tests/src/Host/HostTest.php b/tests/src/Host/HostTest.php index 529027222..e59b86f55 100644 --- a/tests/src/Host/HostTest.php +++ b/tests/src/Host/HostTest.php @@ -8,10 +8,16 @@ namespace Deployer\Host; use Deployer\Configuration; +use Deployer\Deployer; use PHPUnit\Framework\TestCase; class HostTest extends TestCase { + protected function tearDown(): void + { + Deployer::resetInstance(); + } + public function testHost() { $host = new Host('host');