1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/asset-plugin package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Puli\Cli\Handler; |
13
|
|
|
|
14
|
|
|
use Puli\Cli\Style\PuliTableStyle; |
15
|
|
|
use Puli\Cli\Util\StringUtil; |
16
|
|
|
use Puli\Manager\Api\Installer\InstallerDescriptor; |
17
|
|
|
use Puli\Manager\Api\Installer\InstallerManager; |
18
|
|
|
use Puli\Manager\Api\Installer\InstallerParameter; |
19
|
|
|
use RuntimeException; |
20
|
|
|
use Webmozart\Console\Api\Args\Args; |
21
|
|
|
use Webmozart\Console\Api\IO\IO; |
22
|
|
|
use Webmozart\Console\UI\Component\Table; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @since 1.0 |
26
|
|
|
* |
27
|
|
|
* @author Bernhard Schussek <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class InstallerCommandHandler |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var InstallerManager |
33
|
|
|
*/ |
34
|
|
|
private $installerManager; |
35
|
|
|
|
36
|
7 |
|
public function __construct(InstallerManager $installerManager) |
37
|
|
|
{ |
38
|
7 |
|
$this->installerManager = $installerManager; |
39
|
7 |
|
} |
40
|
|
|
|
41
|
2 |
|
public function handleList(Args $args, IO $io) |
42
|
|
|
{ |
43
|
2 |
|
$table = new Table(PuliTableStyle::borderless()); |
44
|
|
|
|
45
|
2 |
|
$table->setHeaderRow(array('Name', 'Class', 'Description')); |
46
|
|
|
|
47
|
2 |
|
foreach ($this->installerManager->getInstallerDescriptors() as $descriptor) { |
48
|
2 |
|
$className = $descriptor->getClassName(); |
49
|
|
|
|
50
|
2 |
|
if (!$args->isOptionSet('long')) { |
51
|
1 |
|
$className = StringUtil::getShortClassName($className); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$parameters = array(); |
55
|
|
|
|
56
|
2 |
|
foreach ($descriptor->getParameters() as $parameterName => $parameter) { |
57
|
2 |
|
if (!$parameter->isRequired()) { |
58
|
2 |
|
$parameterName .= '='.StringUtil::formatValue($parameter->getDefaultValue()); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
$parameters[] = $parameterName; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$description = $descriptor->getDescription(); |
65
|
|
|
|
66
|
2 |
|
if (!empty($parameters)) { |
67
|
|
|
// non-breaking space |
68
|
2 |
|
$description .= ' <c1>('.implode(",\xc2\xa0", $parameters).')</c1>'; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$table->addRow(array( |
72
|
2 |
|
'<u>'.$descriptor->getName().'</u>', |
73
|
2 |
|
'<c1>'.$className.'</c1>', |
74
|
2 |
|
$description, |
75
|
|
|
)); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
$table->render($io); |
79
|
|
|
|
80
|
2 |
|
return 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function handleAdd(Args $args) |
84
|
|
|
{ |
85
|
3 |
|
$descriptions = $args->getOption('description'); |
86
|
3 |
|
$parameters = array(); |
87
|
|
|
|
88
|
|
|
// The first description is for the installer |
89
|
3 |
|
$description = $descriptions ? array_shift($descriptions) : null; |
90
|
|
|
|
91
|
3 |
View Code Duplication |
foreach ($args->getOption('param') as $parameter) { |
|
|
|
|
92
|
|
|
// Subsequent descriptions are for the parameters |
93
|
1 |
|
$paramDescription = $descriptions ? array_shift($descriptions) : null; |
94
|
|
|
|
95
|
|
|
// Optional parameter with default value |
96
|
1 |
|
if (false !== ($pos = strpos($parameter, '='))) { |
97
|
1 |
|
$parameters[] = new InstallerParameter( |
98
|
1 |
|
substr($parameter, 0, $pos), |
99
|
1 |
|
InstallerParameter::OPTIONAL, |
100
|
1 |
|
StringUtil::parseValue(substr($parameter, $pos + 1)), |
101
|
|
|
$paramDescription |
102
|
|
|
); |
103
|
|
|
|
104
|
1 |
|
continue; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Required parameter |
108
|
1 |
|
$parameters[] = new InstallerParameter( |
109
|
|
|
$parameter, |
110
|
1 |
|
InstallerParameter::REQUIRED, |
111
|
1 |
|
null, |
112
|
|
|
$paramDescription |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
$this->installerManager->addRootInstallerDescriptor(new InstallerDescriptor( |
117
|
3 |
|
$args->getArgument('name'), |
118
|
3 |
|
$args->getArgument('class'), |
119
|
|
|
$description, |
120
|
|
|
$parameters |
121
|
|
|
)); |
122
|
|
|
|
123
|
3 |
|
return 0; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
public function handleDelete(Args $args) |
127
|
|
|
{ |
128
|
2 |
|
$installerName = $args->getArgument('name'); |
129
|
|
|
|
130
|
2 |
|
if (!$this->installerManager->hasInstallerDescriptor($installerName)) { |
131
|
1 |
|
throw new RuntimeException(sprintf( |
132
|
1 |
|
'The installer "%s" does not exist.', |
133
|
|
|
$installerName |
134
|
|
|
)); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
$this->installerManager->removeRootInstallerDescriptor($installerName); |
138
|
|
|
|
139
|
1 |
|
return 0; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.