-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
CoreUpdateChannelCommand.php
156 lines (130 loc) · 5.18 KB
/
CoreUpdateChannelCommand.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Console;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Table\Table;
use Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel;
use Joomla\Console\Command\AbstractCommand;
use Joomla\Database\DatabaseInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Console command for managing the update channel for Joomla
*
* @since __DEPLOY_VERSION__
*/
class CoreUpdateChannelCommand extends AbstractCommand
{
/**
* The default command name
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected static $defaultName = 'core:update:channel';
/**
* @var DatabaseInterface
* @since __DEPLOY_VERSION__
*/
private $db;
/**
* CoreUpdateChannelCommand constructor.
*
* @param DatabaseInterface $db Database Instance
*
* @since __DEPLOY_VERSION__
*/
public function __construct(DatabaseInterface $db)
{
$this->db = $db;
parent::__construct();
}
/**
* Initialise the command.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function configure(): void
{
$help = "<info>%command.name%</info> allows to manage the update channel for Joomla core updates. Returns the currently selected channel when called without any parameters, otherwise sets it.
\nUsage: <info>php %command.full_name%</info>";
$this->setDescription('Manage the update channel for Joomla core updates');
$this->setHelp($help);
$this->addArgument('channel', InputArgument::OPTIONAL, 'Name of the update channel [default, next, testing, custom]');
$this->addOption('url', null, InputOption::VALUE_OPTIONAL, 'URL to update source. Only for custom update channel');
}
/**
* Internal function to execute the command.
*
* @param InputInterface $input The input to inject into the command.
* @param OutputInterface $output The output to inject into the command.
*
* @return integer The command exit code
*
* @since __DEPLOY_VERSION__
*/
protected function doExecute(InputInterface $input, OutputInterface $output): int
{
$symfonyStyle = new SymfonyStyle($input, $output);
$params = ComponentHelper::getParams('com_joomlaupdate');
$channel = $input->getArgument('channel');
if (!$channel) {
switch ($params->get('updatesource', 'default')) {
case 'default':
case 'next':
case 'testing':
$symfonyStyle->writeln('You are on the "' . $params->get('updatesource', 'default') . '" update channel.');
break;
case 'custom':
$symfonyStyle->writeln('You are on a "custom" update channel with the URL ' . $params->get('customurl') . '.');
break;
default:
$symfonyStyle->error('The update channel is set to the invalid value \'' . $params->get('updatesource') . '\'!');
return Command::FAILURE;
}
return Command::SUCCESS;
}
if (!\in_array($channel, ['default', 'next', 'testing', 'custom'])) {
$symfonyStyle->error('The given update channel is invalid. Please only choose from [default, next, testing, custom].');
return Command::FAILURE;
}
$params->set('updatesource', $channel);
if ($channel == 'custom') {
$url = $input->getOption('url');
if (!$url) {
$symfonyStyle->error('When using the custom update channel, you have to provide a valid URL.');
return Command::FAILURE;
}
$params->set('customurl', $url);
}
// Storing the parameters in the DB
$table = Table::getInstance('extension');
$table->load(['type' => 'component', 'element' => 'com_joomlaupdate']);
$table->params = $params->toString();
$table->store();
/** @var UpdateModel $updatemodel */
$app = $this->getApplication();
$updatemodel = $app->bootComponent('com_joomlaupdate')->getMVCFactory($app)->createModel('Update', 'Administrator');
$updatemodel->applyUpdateSite();
if ($channel == 'custom') {
$symfonyStyle->success('The update channel for this site has been set to the custom url "' . $params->get('customurl') . '".');
} else {
$symfonyStyle->success('The update channel for this site has been set to "' . $params->get('updatesource', 'default') . '".');
}
return Command::SUCCESS;
}
}