File: GenerateWidget.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (174 lines) | stat: -rw-r--r-- 5,544 bytes parent folder | download
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
<?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\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Widget\WidgetsList;

/**
 */
class GenerateWidget extends GeneratePluginBase
{
    protected function configure()
    {
        $this->setName('generate:widget')
            ->setDescription('Adds a plugin widget class to an existing plugin')
            ->addRequiredValueOption('pluginname', null, 'The name of an existing plugin which does not have any widgets defined yet')
            ->addRequiredValueOption('widgetname', null, 'The name of the widget you want to create')
            ->addRequiredValueOption('category', null, 'The name of the category the widget should belong to');
    }

    protected function doExecute(): int
    {
        $pluginName = $this->getPluginName();
        $this->checkAndUpdateRequiredPiwikVersion($pluginName);

        $widgetName = $this->getWidgetName();
        $category   = $this->getCategory();

        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(array(
             sprintf('plugins/%s/Widgets/%s.php generated.', $pluginName, $widgetClass),
             'You can now start implementing the <comment>render()</comment> method.',
             'Enjoy!',
        ));

        return self::SUCCESS;
    }

    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);
    }

    /**
     * @return string
     * @throws \RuntimeException
     */
    protected function getWidgetName()
    {
        $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 = $this->getInput()->getOption('widgetname');

        if (empty($widgetName)) {
            $widgetName = $this->askAndValidate(
                '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;
    }

    /**
     * @return string
     * @throws \RuntimeException
     */
    protected function getCategory()
    {
        $input = $this->getInput();
        $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)) {
            $category = $this->askAndValidate(
                'Enter the widget category, for instance "Visitor" (you can reuse any existing category or define a new one): ',
                $validate,
                null,
                $categories
            );
        } else {
            $validate($category);
        }

        $translationKey = StaticContainer::get('Piwik\Translation\Translator')->findTranslationKeyForTranslation($category);
        if (!empty($translationKey)) {
            return $translationKey;
        }

        $category = ucfirst($category);

        return $category;
    }

    /**
     * @return string
     * @throws \RuntimeException
     */
    protected function getPluginName()
    {
        $pluginNames = $this->getPluginNames();
        $invalidName = 'You have to enter a name of an existing plugin.';

        return $this->askPluginNameAndValidate($pluginNames, $invalidName);
    }
}