1
|
|
|
<?php |
2
|
|
|
namespace Samurai\Module\Task; |
3
|
|
|
|
4
|
|
|
use Samurai\Module\Module; |
5
|
|
|
use Samurai\Task\ITask; |
6
|
|
|
use Samurai\Task\Task; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Saving |
13
|
|
|
* @package Samurai\Module\Task |
14
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Saving extends Task |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param InputInterface $input |
20
|
|
|
* @param OutputInterface $output |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
4 |
|
public function execute(InputInterface $input, OutputInterface $output) |
24
|
|
|
{ |
25
|
4 |
|
$module = $this->buildModuleFromInput($input); |
26
|
4 |
|
if($this->canBeSaved($input, $output, $module)){ |
27
|
3 |
|
$this->getService('module_procedure')->setOutput($output); |
28
|
3 |
|
if(!$this->getService('module_procedure')->import($module)){ |
29
|
|
|
return ITask::BLOCKING_ERROR_CODE; |
30
|
|
|
} |
31
|
3 |
|
} |
32
|
4 |
|
return ITask::NO_ERROR_CODE; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param InputInterface $input |
37
|
|
|
* @return Module |
38
|
|
|
*/ |
39
|
4 |
View Code Duplication |
private function buildModuleFromInput(InputInterface $input) |
40
|
|
|
{ |
41
|
4 |
|
$module = new Module(); |
42
|
4 |
|
$module->setName($input->getArgument('name')); |
43
|
4 |
|
$module->setPackage($input->getArgument('package')); |
44
|
4 |
|
$module->setVersion($input->getArgument('version')); |
45
|
4 |
|
$module->setDescription($input->getArgument('description')); |
46
|
4 |
|
$module->setSource($input->getArgument('source')); |
47
|
4 |
|
$module->setIsEnable(true); |
48
|
4 |
|
return $module; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param InputInterface $input |
53
|
|
|
* @param OutputInterface $output |
54
|
|
|
* @param Module $newModule |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
4 |
|
private function canBeSaved(InputInterface $input, OutputInterface $output, Module $newModule) |
58
|
|
|
{ |
59
|
4 |
|
return !$this->getService('module_manager')->has($newModule->getName()) |
60
|
4 |
|
|| $this->confirmOverride($input, $output, $this->getInitialModule($newModule), $newModule); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param InputInterface $input |
65
|
|
|
* @param OutputInterface $output |
66
|
|
|
* @param Module $oldModule |
67
|
|
|
* @param Module $newModule |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
2 |
|
private function confirmOverride(InputInterface $input, OutputInterface $output, Module $oldModule, Module $newModule) |
|
|
|
|
71
|
|
|
{ |
72
|
2 |
|
return $this->getService('helper_set')->get('question')->ask( |
73
|
2 |
|
$input, |
74
|
2 |
|
$output, |
75
|
2 |
|
$this->buildQuestion($oldModule, $newModule) |
76
|
2 |
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Module $oldModule |
81
|
|
|
* @param Module $newModule |
82
|
|
|
* @return ConfirmationQuestion |
83
|
|
|
* @internal param Module $module |
84
|
|
|
*/ |
85
|
2 |
|
private function buildQuestion(Module $oldModule, Module $newModule) |
86
|
|
|
{ |
87
|
2 |
|
return new ConfirmationQuestion( |
88
|
2 |
|
sprintf( |
89
|
2 |
|
'<question>Do you want to override the module "%s" with "%s"?</question>[y]', |
90
|
2 |
|
trim($oldModule->getPackage() . ' ' . $oldModule->getVersion()), |
91
|
2 |
|
trim($newModule->getPackage() . ' ' . $newModule->getVersion()) |
92
|
2 |
|
) |
93
|
2 |
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Module $newModule |
98
|
|
|
* @return Module |
99
|
|
|
*/ |
100
|
4 |
|
private function getInitialModule(Module $newModule) |
101
|
4 |
|
{ |
102
|
2 |
|
return $this->getService('module_manager')->get($newModule->getName()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.