diff --git a/plugins/CoreConsole/Commands/GenerateApi.php b/plugins/CoreConsole/Commands/GenerateApi.php index 6a781abce3841f00f87b33b4e740a4348b3c255e..f1ea6d2d44eb4230f0a61553bcca329be07b0009 100644 --- a/plugins/CoreConsole/Commands/GenerateApi.php +++ b/plugins/CoreConsole/Commands/GenerateApi.php @@ -50,31 +50,12 @@ class GenerateApi extends GeneratePluginBase * @return array * @throws \RunTimeException */ - private function getPluginName(InputInterface $input, OutputInterface $output) + protected function getPluginName(InputInterface $input, OutputInterface $output) { $pluginNames = $this->getPluginNamesHavingNotSpecificFile('API.php'); + $invalidName = 'You have to enter the name of an existing plugin which does not already have an API'; - $validate = function ($pluginName) use ($pluginNames) { - if (!in_array($pluginName, $pluginNames)) { - throw new \InvalidArgumentException('You have to enter the name of an existing plugin which does not already have an API'); - } - - return $pluginName; - }; - - $pluginName = $input->getOption('pluginname'); - - if (empty($pluginName)) { - $dialog = $this->getHelperSet()->get('dialog'); - $pluginName = $dialog->askAndValidate($output, 'Enter the name of your plugin: ', $validate, false, null, $pluginNames); - } else { - $validate($pluginName); - } - - $pluginName = ucfirst($pluginName); - - return $pluginName; + return parent::getPluginName($input, $output, $pluginNames, $invalidName); } - } \ No newline at end of file diff --git a/plugins/CoreConsole/Commands/GenerateCommand.php b/plugins/CoreConsole/Commands/GenerateCommand.php index c6da74547cdeaa953567b5d96d05def3a76128c3..0167507937ced91449ba0ea3c01b0c60d4a1e5b4 100644 --- a/plugins/CoreConsole/Commands/GenerateCommand.php +++ b/plugins/CoreConsole/Commands/GenerateCommand.php @@ -84,35 +84,11 @@ class GenerateCommand extends GeneratePluginBase return $testname; } - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return string - * @throws \RunTimeException - */ - private function getPluginName(InputInterface $input, OutputInterface $output) + protected function getPluginName(InputInterface $input, OutputInterface $output) { $pluginNames = $this->getPluginNames(); + $invalidName = 'You have to enter the name of an existing plugin'; - $validate = function ($pluginName) use ($pluginNames) { - if (!in_array($pluginName, $pluginNames)) { - throw new \InvalidArgumentException('You have to enter the name of an existing plugin'); - } - - return $pluginName; - }; - - $pluginName = $input->getOption('pluginname'); - - if (empty($pluginName)) { - $dialog = $this->getHelperSet()->get('dialog'); - $pluginName = $dialog->askAndValidate($output, 'Enter the name of your plugin: ', $validate, false, null, $pluginNames); - } else { - $validate($pluginName); - } - - $pluginName = ucfirst($pluginName); - - return $pluginName; + return parent::getPluginName($input, $output, $pluginNames, $invalidName); } } \ No newline at end of file diff --git a/plugins/CoreConsole/Commands/GenerateController.php b/plugins/CoreConsole/Commands/GenerateController.php index 7540c96dcc004acc6426b550726a81b11351bd34..bf4a88b5122545e026dd3b3a56f37a6400138534 100644 --- a/plugins/CoreConsole/Commands/GenerateController.php +++ b/plugins/CoreConsole/Commands/GenerateController.php @@ -50,30 +50,12 @@ class GenerateController extends GeneratePluginBase * @return array * @throws \RunTimeException */ - private function getPluginName(InputInterface $input, OutputInterface $output) + protected function getPluginName(InputInterface $input, OutputInterface $output) { $pluginNames = $this->getPluginNamesHavingNotSpecificFile('Controller.php'); + $invalidName = 'You have to enter the name of an existing plugin which does not already have a Controller'; - $validate = function ($pluginName) use ($pluginNames) { - if (!in_array($pluginName, $pluginNames)) { - throw new \InvalidArgumentException('You have to enter the name of an existing plugin which does not already have a Controller'); - } - - return $pluginName; - }; - - $pluginName = $input->getOption('pluginname'); - - if (empty($pluginName)) { - $dialog = $this->getHelperSet()->get('dialog'); - $pluginName = $dialog->askAndValidate($output, 'Enter the name of your plugin: ', $validate, false, null, $pluginNames); - } else { - $validate($pluginName); - } - - $pluginName = ucfirst($pluginName); - - return $pluginName; + return parent::getPluginName($input, $output, $pluginNames, $invalidName); } } \ No newline at end of file diff --git a/plugins/CoreConsole/Commands/GeneratePluginBase.php b/plugins/CoreConsole/Commands/GeneratePluginBase.php index 1bf122adac001f59485a88b55d29493bffdf52de..db1876196bffb7a69842ee1c92f8aaa42d4ac4f4 100644 --- a/plugins/CoreConsole/Commands/GeneratePluginBase.php +++ b/plugins/CoreConsole/Commands/GeneratePluginBase.php @@ -14,6 +14,8 @@ namespace Piwik\Plugins\CoreConsole\Commands; use Piwik\Filesystem; use Piwik\Plugin\ConsoleCommand; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; /** * @package CoreConsole @@ -111,4 +113,34 @@ class GeneratePluginBase extends ConsoleCommand return $pluginNames; } + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return array + * @throws \RunTimeException + */ + protected function getPluginName(InputInterface $input, OutputInterface $output, $pluginNames, $invalidArgumentException) + { + $validate = function ($pluginName) use ($pluginNames, $invalidArgumentException) { + if (!in_array($pluginName, $pluginNames)) { + throw new \InvalidArgumentException($invalidArgumentException); + } + + return $pluginName; + }; + + $pluginName = $input->getOption('pluginname'); + + if (empty($pluginName)) { + $dialog = $this->getHelperSet()->get('dialog'); + $pluginName = $dialog->askAndValidate($output, 'Enter the name of your plugin: ', $validate, false, null, $pluginNames); + } else { + $validate($pluginName); + } + + $pluginName = ucfirst($pluginName); + + return $pluginName; + } + } \ No newline at end of file diff --git a/plugins/CoreConsole/Commands/GenerateSettings.php b/plugins/CoreConsole/Commands/GenerateSettings.php index 8a024164eafc4e0c9698553c6afb856758e7c07f..fc395e6a235e1ab64c8f7747bfc93050e413048e 100644 --- a/plugins/CoreConsole/Commands/GenerateSettings.php +++ b/plugins/CoreConsole/Commands/GenerateSettings.php @@ -50,31 +50,12 @@ class GenerateSettings extends GeneratePluginBase * @return array * @throws \RunTimeException */ - private function getPluginName(InputInterface $input, OutputInterface $output) + protected function getPluginName(InputInterface $input, OutputInterface $output) { $pluginNames = $this->getPluginNamesHavingNotSpecificFile('Settings.php'); + $invalidName = 'You have to enter the name of an existing plugin which does not already have settings'; - $validate = function ($pluginName) use ($pluginNames) { - if (!in_array($pluginName, $pluginNames)) { - throw new \InvalidArgumentException('You have to enter the name of an existing plugin which does not already have settings'); - } - - return $pluginName; - }; - - $pluginName = $input->getOption('pluginname'); - - if (empty($pluginName)) { - $dialog = $this->getHelperSet()->get('dialog'); - $pluginName = $dialog->askAndValidate($output, 'Enter the name of your plugin: ', $validate, false, null, $pluginNames); - } else { - $validate($pluginName); - } - - $pluginName = ucfirst($pluginName); - - return $pluginName; + return parent::getPluginName($input, $output, $pluginNames, $invalidName); } - } \ No newline at end of file diff --git a/plugins/CoreConsole/Commands/GenerateTest.php b/plugins/CoreConsole/Commands/GenerateTest.php index 486ec0887313a51a4c98d4c1f118a3cc6931347a..49e36d5ccb650f33d5b8f9b2adc4e2dad9fdb5a6 100644 --- a/plugins/CoreConsole/Commands/GenerateTest.php +++ b/plugins/CoreConsole/Commands/GenerateTest.php @@ -105,33 +105,15 @@ class GenerateTest extends GeneratePluginBase /** * @param InputInterface $input * @param OutputInterface $output - * @return string + * @return array * @throws \RunTimeException */ - private function getPluginName(InputInterface $input, OutputInterface $output) + protected function getPluginName(InputInterface $input, OutputInterface $output) { $pluginNames = $this->getPluginNames(); + $invalidName = 'You have to enter the name of an existing plugin'; - $validate = function ($pluginName) use ($pluginNames) { - if (!in_array($pluginName, $pluginNames)) { - throw new \InvalidArgumentException('You have to enter the name of an existing plugin'); - } - - return $pluginName; - }; - - $pluginName = $input->getOption('pluginname'); - - if (empty($pluginName)) { - $dialog = $this->getHelperSet()->get('dialog'); - $pluginName = $dialog->askAndValidate($output, 'Enter the name of your plugin: ', $validate, false, null, $pluginNames); - } else { - $validate($pluginName); - } - - $pluginName = ucfirst($pluginName); - - return $pluginName; + return parent::getPluginName($input, $output, $pluginNames, $invalidName); } /** @@ -149,7 +131,7 @@ class GenerateTest extends GeneratePluginBase return false; } - function getValidTypes() + public function getValidTypes() { return array('unit', 'integration', 'database'); } @@ -163,11 +145,11 @@ class GenerateTest extends GeneratePluginBase { $testtype = $input->getOption('testtype'); - $validate = function ($testtype) { - + $self = $this; - if (empty($testtype) || !in_array($testtype, $this->getValidTypes())) { - throw new \InvalidArgumentException('You have to enter a valid test type: ' . implode(" or ", $this->getValidTypes())); + $validate = function ($testtype) use ($self) { + if (empty($testtype) || !in_array($testtype, $self->getValidTypes())) { + throw new \InvalidArgumentException('You have to enter a valid test type: ' . implode(" or ", $self->getValidTypes())); } return $testtype; }; diff --git a/plugins/CoreConsole/Commands/GenerateVisualizationPlugin.php b/plugins/CoreConsole/Commands/GenerateVisualizationPlugin.php index d18388c6fbd78ed6ca06b76afd3f65d006b76ebd..a206c4bee4bc6cbe5ef155c5b14a000834c34966 100644 --- a/plugins/CoreConsole/Commands/GenerateVisualizationPlugin.php +++ b/plugins/CoreConsole/Commands/GenerateVisualizationPlugin.php @@ -35,24 +35,23 @@ class GenerateVisualizationPlugin extends GeneratePlugin protected function execute(InputInterface $input, OutputInterface $output) { - $pluginName = $this->getPluginName($input, $output); - $visualizationName = $this->getVisualizationName($input, $output); + $pluginName = $this->getPluginName($input, $output); $description = $this->getPluginDescription($input, $output); - $version = $this->getPluginVersion($input, $output); + $version = $this->getPluginVersion($input, $output); + $visualizationName = $this->getVisualizationName($input, $output); $this->generatePluginFolder($pluginName); - $this->generatePluginJson($pluginName, $version, $description, false); $exampleFolder = PIWIK_INCLUDE_PATH . '/plugins/ExampleVisualization'; $replace = array( - 'ExampleVisualization' => $pluginName, - 'SimpleTable' => $visualizationName, - 'simpleTable' => lcfirst($visualizationName), - 'Simple Table' => $visualizationName + 'SimpleTable' => $visualizationName, + 'simpleTable' => lcfirst($visualizationName), + 'Simple Table' => $visualizationName, + 'ExampleVisualization' => $pluginName, + 'ExampleVisualizationDescription' => $description ); - $this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace); - $this->generatePluginFile($pluginName); + $this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles = array()); $this->writeSuccessMessage($output, array( sprintf('Visualization plugin %s %s generated.', $pluginName, $version), diff --git a/plugins/ExampleVisualization/plugin.json b/plugins/ExampleVisualization/plugin.json index a3a5202a337348ea90105ab7b7e719f9c59ea029..1d31a950c77385c435673631d5d3eaf8dfcbf7d1 100644 --- a/plugins/ExampleVisualization/plugin.json +++ b/plugins/ExampleVisualization/plugin.json @@ -1,7 +1,7 @@ { "name": "ExampleVisualization", "version": "0.1.0", - "description": "Example for generating a simple visualization", + "description": "ExampleVisualizationDescription", "theme": false, "license": "GPL-3.0+", "keywords": ["SimpleTable"],