Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,3 +1,3 @@
<?php

declare(strict_types=1);
Expand Down Expand Up @@ -29,7 +29,7 @@
public function __construct(string $hostname)
{
$parent = null;
if (Deployer::get()) {
if (Deployer::hasInstance()) {
$parent = Deployer::get()->config;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a use case where we create a Host instance before creating Deployer?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should throw an error here instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a use case where we create a Host instance before creating Deployer?

I ran into an error when trying to implement a thin typed wrapper for set('default_selector', ...), the wip implementation is here:

master...m1rm:deployer:feat/add-setDefaultSelector-function

maybe I also took a wrong turn, I'd be fine with throwing an error if that is more appropriate

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. Can't imagine why host without deployer may be needed.

Also please check phpstan errors.

}
$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
Loading