Skip to content
Open
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
19 changes: 18 additions & 1 deletion src/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
*/
class Deployer extends Container
{
private static Deployer $instance;
private static ?self $instance = null;

public function __construct(Application $console)
{
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Host/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions tests/src/Host/HostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down