|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: nicolas |
|
5
|
|
|
* Date: 22/12/17 |
|
6
|
|
|
* Time: 14:34 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Devgiants\Model; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Devgiants\Exception\MissingConfigurationFileException; |
|
13
|
|
|
use Devgiants\Service\LiveboxTools; |
|
14
|
|
|
use Monolog\Logger; |
|
15
|
|
|
use Pimple\Container; |
|
16
|
|
|
use Symfony\Component\Console\Command\Command; |
|
17
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
22
|
|
|
use Symfony\Component\Finder\Finder; |
|
23
|
|
|
|
|
24
|
|
|
abstract class ApplicationCommand extends Command { |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
const FILE_OPTION = 'file'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Container |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $container; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Logger |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $log; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var LiveboxTools |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $tools; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string $host |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $host; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* ApplicationCommand constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param null|string $name |
|
53
|
|
|
* @param Container $container |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct( $name, Container $container ) { |
|
56
|
|
|
$this->container = $container; |
|
57
|
|
|
parent::__construct( $name ); |
|
58
|
|
|
|
|
59
|
|
|
// Initiates logging |
|
60
|
|
|
$this->log = $this->container['main_logger']; |
|
61
|
|
|
if ( ! $this->log instanceof Logger ) { |
|
62
|
|
|
throw new \InvalidArgumentException( "Container main_logger entry must be Logger type" ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->tools = $this->container['tools']; |
|
66
|
|
|
if ( ! $this->tools instanceof LiveboxTools ) { |
|
67
|
|
|
throw new \InvalidArgumentException( "Container tools entry must be LiveboxTools type" ); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function configure() { |
|
75
|
|
|
$this |
|
76
|
|
|
->addOption( self::FILE_OPTION, "f", InputOption::VALUE_OPTIONAL, "The YML configuration file" ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param InputInterface $input |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
* @throws MissingConfigurationFileException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getConfigurationFile( InputInterface $input ) { |
|
86
|
|
|
$ymlFile = $input->getOption( self::FILE_OPTION ); |
|
87
|
|
|
if ( $ymlFile === NULL ) { |
|
88
|
|
|
$finder = new Finder(); |
|
89
|
|
|
$finder |
|
90
|
|
|
->in( $this->container['app_dir'] ) |
|
91
|
|
|
->files() |
|
92
|
|
|
->name( '*.yml' ); |
|
93
|
|
|
|
|
94
|
|
|
if ( $finder->count() === 1 ) { |
|
95
|
|
|
foreach ( $finder as $file ) { |
|
96
|
|
|
$ymlFile = $file->getRealPath(); |
|
97
|
|
|
} |
|
98
|
|
|
} else { |
|
99
|
|
|
throw new MissingConfigurationFileException(); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $ymlFile; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @inheritdoc |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function execute( InputInterface $input, OutputInterface $output ) { |
|
110
|
|
|
# TODO use host given in configuration file |
|
111
|
|
|
$this->tools->logout( '192.168.1.1' ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Run command and return the output in the current command |
|
116
|
|
|
* |
|
117
|
|
|
* @param InputInterface $commandToRunInput |
|
118
|
|
|
* @param InputInterface $initalCommandInput |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function runAnotherCommand (InputInterface $commandToRunInput, InputInterface $initalCommandInput) : string { |
|
122
|
|
|
if ($initalCommandInput->hasOption('file')) { |
|
123
|
|
|
$commandToRunInput->setOption('--file', $initalCommandInput->getOption('file')); |
|
124
|
|
|
} |
|
125
|
|
|
$application = $this->getApplication(); |
|
126
|
|
|
$application->setAutoExit(false); |
|
127
|
|
|
$output = new BufferedOutput(); |
|
128
|
|
|
$application->run($commandToRunInput, $output); |
|
129
|
|
|
$application->setAutoExit(true); |
|
130
|
|
|
return $output->fetch(); |
|
131
|
|
|
} |
|
132
|
|
|
} |