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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
<?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\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Widget\WidgetsList;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
*/
class GenerateWidget extends GeneratePluginBase
{
protected function configure()
{
$this->setName('generate:widget')
->setDescription('Adds a plugin widget class to an existing plugin')
->addOption('pluginname', null, InputOption::VALUE_REQUIRED, 'The name of an existing plugin which does not have any widgets defined yet')
->addOption('widgetname', null, InputOption::VALUE_REQUIRED, 'The name of the widget you want to create')
->addOption('category', null, InputOption::VALUE_REQUIRED, 'The name of the category the widget should belong to');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$pluginName = $this->getPluginName($input, $output);
$this->checkAndUpdateRequiredPiwikVersion($pluginName, $output);
$widgetName = $this->getWidgetName($input, $output);
$category = $this->getCategory($input, $output);
if ($category === Piwik::translate($category)) {
// no translation found...
$category = $this->makeTranslationIfPossible($pluginName, $category);
}
$widgetMethod = $this->getWidgetMethodName($widgetName);
$widgetClass = ucfirst($widgetMethod);
$exampleFolder = Manager::getPluginDirectory('ExamplePlugin');
$replace = array('ExamplePlugin' => $pluginName,
'MyExampleWidget' => $widgetClass,
'Example Widget Name' => $this->makeTranslationIfPossible($pluginName, $widgetName),
'About Matomo' => $category);
$whitelistFiles = array('/Widgets', '/Widgets/MyExampleWidget.php');
$this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles);
$this->writeSuccessMessage($output, array(
sprintf('plugins/%s/Widgets/%s.php generated.', $pluginName, $widgetClass),
'You can now start implementing the <comment>render()</comment> method.',
'Enjoy!'
));
}
private function getWidgetMethodName($methodName)
{
$methodName = trim($methodName);
$methodName = str_replace(' ', '', $methodName);
$methodName = preg_replace("/[^A-Za-z0-9]/", '', $methodName);
if (0 !== strpos(strtolower($methodName), 'get')) {
$methodName = 'get' . ucfirst($methodName);
}
return lcfirst($methodName);
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return array
* @throws \RuntimeException
*/
protected function getWidgetName(InputInterface $input, OutputInterface $output)
{
$validate = function ($widgetName) {
if (empty($widgetName)) {
throw new \InvalidArgumentException('Please enter the name of your widget');
}
if (preg_match("/[^A-Za-z0-9 ]/", $widgetName)) {
throw new \InvalidArgumentException('Only alpha numerical characters and whitespaces are allowed');
}
return $widgetName;
};
$widgetName = $input->getOption('widgetname');
if (empty($widgetName)) {
$dialog = $this->getHelperSet()->get('dialog');
$widgetName = $dialog->askAndValidate($output, 'Enter the name of your Widget, for instance "Browser Families": ', $validate);
} else {
$validate($widgetName);
}
$widgetName = ucfirst($widgetName);
return $widgetName;
}
protected function getExistingCategories()
{
$categories = array();
foreach (WidgetsList::get()->getWidgetConfigs() as $widget) {
if ($widget->getCategoryId()) {
$categories[] = Piwik::translate($widget->getCategoryId());
}
}
$categories = array_values(array_unique($categories));
return $categories;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return string
* @throws \RuntimeException
*/
protected function getCategory(InputInterface $input, OutputInterface $output)
{
$validate = function ($category) {
if (empty($category)) {
throw new \InvalidArgumentException('Please enter the name of the category your widget should belong to');
}
return $category;
};
$category = $input->getOption('category');
$categories = $this->getExistingCategories();
if (empty($category)) {
$dialog = $this->getHelperSet()->get('dialog');
$category = $dialog->askAndValidate($output, 'Enter the widget category, for instance "Visitor" (you can reuse any existing category or define a new one): ', $validate, false, null, $categories);
} else {
$validate($category);
}
$translationKey = StaticContainer::get('Piwik\Translation\Translator')->findTranslationKeyForTranslation($category);
if (!empty($translationKey)) {
return $translationKey;
}
$category = ucfirst($category);
return $category;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return string
* @throws \RuntimeException
*/
protected function getPluginName(InputInterface $input, OutputInterface $output)
{
$pluginNames = $this->getPluginNames();
$invalidName = 'You have to enter a name of an existing plugin.';
return $this->askPluginNameAndValidate($input, $output, $pluginNames, $invalidName);
}
}
|