Skip to content

Commit 99cf6a0

Browse files
committed
feat: improve deployer singleton handling by accounting for uninitialized state
1 parent 674407d commit 99cf6a0

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/Deployer.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
*/
6767
class Deployer extends Container
6868
{
69-
private static Deployer $instance;
69+
private static ?self $instance = null;
7070

7171
public function __construct(Application $console)
7272
{
@@ -167,9 +167,26 @@ public function __construct(Application $console)
167167

168168
public static function get(): self
169169
{
170+
if (self::$instance === null) {
171+
throw new \RuntimeException('Deployer is not initialized.');
172+
}
173+
170174
return self::$instance;
171175
}
172176

177+
public static function hasInstance(): bool
178+
{
179+
return self::$instance !== null;
180+
}
181+
182+
/**
183+
* @internal For tests that need a clean Deployer singleton between cases.
184+
*/
185+
public static function resetInstance(): void
186+
{
187+
self::$instance = null;
188+
}
189+
173190
public function init(): void
174191
{
175192
$this->addTaskCommands();

src/Host/Host.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Host
2929
public function __construct(string $hostname)
3030
{
3131
$parent = null;
32-
if (Deployer::get()) {
32+
if (Deployer::hasInstance()) {
3333
$parent = Deployer::get()->config;
3434
}
3535
$this->config = new Configuration($parent);

tests/src/Host/HostTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
namespace Deployer\Host;
99

1010
use Deployer\Configuration;
11+
use Deployer\Deployer;
1112
use PHPUnit\Framework\TestCase;
1213

1314
class HostTest extends TestCase
1415
{
16+
protected function tearDown(): void
17+
{
18+
Deployer::resetInstance();
19+
}
20+
1521
public function testHost()
1622
{
1723
$host = new Host('host');

0 commit comments

Comments
 (0)