|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Zalas\Toolbox\Cli\ServiceContainer; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
6
|
|
|
use Psr\Container\ContainerInterface; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Zalas\Toolbox\Cli\Runner\DryRunner; |
|
10
|
|
|
use Zalas\Toolbox\Runner\ParametrisedRunner; |
|
11
|
|
|
use Zalas\Toolbox\Runner\PassthruRunner; |
|
12
|
|
|
use Zalas\Toolbox\Runner\Runner; |
|
13
|
|
|
|
|
14
|
|
|
class RunnerFactory |
|
15
|
|
|
{ |
|
16
|
|
|
private $container; |
|
17
|
|
|
|
|
18
|
10 |
|
public function __construct(ContainerInterface $container) |
|
19
|
|
|
{ |
|
20
|
10 |
|
$this->container = $container; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
7 |
|
public function createRunner(): Runner |
|
24
|
|
|
{ |
|
25
|
7 |
|
$runner = $this->createRealRunner(); |
|
26
|
|
|
|
|
27
|
7 |
|
if ($parameters = $this->parameters()) { |
|
28
|
4 |
|
return new ParametrisedRunner($runner, $parameters); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
2 |
|
return $runner; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return DryRunner|PassthruRunner |
|
36
|
|
|
*/ |
|
37
|
7 |
|
private function createRealRunner() |
|
38
|
|
|
{ |
|
39
|
7 |
|
if ($this->container->get(InputInterface::class)->getOption('dry-run')) { |
|
40
|
4 |
|
return new DryRunner($this->container->get(OutputInterface::class)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
return new PassthruRunner(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
7 |
|
private function parameters(): array |
|
47
|
|
|
{ |
|
48
|
7 |
|
if ($targetDir = $this->targetDir()) { |
|
49
|
4 |
|
return ['%target-dir%' => $targetDir]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
return []; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
7 |
|
private function targetDir(): ?string |
|
56
|
|
|
{ |
|
57
|
7 |
|
if (!$this->container->get(InputInterface::class)->hasOption('target-dir')) { |
|
58
|
2 |
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
$targetDir = $this->container->get(InputInterface::class)->getOption('target-dir'); |
|
62
|
|
|
|
|
63
|
5 |
|
if (!\is_dir($targetDir)) { |
|
64
|
|
|
throw new class(\sprintf('The target dir does not exist: "%s".', $targetDir)) extends \RuntimeException implements ContainerExceptionInterface { |
|
65
|
|
|
}; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
return \realpath($targetDir); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|