1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-21 22:47:43 +00:00
Files
matomo/plugins/CoreConsole/Commands/BuildTracker.php
Michal Kleiner 9a3ef94df6 [Coding Style] Enable rule PSR12.Files.FileHeader + unify file headers (#22132)
* [Coding Style] Enable rule PSR12.Files.FileHeader

* Apply CS

* Replace Piwik with Matomo in file headers

* Unify file headers (position, no. of lines, https links)

* Rebuild dist files

* Apply CS

* Fix system test that relies on line numbers in a file that had the file header updated

---------

Co-authored-by: Stefan Giehl <stefan@matomo.org>
2024-04-20 20:50:47 +02:00

88 خطوط
2.9 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CoreConsole\Commands;
use Piwik\Http;
use Piwik\Plugin\ConsoleCommand;
use Piwik\Plugin\Manager;
use Piwik\Unzip;
class BuildTracker extends ConsoleCommand
{
public const YUI_COMPRESSOR_URL = 'https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.zip';
protected function configure()
{
$this->setName('development:build-tracker-js');
$this->setDescription('Minifies tracker JavaScript for Matomo core or a single plugin.');
$this->addRequiredValueOption('plugin', null, 'The plugin to minify. If not supplied, minifies core tracker JS.');
}
public function isEnabled()
{
return \Piwik\Development::isEnabled();
}
protected function doExecute(): int
{
$plugin = $this->getInput()->getOption('plugin');
if ($plugin && !Manager::getInstance()->isPluginInFilesystem($plugin)) {
throw new \InvalidArgumentException("Invalid plugin '$plugin'");
}
$this->installYuiCompressorIfNeeded();
$this->compress($plugin);
return self::SUCCESS;
}
private function compress($plugin)
{
$output = $this->getOutput();
$output->writeln("Minifying...");
$jsPath = PIWIK_INCLUDE_PATH . '/js';
if (!$plugin) {
$command = "cd $jsPath && sed '/<DEBUG>/,/<\\/DEBUG>/d' < piwik.js | sed 's/eval/replacedEvilString/' | java -jar yuicompressor-2.4.8.jar --type js --line-break 1000 | sed 's/replacedEvilString/eval/' | sed 's/^[/][*]/\\/*!/' > piwik.min.js && cp piwik.min.js ../piwik.js && cp piwik.min.js ../matomo.js";
if ($output->isVerbose()) {
$output->writeln("Command: $command");
}
passthru($command);
return;
}
$pluginTrackerJs = PIWIK_INCLUDE_PATH . '/plugins/' . $plugin . '/tracker.js';
$pluginTrackerMinJs = PIWIK_INCLUDE_PATH . '/plugins/' . $plugin . '/tracker.min.js';
$command = "cd $jsPath && cat $pluginTrackerJs | java -jar yuicompressor-2.4.8.jar --type js --line-break 1000 | sed 's/^[/][*]/\\/*!/' > $pluginTrackerMinJs";
if ($output->isVerbose()) {
$output->writeln("Command: $command");
}
passthru($command);
$output->writeln("Done.");
}
private function installYuiCompressorIfNeeded()
{
$zipPath = PIWIK_INCLUDE_PATH . '/js/yuicompressor-2.4.8.zip';
$jarPath = PIWIK_INCLUDE_PATH . '/js/yuicompressor-2.4.8.jar';
if (is_file($jarPath)) {
return;
}
$this->getOutput()->writeln("Downloading YUI compressor...");
Http::fetchRemoteFile(self::YUI_COMPRESSOR_URL, $zipPath);
$unzip = Unzip::factory('PclZip', $zipPath);
$unzip->extract(PIWIK_INCLUDE_PATH . '/js/');
}
}