1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-22 15:07:44 +00:00
Files
matomo/plugins/CoreConsole/Commands/GenerateSystemCheck.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

104 خطوط
3.4 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\Common;
use Piwik\Plugin\Manager;
class GenerateSystemCheck extends GeneratePluginBase
{
protected function configure()
{
$this->setName('generate:system-check')
->setDescription('Adds a new system check to an existing plugin')
->addRequiredValueOption('pluginname', null, 'The name of an existing plugin which does not have a menu defined yet')
->addRequiredValueOption('checkname', null, 'The name of the system check you want to create');
}
protected function doExecute(): int
{
$pluginName = $this->getPluginName();
$this->checkAndUpdateRequiredPiwikVersion($pluginName);
$checkName = $this->getSystemCheckName();
$className = ucfirst(str_replace(' ', '', $checkName));
if (!Common::stringEndsWith($className, 'check') && !Common::stringEndsWith($className, 'Check')) {
$className .= 'Check';
}
$exampleFolder = Manager::getPluginDirectory('ExamplePlugin');
$replace = array('ExampleCheck' => $className,
'Example Check' => $checkName,
'ExamplePlugin' => $pluginName,
);
$whitelistFiles = array('/Diagnostic', '/Diagnostic/ExampleCheck.php');
$this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles);
$this->writeSuccessMessage(array(
sprintf('plugins/%s/Diagnostic/%s.php for %s generated.', $pluginName, $className, $pluginName),
sprintf('You should now implement the method called <comment>execute</comment> in %s.php', $className),
'You also need to make the diagnostic check known to Matomo in your "plugins/' . $pluginName . '/config/config.php".',
'Read more about this here: https://developer.matomo.org/guides/system-check',
'Enjoy!'
));
return self::SUCCESS;
}
/**
* @return string
* @throws \RuntimeException
*/
protected function getSystemCheckName()
{
$input = $this->getInput();
$validate = function ($checkName) {
if (empty($checkName)) {
throw new \InvalidArgumentException('Please enter the name of your system check');
}
if (preg_match("/[^A-Za-z0-9 ]/", $checkName)) {
throw new \InvalidArgumentException('Only alpha numerical characters and whitespaces are allowed');
}
return $checkName;
};
$checkName = $input->getOption('checkname');
if (empty($checkName)) {
$checkName = $this->askAndValidate(
'Enter the name of your system check, for example "PDF PHP Extension Check": ',
$validate
);
} else {
$validate($checkName);
}
$checkName = ucfirst($checkName);
return $checkName;
}
/**
* @return string
* @throws \RuntimeException
*/
protected function getPluginName()
{
$pluginNames = $this->getPluginNames();
$invalidName = 'You have to enter a name of an existing plugin.';
return $this->askPluginNameAndValidate($pluginNames, $invalidName);
}
}