File: GenerateSystemCheck.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (103 lines) | stat: -rw-r--r-- 3,480 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?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);
    }
}