File: autoload.php

package info (click to toggle)
matomo-device-detector 6.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,124 kB
  • sloc: php: 7,493; xml: 79; makefile: 15; sh: 3
file content (39 lines) | stat: -rw-r--r-- 1,121 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
<?php

declare(strict_types=1);

/**
 * PSR-4 autoloader implementation for the DeviceDetector namespace.
 * First we define the 'dd_autoload' function, and then we register
 * it with 'spl_autoload_register' so that PHP knows to use it.
 */

/**
 * Automatically include the file that defines <code>class</code>.
 *
 * @param string $class
 *     the name of the class to load
 *
 * @return void
 */
function dd_autoload(string $class): void
{
    if (false === strpos($class, 'DeviceDetector\\')) {
        return;
    }

    $namespaceMap = ['DeviceDetector\\' => __DIR__ . '/'];

    foreach ($namespaceMap as $prefix => $dir) {
        /* First swap out the namespace prefix with a directory... */
        $path = str_replace($prefix, $dir, $class);
        /* replace the namespace separator with a directory separator... */
        $path = str_replace('\\', '/', $path);
        /* and finally, add the PHP file extension to the result. */
        $path .= '.php';
        /* $path should now contain the path to a PHP file defining $class */
        @include $path;
    }
}

spl_autoload_register('dd_autoload');