PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/CoreConsole/Commands/GeneratePluginBase.php

https://github.com/CodeYellowBV/piwik
PHP | 143 lines | 91 code | 29 blank | 23 comment | 10 complexity | d772bd0c6609d22028a74bb7324272a1 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\CoreConsole\Commands;
  10. use Piwik\Filesystem;
  11. use Piwik\Plugin\ConsoleCommand;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. /**
  15. */
  16. abstract class GeneratePluginBase extends ConsoleCommand
  17. {
  18. public function getPluginPath($pluginName)
  19. {
  20. return PIWIK_INCLUDE_PATH . '/plugins/' . ucfirst($pluginName);
  21. }
  22. private function createFolderWithinPluginIfNotExists($pluginName, $folder)
  23. {
  24. $pluginPath = $this->getPluginPath($pluginName);
  25. if (!file_exists($pluginName . $folder)) {
  26. Filesystem::mkdir($pluginPath . $folder);
  27. }
  28. }
  29. protected function createFileWithinPluginIfNotExists($pluginName, $fileName, $content)
  30. {
  31. $pluginPath = $this->getPluginPath($pluginName);
  32. if (!file_exists($pluginPath . $fileName)) {
  33. file_put_contents($pluginPath . $fileName, $content);
  34. }
  35. }
  36. /**
  37. * @param string $templateFolder full path like /home/...
  38. * @param string $pluginName
  39. * @param array $replace array(key => value) $key will be replaced by $value in all templates
  40. * @param array $whitelistFiles If not empty, only given files/directories will be copied.
  41. * For instance array('/Controller.php', '/templates', '/templates/index.twig')
  42. */
  43. protected function copyTemplateToPlugin($templateFolder, $pluginName, array $replace = array(), $whitelistFiles = array())
  44. {
  45. $replace['PLUGINNAME'] = $pluginName;
  46. $files = array_merge(
  47. Filesystem::globr($templateFolder, '*'),
  48. // Also copy files starting with . such as .gitignore
  49. Filesystem::globr($templateFolder, '.*')
  50. );
  51. foreach ($files as $file) {
  52. $fileNamePlugin = str_replace($templateFolder, '', $file);
  53. if (!empty($whitelistFiles) && !in_array($fileNamePlugin, $whitelistFiles)) {
  54. continue;
  55. }
  56. if (is_dir($file)) {
  57. $this->createFolderWithinPluginIfNotExists($pluginName, $fileNamePlugin);
  58. } else {
  59. $template = file_get_contents($file);
  60. foreach ($replace as $key => $value) {
  61. $template = str_replace($key, $value, $template);
  62. }
  63. foreach ($replace as $key => $value) {
  64. $fileNamePlugin = str_replace($key, $value, $fileNamePlugin);
  65. }
  66. $this->createFileWithinPluginIfNotExists($pluginName, $fileNamePlugin, $template);
  67. }
  68. }
  69. }
  70. protected function getPluginNames()
  71. {
  72. $pluginDirs = \_glob(PIWIK_INCLUDE_PATH . '/plugins/*', GLOB_ONLYDIR);
  73. $pluginNames = array();
  74. foreach ($pluginDirs as $pluginDir) {
  75. $pluginNames[] = basename($pluginDir);
  76. }
  77. return $pluginNames;
  78. }
  79. protected function getPluginNamesHavingNotSpecificFile($filename)
  80. {
  81. $pluginDirs = \_glob(PIWIK_INCLUDE_PATH . '/plugins/*', GLOB_ONLYDIR);
  82. $pluginNames = array();
  83. foreach ($pluginDirs as $pluginDir) {
  84. if (!file_exists($pluginDir . '/' . $filename)) {
  85. $pluginNames[] = basename($pluginDir);
  86. }
  87. }
  88. return $pluginNames;
  89. }
  90. /**
  91. * @param InputInterface $input
  92. * @param OutputInterface $output
  93. * @return array
  94. * @throws \RunTimeException
  95. */
  96. protected function askPluginNameAndValidate(InputInterface $input, OutputInterface $output, $pluginNames, $invalidArgumentException)
  97. {
  98. $validate = function ($pluginName) use ($pluginNames, $invalidArgumentException) {
  99. if (!in_array($pluginName, $pluginNames)) {
  100. throw new \InvalidArgumentException($invalidArgumentException);
  101. }
  102. return $pluginName;
  103. };
  104. $pluginName = $input->getOption('pluginname');
  105. if (empty($pluginName)) {
  106. $dialog = $this->getHelperSet()->get('dialog');
  107. $pluginName = $dialog->askAndValidate($output, 'Enter the name of your plugin: ', $validate, false, null, $pluginNames);
  108. } else {
  109. $validate($pluginName);
  110. }
  111. $pluginName = ucfirst($pluginName);
  112. return $pluginName;
  113. }
  114. }