Skip to content

Commit

Permalink
refs matomo-org#4241 let plugins add new console commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Nov 11, 2013
1 parent 0203250 commit 1a3b068
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 39 deletions.
1 change: 1 addition & 0 deletions config/global.ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@
Plugins[] = SitesManager
Plugins[] = Installation
Plugins[] = CoreUpdater
Plugins[] = CoreConsole
Plugins[] = ScheduledReports
Plugins[] = UserCountryMap
Plugins[] = Live
Expand Down
3 changes: 3 additions & 0 deletions console
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ require_once PIWIK_INCLUDE_PATH . '/vendor/autoload.php';
require_once PIWIK_INCLUDE_PATH . '/core/Loader.php';
require_once PIWIK_INCLUDE_PATH . '/libs/upgradephp/upgrade.php';

// load all plugins
Piwik\Plugin\Manager::getInstance()->returnLoadedPluginsInfo();

if (!Piwik\Common::isPhpCliMode()) {
exit;
}
Expand Down
76 changes: 37 additions & 39 deletions core/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,51 @@
*/
namespace Piwik;

use Piwik\Plugins\CoreConsole\CodeCoverage;
use Piwik\Plugins\CoreConsole\GenerateApi;
use Piwik\Plugins\CoreConsole\GenerateController;
use Piwik\Plugins\CoreConsole\GeneratePlugin;
use Piwik\Plugins\CoreConsole\GenerateSettings;
use Piwik\Plugins\CoreConsole\GenerateVisualizationPlugin;
use Piwik\Plugins\CoreConsole\GitCommit;
use Piwik\Plugins\CoreConsole\GitPull;
use Piwik\Plugins\CoreConsole\GitPush;
use Piwik\Plugins\CoreConsole\RunTests;
use Piwik\Plugins\CoreConsole\Translations\CreatePull;
use Piwik\Plugins\CoreConsole\Translations\FetchFromOTrance;
use Piwik\Plugins\CoreConsole\Translations\LanguageCodes;
use Piwik\Plugins\CoreConsole\Translations\LanguageNames;
use Piwik\Plugins\CoreConsole\Translations\PluginsWithTranslations;
use Piwik\Plugins\CoreConsole\Translations\SetTranslations;
use Piwik\Plugins\CoreConsole\Translations\Update;
use Piwik\Plugins\CoreConsole\WatchLog;
use Piwik\Plugins\CoreConsole\GenerateTest;
use Symfony\Component\Console\Application;

class Console
{
public function run()
{
$console = new Application();
$console = new Application();
$commands = $this->getAvailableCommands();

$console->add(new RunTests());
$console->add(new GeneratePlugin());
$console->add(new GenerateApi());
$console->add(new GenerateSettings());
$console->add(new GenerateController());
$console->add(new GenerateVisualizationPlugin());
$console->add(new GenerateTest());
$console->add(new WatchLog());
$console->add(new GitPull());
$console->add(new GitCommit());
$console->add(new GitPush());
$console->add(new PluginsWithTranslations());
$console->add(new LanguageCodes());
$console->add(new LanguageNames());
$console->add(new FetchFromOTrance());
$console->add(new SetTranslations());
$console->add(new Update());
$console->add(new CreatePull());
$console->add(new CodeCoverage());
foreach ($commands as $command) {
if (class_exists($command)) {
$console->add(new $command);
} else {
Log::warning(sprintf('Cannot add command %s, class does not exist.', $command));
}
}

$console->run();
}

/**
* Returns a list of available command classnames.
*
* @return string[]
*/
private function getAvailableCommands()
{
$commands = array();

/**
* Triggered when gathering all available console commands. Plugins that want to expose new console commands
* should subscribe to this event and add commands to the incoming array.
*
* **Example**
* ```
* public function addConsoleCommands(&$commands)
* {
* $commands[] = 'Piwik\Plugins\MyPlugin\Commands\MyCommand';
* }
* ```
*
* @param array &$commands An array containing a list of command classnames.
*/
Piwik::postEvent('Console.addCommands', array(&$commands));

return $commands;
}
}
50 changes: 50 additions & 0 deletions plugins/CoreConsole/CoreConsole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik_Plugins
* @package CoreConsole
*/
namespace Piwik\Plugins\CoreConsole;

/**
* @package CoreConsole
*/
class CoreConsole extends \Piwik\Plugin
{
/**
* @see Piwik_Plugin::getListHooksRegistered
*/
public function getListHooksRegistered()
{
return array(
'Console.addCommands' => 'addConsoleCommands'
);
}

public function addConsoleCommands(&$commands)
{
$commands[] = 'Piwik\Plugins\CoreConsole\CodeCoverage';
$commands[] = 'Piwik\Plugins\CoreConsole\GenerateApi';
$commands[] = 'Piwik\Plugins\CoreConsole\GenerateController';
$commands[] = 'Piwik\Plugins\CoreConsole\GeneratePlugin';
$commands[] = 'Piwik\Plugins\CoreConsole\GenerateSettings';
$commands[] = 'Piwik\Plugins\CoreConsole\GenerateVisualizationPlugin';
$commands[] = 'Piwik\Plugins\CoreConsole\GitCommit';
$commands[] = 'Piwik\Plugins\CoreConsole\GitPull';
$commands[] = 'Piwik\Plugins\CoreConsole\GitPush';
$commands[] = 'Piwik\Plugins\CoreConsole\RunTests';
$commands[] = 'Piwik\Plugins\CoreConsole\Translations\CreatePull';
$commands[] = 'Piwik\Plugins\CoreConsole\Translations\FetchFromOTrance';
$commands[] = 'Piwik\Plugins\CoreConsole\Translations\LanguageCodes';
$commands[] = 'Piwik\Plugins\CoreConsole\Translations\LanguageNames';
$commands[] = 'Piwik\Plugins\CoreConsole\Translations\PluginsWithTranslations';
$commands[] = 'Piwik\Plugins\CoreConsole\Translations\SetTranslations';
$commands[] = 'Piwik\Plugins\CoreConsole\Translations\Update';
$commands[] = 'Piwik\Plugins\CoreConsole\WatchLog';
$commands[] = 'Piwik\Plugins\CoreConsole\GenerateTest';
}
}

0 comments on commit 1a3b068

Please sign in to comment.