Skip to content

Commit bceb6c5

Browse files
committed
Move deployer initialization to separate class
1 parent 486dbd0 commit bceb6c5

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

src/Command/Cleanup.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Hypernode\Deploy\Brancher\BrancherHypernodeManager;
88
use Hypernode\Deploy\ConfigurationLoader;
9+
use Hypernode\Deploy\DeployerLoader;
910
use Hypernode\Deploy\Report\ReportLoader;
1011
use Hypernode\DeployConfiguration\BrancherServer;
1112
use Hypernode\DeployConfiguration\Configuration;
@@ -19,17 +20,20 @@
1920
class Cleanup extends Command
2021
{
2122
private ReportLoader $reportLoader;
23+
private DeployerLoader $deployerLoader;
2224
private ConfigurationLoader $configurationLoader;
2325
private BrancherHypernodeManager $brancherHypernodeManager;
2426

2527
public function __construct(
2628
ReportLoader $reportLoader,
29+
DeployerLoader $deployerLoader,
2730
ConfigurationLoader $configurationLoader,
2831
BrancherHypernodeManager $brancherHypernodeManager
2932
) {
3033
parent::__construct();
3134

3235
$this->reportLoader = $reportLoader;
36+
$this->deployerLoader = $deployerLoader;
3337
$this->configurationLoader = $configurationLoader;
3438
$this->brancherHypernodeManager = $brancherHypernodeManager;
3539
}
@@ -58,6 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5862
/** @var string $stageName */
5963
$stageName = $input->getArgument('stage');
6064
if ($stageName) {
65+
$this->deployerLoader->getOrCreateInstance($output);
6166
$config = $this->configurationLoader->load($input->getOption('file') ?: 'deploy.php');
6267
$this->cancelByStage($stageName, $config);
6368
}

src/DeployRunner.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
use Deployer\Exception\Exception;
77
use Deployer\Exception\GracefulShutdownException;
88
use Deployer\Host\Host;
9-
use Hypernode\Deploy\Console\Output\OutputWatcher;
9+
use Hypernode\Deploy\Brancher\BrancherHypernodeManager;
1010
use Hypernode\Deploy\Deployer\RecipeLoader;
1111
use Hypernode\Deploy\Deployer\Task\ConfigurableTaskInterface;
1212
use Hypernode\Deploy\Deployer\Task\TaskFactory;
13-
use Hypernode\Deploy\Brancher\BrancherHypernodeManager;
1413
use Hypernode\Deploy\Exception\CreateBrancherHypernodeFailedException;
1514
use Hypernode\Deploy\Exception\InvalidConfigurationException;
1615
use Hypernode\Deploy\Exception\TimeoutException;
@@ -21,18 +20,13 @@
2120
use Hypernode\DeployConfiguration\Server;
2221
use Hypernode\DeployConfiguration\Stage;
2322
use Psr\Log\LoggerInterface;
24-
use Symfony\Component\Console\Application;
25-
use Symfony\Component\Console\Input\ArrayInput;
26-
use Symfony\Component\Console\Input\InputDefinition;
2723
use Symfony\Component\Console\Input\InputInterface;
28-
use Symfony\Component\Console\Input\InputOption;
2924
use Symfony\Component\Console\Output\OutputInterface;
3025
use Throwable;
3126

3227
use function Deployer\host;
3328
use function Deployer\localhost;
3429
use function Deployer\run;
35-
use function Deployer\task;
3630

3731
class DeployRunner
3832
{
@@ -43,6 +37,7 @@ class DeployRunner
4337
private InputInterface $input;
4438
private LoggerInterface $log;
4539
private RecipeLoader $recipeLoader;
40+
private DeployerLoader $deployerLoader;
4641
private ConfigurationLoader $configurationLoader;
4742
private BrancherHypernodeManager $brancherHypernodeManager;
4843

@@ -61,15 +56,17 @@ public function __construct(
6156
InputInterface $input,
6257
LoggerInterface $log,
6358
RecipeLoader $recipeLoader,
59+
DeployerLoader $deployerLoader,
6460
ConfigurationLoader $configurationLoader,
6561
BrancherHypernodeManager $brancherHypernodeManager
6662
) {
6763
$this->taskFactory = $taskFactory;
6864
$this->input = $input;
6965
$this->log = $log;
7066
$this->recipeLoader = $recipeLoader;
71-
$this->brancherHypernodeManager = $brancherHypernodeManager;
67+
$this->deployerLoader = $deployerLoader;
7268
$this->configurationLoader = $configurationLoader;
69+
$this->brancherHypernodeManager = $brancherHypernodeManager;
7370
}
7471

7572
/**
@@ -79,7 +76,7 @@ public function __construct(
7976
*/
8077
public function run(OutputInterface $output, string $stage, string $task, bool $configureBuildStage, bool $configureServers): int
8178
{
82-
$deployer = $this->getDeployerInstance($output);
79+
$deployer = $this->deployerLoader->getOrCreateInstance($output);
8380

8481
try {
8582
$this->prepare($configureBuildStage, $configureServers, $stage);
@@ -341,19 +338,4 @@ public function getDeploymentReport()
341338
$this->brancherHypernodesRegistered
342339
);
343340
}
344-
345-
private function getDeployerInstance(OutputInterface $output): Deployer
346-
{
347-
$console = new Application();
348-
$deployer = new Deployer($console);
349-
$deployer['output'] = new OutputWatcher($output);
350-
$deployer['input'] = new ArrayInput(
351-
[],
352-
new InputDefinition([
353-
new InputOption('limit'),
354-
new InputOption('profile'),
355-
])
356-
);
357-
return $deployer;
358-
}
359341
}

src/DeployerLoader.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hypernode\Deploy;
6+
7+
use Deployer\Deployer;
8+
use Hypernode\Deploy\Console\Output\OutputWatcher;
9+
use Symfony\Component\Console\Application;
10+
use Symfony\Component\Console\Input\ArrayInput;
11+
use Symfony\Component\Console\Input\InputDefinition;
12+
use Symfony\Component\Console\Input\InputOption;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
15+
class DeployerLoader
16+
{
17+
private ?Deployer $deployer = null;
18+
19+
public function getOrCreateInstance(OutputInterface $output): Deployer
20+
{
21+
if ($this->deployer) {
22+
return $this->deployer;
23+
}
24+
25+
$console = new Application();
26+
$this->deployer = new Deployer($console);
27+
$this->deployer['output'] = new OutputWatcher($output);
28+
$this->deployer['input'] = new ArrayInput(
29+
[],
30+
new InputDefinition([
31+
new InputOption('limit'),
32+
new InputOption('profile'),
33+
])
34+
);
35+
36+
return $this->deployer;
37+
}
38+
}

0 commit comments

Comments
 (0)