Newer
Older
Thomas Steur
a validé
<?php
/**
* Piwik - free/libre analytics platform
Thomas Steur
a validé
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik;
use Piwik\Application\Environment;
use Piwik\Config\ConfigNotFoundException;
use Piwik\Container\StaticContainer;
use Piwik\Plugin\Manager as PluginManager;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
Thomas Steur
a validé
use Symfony\Component\Console\Application;
diosmosis
a validé
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Thomas Steur
a validé
class Console extends Application
Thomas Steur
a validé
{
/**
* @var Environment
*/
private $environment;
public function __construct()
Thomas Steur
a validé
{
$this->setServerArgsIfPhpCgi();
parent::__construct();
$option = new InputOption('piwik-domain',
null,
InputOption::VALUE_OPTIONAL,
'Piwik URL (protocol and domain) eg. "http://piwik.example.org"'
);
$this->getDefinition()->addOption($option);
$option = new InputOption('xhprof',
null,
InputOption::VALUE_NONE,
'Enable profiling with XHProf'
);
$this->getDefinition()->addOption($option);
}
public function doRun(InputInterface $input, OutputInterface $output)
{
if ($input->hasParameterOption('--xhprof')) {
Profiler::setupProfilerXHProf(true, true);
}
$this->initPiwikHost($input);
diosmosis
a validé
$this->initEnvironment($output);
Thomas Steur
a validé
try {
self::initPlugins();
} catch (ConfigNotFoundException $e) {
// Piwik not installed yet, no config file?
Log::warning($e->getMessage());
$commands = $this->getAvailableCommands();
Thomas Steur
a validé
foreach ($commands as $command) {
Thomas Steur
a validé
$this->addCommandIfExists($command);
Thomas Steur
a validé
$self = $this;
return Access::doAsSuperUser(function () use ($input, $output, $self) {
return call_user_func(array($self, 'Symfony\Component\Console\Application::doRun'), $input, $output);
diosmosis
a validé
});
Thomas Steur
a validé
}
Thomas Steur
a validé
private function addCommandIfExists($command)
{
if (!class_exists($command)) {
Log::warning(sprintf('Cannot add command %s, class does not exist', $command));
diosmosis
a validé
} else if (!is_subclass_of($command, 'Piwik\Plugin\ConsoleCommand')) {
Thomas Steur
a validé
Log::warning(sprintf('Cannot add command %s, class does not extend Piwik\Plugin\ConsoleCommand', $command));
} else {
diosmosis
a validé
/** @var Command $commandInstance */
$commandInstance = new $command;
// do not add the command if it already exists; this way we can add the command ourselves in tests
diosmosis
a validé
if (!$this->has($commandInstance->getName())) {
$this->add($commandInstance);
}
Thomas Steur
a validé
}
}
/**
* Returns a list of available command classnames.
*
* @return string[]
*/
private function getAvailableCommands()
{
$commands = $this->getDefaultPiwikCommands();
Thomas Steur
a validé
$detected = PluginManager::getInstance()->findMultipleComponents('Commands', 'Piwik\\Plugin\\ConsoleCommand');
Thomas Steur
a validé
$commands = array_merge($commands, $detected);
Thomas Steur
a validé
Thomas Steur
a validé
* Triggered to filter / restrict console commands. Plugins that want to restrict commands
* should subscribe to this event and remove commands from the existing list.
Thomas Steur
a validé
* public function filterConsoleCommands(&$commands)
Thomas Steur
a validé
* $key = array_search('Piwik\Plugins\MyPlugin\Commands\MyCommand', $commands);
* if (false !== $key) {
* unset($commands[$key]);
* }
* @param array &$commands An array containing a list of command class names.
Thomas Steur
a validé
Piwik::postEvent('Console.filterCommands', array(&$commands));
$commands = array_values(array_unique($commands));
return $commands;
}
private function setServerArgsIfPhpCgi()
{
if (Common::isPhpCgiType()) {
$_SERVER['argv'] = array();
foreach ($_GET as $name => $value) {
$argument = $name;
if (!empty($value)) {
$argument .= '=' . $value;
}
$_SERVER['argv'][] = $argument;
}
if (!defined('STDIN')) {
define('STDIN', fopen('php://stdin','r'));
}
}
}
public static function isSupported()
{
return Common::isPhpCliMode() && !Common::isPhpCgiType();
}
protected function initPiwikHost(InputInterface $input)
$piwikHostname = $input->getParameterOption('--piwik-domain');
Thomas Steur
a validé
if (empty($piwikHostname)) {
$piwikHostname = $input->getParameterOption('--url');
}
$piwikHostname = UrlHelper::getHostFromUrl($piwikHostname);
Url::setHost($piwikHostname);
}
diosmosis
a validé
protected function initEnvironment(OutputInterface $output)
diosmosis
a validé
$this->environment = new Environment('cli');
$this->environment->init();
$config = Config::getInstance();
return $config;
} catch (\Exception $e) {
$output->writeln($e->getMessage() . "\n");
/**
* Register the console output into the logger.
*
* Ideally, this should be done automatically with events:
* @see http://symfony.com/fr/doc/current/components/console/events.html
* @see Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand()
* But it would require to install Symfony's Event Dispatcher.
*/
private function initLoggerOutput(OutputInterface $output)
{
/** @var ConsoleHandler $consoleLogHandler */
$consoleLogHandler = StaticContainer::get('Symfony\Bridge\Monolog\Handler\ConsoleHandler');
$consoleLogHandler->setOutput($output);
}
mattab
a validé
Plugin\Manager::getInstance()->loadActivatedPlugins();
diosmosis
a validé
Plugin\Manager::getInstance()->loadPluginTranslations();
private function getDefaultPiwikCommands()
{
$commands = array(
'Piwik\CliMulti\RequestCommand'
);
if (class_exists('Piwik\Plugins\EnterpriseAdmin\EnterpriseAdmin')) {
$extra = new \Piwik\Plugins\EnterpriseAdmin\EnterpriseAdmin();
Thomas Steur
a validé
Thomas Steur
a validé