xref: /plugin/statistics/vendor/matomo/device-detector/autoload.php (revision d5ef99ddb7dfb0cfae33e9257bd1d788f682c50f)
1*d5ef99ddSAndreas Gohr<?php
2*d5ef99ddSAndreas Gohr
3*d5ef99ddSAndreas Gohrdeclare(strict_types=1);
4*d5ef99ddSAndreas Gohr
5*d5ef99ddSAndreas Gohr/**
6*d5ef99ddSAndreas Gohr * PSR-4 autoloader implementation for the DeviceDetector namespace.
7*d5ef99ddSAndreas Gohr * First we define the 'dd_autoload' function, and then we register
8*d5ef99ddSAndreas Gohr * it with 'spl_autoload_register' so that PHP knows to use it.
9*d5ef99ddSAndreas Gohr */
10*d5ef99ddSAndreas Gohr
11*d5ef99ddSAndreas Gohr/**
12*d5ef99ddSAndreas Gohr * Automatically include the file that defines <code>class</code>.
13*d5ef99ddSAndreas Gohr *
14*d5ef99ddSAndreas Gohr * @param string $class
15*d5ef99ddSAndreas Gohr *     the name of the class to load
16*d5ef99ddSAndreas Gohr *
17*d5ef99ddSAndreas Gohr * @return void
18*d5ef99ddSAndreas Gohr */
19*d5ef99ddSAndreas Gohrfunction dd_autoload(string $class): void
20*d5ef99ddSAndreas Gohr{
21*d5ef99ddSAndreas Gohr    if (false === strpos($class, 'DeviceDetector\\')) {
22*d5ef99ddSAndreas Gohr        return;
23*d5ef99ddSAndreas Gohr    }
24*d5ef99ddSAndreas Gohr
25*d5ef99ddSAndreas Gohr    $namespaceMap = ['DeviceDetector\\' => __DIR__ . '/'];
26*d5ef99ddSAndreas Gohr
27*d5ef99ddSAndreas Gohr    foreach ($namespaceMap as $prefix => $dir) {
28*d5ef99ddSAndreas Gohr        /* First swap out the namespace prefix with a directory... */
29*d5ef99ddSAndreas Gohr        $path = str_replace($prefix, $dir, $class);
30*d5ef99ddSAndreas Gohr        /* replace the namespace separator with a directory separator... */
31*d5ef99ddSAndreas Gohr        $path = str_replace('\\', '/', $path);
32*d5ef99ddSAndreas Gohr        /* and finally, add the PHP file extension to the result. */
33*d5ef99ddSAndreas Gohr        $path .= '.php';
34*d5ef99ddSAndreas Gohr        /* $path should now contain the path to a PHP file defining $class */
35*d5ef99ddSAndreas Gohr        require $path;
36*d5ef99ddSAndreas Gohr    }
37*d5ef99ddSAndreas Gohr}
38*d5ef99ddSAndreas Gohr
39*d5ef99ddSAndreas Gohrspl_autoload_register('dd_autoload');
40