-
Notifications
You must be signed in to change notification settings - Fork 12
/
Application.php
94 lines (76 loc) · 2.83 KB
/
Application.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Companienv;
use Companienv\Extension\Chained;
use Companienv\Extension\FileToPropagate;
use Companienv\Extension\OnlyIf;
use Companienv\Extension\RsaKeys;
use Companienv\Extension\SslCertificate;
use Companienv\IO\FileSystem\NativePhpFileSystem;
use Companienv\Interaction\AskVariableValues;
use Companienv\IO\InputOutputInteraction;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Output\OutputInterface;
class Application extends ConsoleApplication
{
private $rootDirectory;
/** @var Extension[] */
private $extensions = [];
public function __construct(string $rootDirectory, array $extensions = null)
{
parent::__construct('Companienv', '0.0.x-dev');
$this->rootDirectory = $rootDirectory;
$this->extensions = $extensions !== null ? $extensions : self::defaultExtensions();
$this->add(new class([$this, 'companion'], 'companion') extends Command {
private $callable;
public function __construct(callable $callable, $name)
{
parent::__construct($name);
$this->callable = $callable;
$this->addOption('dist-file', null, InputOption::VALUE_REQUIRED, 'Name of the file used as reference', Application::defaultDistributionFile());
$this->addOption('file', null, InputOption::VALUE_REQUIRED, 'Name of the file used for the values', Application::defaultFile());
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$callable = $this->callable;
return $callable($input, $output);
}
});
$this->setDefaultCommand('companion', true);
}
public function companion(InputInterface $input, OutputInterface $output)
{
$companion = new Companion(
new NativePhpFileSystem($this->rootDirectory),
new InputOutputInteraction($input, $output),
new Chained($this->extensions),
$input->getOption('file'),
$input->getOption('dist-file')
);
$companion->fillGaps();
}
public function registerExtension(Extension $extension)
{
array_unshift($this->extensions, $extension);
}
public static function defaultExtensions()
{
return [
new OnlyIf(),
new SslCertificate(),
new RsaKeys(),
new FileToPropagate(),
new AskVariableValues(),
];
}
public static function defaultFile()
{
return '.env';
}
public static function defaultDistributionFile()
{
return '.env.dist';
}
}