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

Loader.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 927fbdc797f58594dc16a1d51b25ede28b4d9706 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?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
 * @package Piwik
 */

namespace Piwik;

use Exception;

/**
 * Piwik auto loader
 *
 * @package Piwik
 */
class Loader
{
    // our class search path; current directory is intentionally excluded
    protected static $dirs = array('/core/', '/plugins/');

    /**
     * Get class file name
     *
     * @param string $class Class name
     * @return string Class file name
     * @throws Exception if class name is invalid
     */
    protected static function  getClassFileName($class)
    {
        if (!preg_match('/^[A-Za-z0-9_\\\\]+$/D', $class)) {
            throw new Exception("Invalid class name \"$class\".");
        }

        // prefixed class
        $class = str_replace('_', '/', $class);

        // namespace \Piwik\Common
        $class = str_replace('\\', '/', $class);

        if ($class == 'Piwik') {
            return $class;
        }

        $class = self::removePrefix($class, 'Piwik/');
        $class = self::removePrefix($class, 'Plugins/');
        return $class;
    }

    protected static function removePrefix($class, $vendorPrefixToRemove)
    {
        if (strpos($class, $vendorPrefixToRemove) === 0) {
            return substr($class, strlen($vendorPrefixToRemove));
        }

        return $class;
    }

    /**
     * Load class by name
     *
     * @param string $class Class name
     * @throws Exception if class not found
     */
    public static function loadClass($class)
    {
        $classPath = self::getClassFileName($class);
        if (strpos($class, '\Piwik') === 0
            || strpos($class, 'Piwik') === 0)
        {
            // Piwik classes are in core/ or plugins/
            do {
                // auto-discover class location
                foreach (self::$dirs as $dir) {
                    $path = PIWIK_INCLUDE_PATH . $dir . $classPath . '.php';
                    if (file_exists($path)) {
                        require_once $path; // prefixed by PIWIK_INCLUDE_PATH
                        if (class_exists($class, false) || interface_exists($class, false)) {
                            return;
                        }
                    }
                }

                // truncate to find file with multiple class definitions
                $lastSlash = strrpos($classPath, '/');
                $classPath = ($lastSlash === false) ? '' : substr($classPath, 0, $lastSlash);
            } while (!empty($classPath));
        } else {
            // non-Piwik classes (e.g., Zend Framework) are in libs/
            $path = PIWIK_INCLUDE_PATH . '/libs/' . $classPath . '.php';
            if (file_exists($path)) {
                require_once $path; // prefixed by PIWIK_INCLUDE_PATH
                if (class_exists($class, false) || interface_exists($class, false)) {
                    return;
                }
            }
        }
        throw new Exception("Class \"$class\" not found.");
    }

    /**
     * Autoloader
     *
     * @param string $class Class name
     */
    public static function autoload($class)
    {
        try {
            self::loadClass($class);
        } catch (Exception $e) {
        }
    }
}

// Note: only one __autoload per PHP instance
if (function_exists('spl_autoload_register')) {
    // use the SPL autoload stack
    spl_autoload_register(array('Piwik\Loader', 'autoload'));

    // preserve any existing __autoload
    if (function_exists('__autoload')) {
        spl_autoload_register('__autoload');
    }
} else {
    function __autoload($class)
    {
        Loader::autoload($class);
    }
}