Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Development.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 880b55ea83073d223626b569e109ab96b4bb8948 (plain)
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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik;

use \Exception;

/**
 * Development related checks and tools
 */
class Development
{
    private static $isEnabled = null;

    /**
     * Returns `true` if segmentation is allowed for this user, `false` if otherwise.
     *
     * @return bool
     * @api
     */
    public static function isEnabled()
    {
        if (is_null(self::$isEnabled)) {
            self::$isEnabled = (bool) Config::getInstance()->General['development_mode'];
        }

        return self::$isEnabled;
    }

    public static function methodExists($classOrInstance, $method)
    {
        if (is_string($classOrInstance)) {
            return class_exists($classOrInstance) && method_exists($classOrInstance, $method);
        }

        return method_exists($classOrInstance, $method);
    }

    public static function formatMethodCall($classOrInstance, $method)
    {
        if (is_object($classOrInstance)) {
            $classOrInstance = get_class($classOrInstance);
        }

        return $classOrInstance . '::' . $method . '()';
    }

    public static function checkMethodIsCallable($classOrInstance, $method, $prefixMessageIfError)
    {
        if (!self::isEnabled()) {
            return;
        }

        if (!self::methodExists($classOrInstance, $method)) {
            self::error($prefixMessageIfError . ' "' . self::formatMethodCall($classOrInstance, $method) .  '" does not exist. Please make sure to define such a method.');
        }

        if (!self::isCallableMethod($classOrInstance, $method)) {
            self::error($prefixMessageIfError . ' "' . self::formatMethodCall($classOrInstance, $method) .  '" is not callable. Please make sure to method is public');

        }
    }

    public static function isCallableMethod($classOrInstance, $method)
    {
        if (!self::methodExists($classOrInstance, $method)) {
            return false;
        }

        $reflection = new \ReflectionMethod($classOrInstance, $method);
        return $reflection->isPublic();
    }

    public static function error($message)
    {
        $message .= ' (This error is only triggered in development mode. Your plugin still works when development mode is disabled but will lead in an error at some point. We highly recommend to fix this issue!)';
        throw new Exception($message);
    }
}