diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2015-07-03 03:54:27 +0300 |
---|---|---|
committer | sgiehl <stefan@piwik.org> | 2015-10-06 18:25:13 +0300 |
commit | 9ba8f216fd7856ce5fef06bf82ecb8f8a2e7e630 (patch) | |
tree | 6ce07d18a85d00b39ab720abe042361c0775aead /plugins/CoreConsole | |
parent | 8ccc9dc05da021325cdbf141a548637fa52f16b2 (diff) |
generate pages instead of implementing them in each controller
Diffstat (limited to 'plugins/CoreConsole')
-rw-r--r-- | plugins/CoreConsole/Commands/GenerateReport.php | 24 | ||||
-rw-r--r-- | plugins/CoreConsole/Commands/GenerateWidget.php | 76 |
2 files changed, 81 insertions, 19 deletions
diff --git a/plugins/CoreConsole/Commands/GenerateReport.php b/plugins/CoreConsole/Commands/GenerateReport.php index 9eb7313400..3f9fda4939 100644 --- a/plugins/CoreConsole/Commands/GenerateReport.php +++ b/plugins/CoreConsole/Commands/GenerateReport.php @@ -10,8 +10,10 @@ namespace Piwik\Plugins\CoreConsole\Commands; use Piwik\Columns\Dimension; +use Piwik\Piwik; use Piwik\Plugin\Manager; use Piwik\Plugin\Report; +use Piwik\Plugin\Reports; use Piwik\Translate; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -67,8 +69,8 @@ class GenerateReport extends GeneratePluginBase $this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles); $this->writeSuccessMessage($output, array( - sprintf('Reports/%s.php for %s generated.', ucfirst($apiName), $pluginName), - 'You should now implement the method called "' . lcfirst($apiName) . '" in API.php', + sprintf('plugins/%s/Reports/%s.php for %s generated.', $pluginName, ucfirst($apiName)), + 'You should now implement the method called <comment>"' . lcfirst($apiName) . '()"</comment> in API.php', // 'Read more about this here: link to developer guide', 'Enjoy!' )); @@ -78,8 +80,10 @@ class GenerateReport extends GeneratePluginBase { $order = 1; - foreach (Report::getAllReports() as $report) { - if ($report->getCategory() === $category) { + $reports = new Reports(); + + foreach ($reports->getAllReports() as $report) { + if ($report->getCategoryId() === $category) { if ($report->getOrder() > $order) { $order = $report->getOrder() + 1; } @@ -189,10 +193,12 @@ class GenerateReport extends GeneratePluginBase $category = $input->getOption('category'); + $reports = new Reports(); + $categories = array(); - foreach (Report::getAllReports() as $report) { - if ($report->getCategory()) { - $categories[] = $report->getCategory(); + foreach ($reports->getAllReports() as $report) { + if ($report->getCategoryId()) { + $categories[] = Piwik::translate($report->getCategoryId()); } } $categories = array_values(array_unique($categories)); @@ -226,7 +232,9 @@ class GenerateReport extends GeneratePluginBase $dimensions = array(); $dimensionNames = array(); - foreach (Report::getAllReports() as $report) { + $reports = new Reports(); + + foreach ($reports->getAllReports() as $report) { $dimension = $report->getDimension(); if (is_object($dimension)) { $name = $dimension->getName(); diff --git a/plugins/CoreConsole/Commands/GenerateWidget.php b/plugins/CoreConsole/Commands/GenerateWidget.php index da4f8e349d..05f92bd4af 100644 --- a/plugins/CoreConsole/Commands/GenerateWidget.php +++ b/plugins/CoreConsole/Commands/GenerateWidget.php @@ -10,8 +10,8 @@ namespace Piwik\Plugins\CoreConsole\Commands; use Piwik\Piwik; -use Piwik\Plugin\Widgets; use Piwik\Translate; +use Piwik\Widget\WidgetsList; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -25,6 +25,7 @@ class GenerateWidget extends GeneratePluginBase $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'); } @@ -33,6 +34,7 @@ class GenerateWidget extends GeneratePluginBase $pluginName = $this->getPluginName($input, $output); $this->checkAndUpdateRequiredPiwikVersion($pluginName, $output); + $widgetName = $this->getWidgetName($input, $output); $category = $this->getCategory($input, $output); if ($category === Piwik::translate($category)) { @@ -40,26 +42,78 @@ class GenerateWidget extends GeneratePluginBase $category = $this->makeTranslationIfPossible($pluginName, $category); } + $widgetMethod = $this->getWidgetMethodName($widgetName); + $widgetClass = ucfirst($widgetMethod); + $exampleFolder = PIWIK_INCLUDE_PATH . '/plugins/ExamplePlugin'; - $replace = array('ExamplePlugin' => $pluginName, - 'Example Category' => $category); - $whitelistFiles = array('/Widgets.php'); + $replace = array('ExamplePlugin' => $pluginName, + 'MyExampleWidget' => $widgetClass, + 'Example Widget Name' => $this->makeTranslationIfPossible($pluginName, $widgetName), + 'Example Widgets' => $category); + $whitelistFiles = array('/Widgets', '/Widgets/MyExampleWidget.php'); $this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles); $this->writeSuccessMessage($output, array( - sprintf('Widgets.php for %s generated.', $pluginName), - 'You can now start defining your plugin widgets', + 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 (Widgets::getAllWidgets() as $widget) { - if ($widget->getCategory()) { - $categories[] = Piwik::translate($widget->getCategory()); + foreach (WidgetsList::get()->getWidgetConfigs() as $widget) { + if ($widget->getCategoryId()) { + $categories[] = Piwik::translate($widget->getCategoryId()); } } $categories = array_values(array_unique($categories)); @@ -111,8 +165,8 @@ class GenerateWidget extends GeneratePluginBase */ protected function getPluginName(InputInterface $input, OutputInterface $output) { - $pluginNames = $this->getPluginNamesHavingNotSpecificFile('Widgets.php'); - $invalidName = 'You have to enter the name of an existing plugin which does not already have any widgets defined'; + $pluginNames = $this->getPluginNames(); + $invalidName = 'You have to enter a name of an existing plugin.'; return $this->askPluginNameAndValidate($input, $output, $pluginNames, $invalidName); } |