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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CoreConsole\Commands;
use Piwik\Plugin;
use Piwik\Updater;
use Piwik\Version;
class GenerateUpdate extends GeneratePluginBase
{
protected function configure()
{
$this->setName('generate:update')
->setDescription('Adds a new update to an existing plugin or "core"')
->addRequiredValueOption('component', null, 'The name of an existing plugin or "core"');
}
protected function doExecute(): int
{
$component = $this->getComponent();
$version = $this->getVersion($component);
$namespace = $this->getNamespace($component);
$className = $this->getUpdateClassName($component, $version);
$exampleFolder = Plugin\Manager::getPluginDirectory('ExamplePlugin');
$replace = array('Piwik\Plugins\ExamplePlugin\Updates' => $namespace,
'ExamplePlugin' => $component,
'Updates_0_0_2' => $className,
'0.0.2' => $version);
$whitelistFiles = array('/Updates', '/Updates/0.0.2.php');
$this->copyTemplateToPlugin($exampleFolder, $component, $replace, $whitelistFiles);
$this->writeSuccessMessage(array(
sprintf('Updates/%s.php for %s generated.', $version, $component),
'You should have a look at the method update() or getSql() now.',
'Enjoy!',
));
return self::SUCCESS;
}
private function getUpdateClassName($component, $version)
{
$updater = new Updater();
$className = $updater->getUpdateClassName($component, $version);
$parts = explode('\\', $className);
return end($parts);
}
private function getVersion($component)
{
if ($component === 'core') {
return Version::VERSION;
}
$pluginManager = Plugin\Manager::getInstance();
if ($pluginManager->isPluginLoaded($component)) {
$plugin = $pluginManager->getLoadedPlugin($component);
} else {
$plugin = new Plugin($component);
}
return $plugin->getVersion();
}
private function getNamespace($component)
{
$updater = new Updater();
$className = $updater->getUpdateClassName($component, 'xx');
$className = str_replace('Updates_xx', '', $className);
$className = trim($className, '\\');
return $className;
}
/**
* @return string
* @throws \RuntimeException
*/
private function getComponent()
{
$input = $this->getInput();
$components = $this->getPluginNames();
$components[] = 'core';
$validate = function ($component) use ($components) {
if (!in_array($component, $components)) {
throw new \InvalidArgumentException('You have to enter a name of an existing plugin or "core".');
}
return $component;
};
$component = $input->getOption('component');
if (empty($component)) {
$component = $this->askAndValidate(
'Enter the name of your plugin or "core": ',
$validate,
null,
$components
);
} else {
$validate($component);
}
return $component;
}
}
|