Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Console.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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;
  10. use Piwik\Plugin\Manager as PluginManager;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Input\ArgvInput;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Input\StringInput;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class Console extends Application
  18. {
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $option = new InputOption('piwik-domain',
  23. null,
  24. InputOption::VALUE_OPTIONAL,
  25. 'Piwik URL (protocol and domain) eg. "http://piwik.example.org"'
  26. );
  27. $this->getDefinition()->addOption($option);
  28. }
  29. public function init()
  30. {
  31. $this->checkCompatibility();
  32. }
  33. public function doRun(InputInterface $input, OutputInterface $output)
  34. {
  35. $this->initPiwikHost($input);
  36. $this->initConfig($output);
  37. try {
  38. self::initPlugins();
  39. } catch(\Exception $e) {
  40. // Piwik not installed yet, no config file?
  41. }
  42. Translate::reloadLanguage('en');
  43. $commands = $this->getAvailableCommands();
  44. foreach ($commands as $command) {
  45. if (!class_exists($command)) {
  46. Log::warning(sprintf('Cannot add command %s, class does not exist', $command));
  47. } elseif (!is_subclass_of($command, 'Piwik\Plugin\ConsoleCommand')) {
  48. Log::warning(sprintf('Cannot add command %s, class does not extend Piwik\Plugin\ConsoleCommand', $command));
  49. } else {
  50. $this->add(new $command);
  51. }
  52. }
  53. return parent::doRun($input, $output);
  54. }
  55. /**
  56. * Returns a list of available command classnames.
  57. *
  58. * @return string[]
  59. */
  60. private function getAvailableCommands()
  61. {
  62. $commands = $this->getDefaultPiwikCommands();
  63. $detected = PluginManager::getInstance()->findMultipleComponents('Commands', 'Piwik\\Plugin\\ConsoleCommand');
  64. $commands = array_merge($commands, $detected);
  65. /**
  66. * Triggered to filter / restrict console commands. Plugins that want to restrict commands
  67. * should subscribe to this event and remove commands from the existing list.
  68. *
  69. * **Example**
  70. *
  71. * public function filterConsoleCommands(&$commands)
  72. * {
  73. * $key = array_search('Piwik\Plugins\MyPlugin\Commands\MyCommand', $commands);
  74. * if (false !== $key) {
  75. * unset($commands[$key]);
  76. * }
  77. * }
  78. *
  79. * @param array &$commands An array containing a list of command class names.
  80. */
  81. Piwik::postEvent('Console.filterCommands', array(&$commands));
  82. $commands = array_values(array_unique($commands));
  83. return $commands;
  84. }
  85. private function checkCompatibility()
  86. {
  87. if (Common::isPhpCgiType()) {
  88. echo 'Piwik Console is known to be not compatible with PHP-CGI (you are using '.php_sapi_name().'). ' .
  89. 'Please execute console using PHP-CLI. For instance "/usr/bin/php-cli console ..."';
  90. echo "\n";
  91. exit(1);
  92. }
  93. }
  94. public static function isSupported()
  95. {
  96. return Common::isPhpCliMode() && !Common::isPhpCgiType();
  97. }
  98. protected function initPiwikHost(InputInterface $input)
  99. {
  100. $piwikHostname = $input->getParameterOption('--piwik-domain');
  101. if (empty($piwikHostname)) {
  102. $piwikHostname = $input->getParameterOption('--url');
  103. }
  104. $piwikHostname = UrlHelper::getHostFromUrl($piwikHostname);
  105. Url::setHost($piwikHostname);
  106. }
  107. protected function initConfig(OutputInterface $output)
  108. {
  109. $config = Config::getInstance();
  110. try {
  111. $config->checkLocalConfigFound();
  112. return $config;
  113. } catch (\Exception $e) {
  114. $output->writeln($e->getMessage() . "\n");
  115. }
  116. }
  117. public static function initPlugins()
  118. {
  119. Plugin\Manager::getInstance()->loadActivatedPlugins();
  120. }
  121. private function getDefaultPiwikCommands()
  122. {
  123. $commands = array(
  124. 'Piwik\CliMulti\RequestCommand'
  125. );
  126. if (class_exists('Piwik\Plugins\EnterpriseAdmin\EnterpriseAdmin')) {
  127. $extra = new \Piwik\Plugins\EnterpriseAdmin\EnterpriseAdmin();
  128. $extra->addConsoleCommands($commands);
  129. }
  130. return $commands;
  131. }
  132. }