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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\CoreConsole\Commands;
use Piwik\Plugin\Manager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
*/
class GenerateVisualizationPlugin extends GeneratePlugin
{
protected function configure()
{
$this->setName('generate:visualizationplugin')
->setDescription('Generates a new visualization plugin including all needed files')
->addOption('name', null, InputOption::VALUE_REQUIRED, 'Plugin name ([a-Z0-9_-])')
->addOption('visualizationname', null, InputOption::VALUE_REQUIRED, 'Visualization name ([a-Z0-9])')
->addOption('description', null, InputOption::VALUE_REQUIRED, 'Plugin description, max 150 characters')
->addOption('pluginversion', null, InputOption::VALUE_OPTIONAL, 'Plugin version')
->addOption('overwrite', null, InputOption::VALUE_NONE, 'Generate even if plugin directory already exists.')
->addOption('full', null, InputOption::VALUE_OPTIONAL, 'If a value is set, an API and a Controller will be created as well. Option is only available for creating plugins, not for creating themes.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$pluginName = $this->getPluginName($input, $output);
$this->checkAndUpdateRequiredPiwikVersion($pluginName, $output);
$description = $this->getPluginDescription($input, $output);
$version = $this->getPluginVersion($input, $output);
$visualizationName = $this->getVisualizationName($input, $output);
$this->generatePluginFolder($pluginName);
$exampleFolder = Manager::getPluginDirectory('ExampleVisualization');
$replace = array(
'SimpleTable' => $visualizationName,
'simpleTable' => lcfirst($visualizationName),
'Simple Table' => $this->makeTranslationIfPossible($pluginName, $visualizationName),
'ExampleVisualization' => $pluginName,
'ExampleVisualizationDescription' => $description
);
$this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles = array());
$this->writeSuccessMessage($output, array(
sprintf('Visualization plugin %s %s generated.', $pluginName, $version),
'Enjoy!'
));
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return string
* @throws \RuntimeException
*/
private function getVisualizationName(InputInterface $input, OutputInterface $output)
{
$self = $this;
$validate = function ($visualizationName) use ($self) {
if (empty($visualizationName)) {
throw new \RuntimeException('You have to enter a visualization name');
}
if (!ctype_alnum($visualizationName)) {
throw new \RuntimeException(sprintf('The visualization name %s is not valid (only AlNum allowed)', $visualizationName));
}
return $visualizationName;
};
$visualizationName = $input->getOption('visualizationname');
if (empty($visualizationName)) {
$dialog = $this->getHelperSet()->get('dialog');
$visualizationName = $dialog->askAndValidate($output, 'Enter a visualization name (only AlNum allowed): ', $validate);
} else {
$validate($visualizationName);
}
$visualizationName = ucfirst($visualizationName);
return $visualizationName;
}
}
|