/
UninstallPackageCommand.php
87 lines (77 loc) · 2.96 KB
/
UninstallPackageCommand.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
<?php
namespace Concrete\Core\Console\Command;
use Concrete\Core\Console\Command;
use Concrete\Core\Console\ConsoleAwareInterface;
use Concrete\Core\Error\ErrorList\ErrorList;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Package;
use Exception;
class UninstallPackageCommand extends Command
{
protected function configure()
{
$errExitCode = static::RETURN_CODE_ON_FAILURE;
$this
->setName('c5:package:uninstall')
->setAliases([
'c5:package-uninstall',
'c5:uninstall-package',
])
->addEnvOption()
->setCanRunAsRoot(false)
->addOption('trash', null, InputOption::VALUE_NONE, 'If this option is specified the package directory will be moved to the trash directory')
->addArgument('package', InputArgument::REQUIRED, 'The handle of the package to be uninstalled')
->setDescription('Uninstall a concrete5 package')
->setHelp(<<<EOT
Returns codes:
0 operation completed successfully
$errExitCode errors occurred
More info at http://documentation.concrete5.org/developers/appendix/cli-commands#c5-package-uninstall
EOT
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$pkgHandle = $input->getArgument('package');
$output->write("Looking for package '$pkgHandle'... ");
$pkg = null;
foreach (Package::getInstalledList() as $installed) {
if ($installed->getPackageHandle() === $pkgHandle) {
$pkg = $installed;
break;
}
}
if ($pkg === null) {
throw new Exception(sprintf("No package with handle '%s' is installed", $pkgHandle));
}
// Provide the console objects to objects that are aware of the console
if ($pkg instanceof ConsoleAwareInterface) {
$pkg->setConsole($this->getApplication(), $output, $input);
}
$output->writeln(sprintf('<info>found (%s).</info>', $pkg->getPackageName()));
$output->write('Checking preconditions... ');
$test = $pkg->testForUninstall();
if (is_object($test)) {
/*
* @var Error $test
*/
throw new Exception(implode("\n", $test->getList()));
}
$output->writeln('<info>good.</info>');
$output->write('Uninstalling package... ');
$pkg->uninstall();
$output->writeln('<info>done.</info>');
if ($input->getOption('trash')) {
$output->write('Moving package to trash... ');
$r = $pkg->backup();
if ($r instanceof ErrorList) {
throw new Exception(implode("\n", $r->getList()));
}
$output->writeln('<info>done.</info>');
}
}
}