1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\ComposerConfigManager\Infrastructure\Command; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\Console\Command\Command; |
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
7
|
|
|
use Yoanm\ComposerConfigManager\Application\Loader\ConfigurationFileLoaderInterface; |
8
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Configuration; |
9
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile; |
10
|
|
|
|
11
|
|
|
abstract class AbstractTemplatableCommand extends Command |
12
|
|
|
{ |
13
|
|
|
const OPTION_TEMPLATE = 'template'; |
14
|
|
|
|
15
|
|
|
/** @var ConfigurationFileLoaderInterface */ |
16
|
|
|
private $configurationFileLoader; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param ConfigurationFileLoaderInterface $configurationFileLoader |
20
|
|
|
*/ |
21
|
|
|
public function __construct(ConfigurationFileLoaderInterface $configurationFileLoader) |
22
|
|
|
{ |
23
|
|
|
parent::__construct(); |
24
|
|
|
$this->configurationFileLoader = $configurationFileLoader; |
25
|
|
|
} |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
|
|
->addOption( |
33
|
|
|
self::OPTION_TEMPLATE, |
34
|
|
|
null, |
35
|
|
|
InputOption::VALUE_REQUIRED, |
36
|
|
|
'Path of the json template file. Will be used as default values.' |
37
|
|
|
) |
38
|
|
|
; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return ConfigurationFileLoaderInterface |
43
|
|
|
*/ |
44
|
|
|
public function getConfigurationFileLoader() |
45
|
|
|
{ |
46
|
|
|
return $this->configurationFileLoader; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param InputInterface $input |
51
|
|
|
* |
52
|
|
|
* @return null|ConfigurationFile |
53
|
|
|
*/ |
54
|
|
|
protected function loadTemplateConfigurationFile(InputInterface $input) |
55
|
|
|
{ |
56
|
|
|
$templatePath = $input->getOption(self::OPTION_TEMPLATE); |
57
|
|
|
$templateConfiguration = null; |
58
|
|
|
if ($templatePath) { |
59
|
|
|
if (is_dir($templatePath)) { |
60
|
|
|
$templateConfiguration = $this->configurationFileLoader->fromPath($templatePath); |
61
|
|
|
} elseif (is_file($templatePath)) { |
62
|
|
|
$templateConfiguration = $this->configurationFileLoader->fromString(file_get_contents($templatePath)); |
63
|
|
|
} else { |
64
|
|
|
throw new \UnexpectedValueException('Template path is nor a file or a path !'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $templateConfiguration; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|