1*0b7ac7c9SAndreas Gohr<?php 2*0b7ac7c9SAndreas Gohr 3*0b7ac7c9SAndreas Gohr/** 4*0b7ac7c9SAndreas Gohr * Simple PSR-4 autoloader 5*0b7ac7c9SAndreas Gohr * 6*0b7ac7c9SAndreas Gohr * Based on PSR-4 example code 7*0b7ac7c9SAndreas Gohr * 8*0b7ac7c9SAndreas Gohr * @link http://www.php-fig.org/psr/psr-4/examples/ 9*0b7ac7c9SAndreas Gohr * @param string $class The fully-qualified class name. 10*0b7ac7c9SAndreas Gohr * @return void 11*0b7ac7c9SAndreas Gohr */ 12*0b7ac7c9SAndreas Gohrspl_autoload_register(function($class) { 13*0b7ac7c9SAndreas Gohr $namespaces = array( 14*0b7ac7c9SAndreas Gohr 'Psr\\Log\\' => __DIR__ . '/subtree/php-fig/log/Psr/Log/', 15*0b7ac7c9SAndreas Gohr 'Tx\\' => __DIR__ . '/subtree/txtthinking/Mailer/src/', 16*0b7ac7c9SAndreas Gohr 'splitbrain\\dokuwiki\\plugin\\smtp\\' => __DIR__ . '/classes/' 17*0b7ac7c9SAndreas Gohr ); 18*0b7ac7c9SAndreas Gohr 19*0b7ac7c9SAndreas Gohr foreach($namespaces as $prefix => $base_dir) { 20*0b7ac7c9SAndreas Gohr // does the class use the namespace prefix? 21*0b7ac7c9SAndreas Gohr $len = strlen($prefix); 22*0b7ac7c9SAndreas Gohr if (strncmp($prefix, $class, $len) !== 0) { 23*0b7ac7c9SAndreas Gohr // no, move to the next 24*0b7ac7c9SAndreas Gohr continue; 25*0b7ac7c9SAndreas Gohr } 26*0b7ac7c9SAndreas Gohr 27*0b7ac7c9SAndreas Gohr // get the relative class name 28*0b7ac7c9SAndreas Gohr $relative_class = substr($class, $len); 29*0b7ac7c9SAndreas Gohr 30*0b7ac7c9SAndreas Gohr // replace the namespace prefix with the base directory, replace namespace 31*0b7ac7c9SAndreas Gohr // separators with directory separators in the relative class name, append 32*0b7ac7c9SAndreas Gohr // with .php 33*0b7ac7c9SAndreas Gohr $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; 34*0b7ac7c9SAndreas Gohr 35*0b7ac7c9SAndreas Gohr // if the file exists, require it 36*0b7ac7c9SAndreas Gohr if (file_exists($file)) { 37*0b7ac7c9SAndreas Gohr require $file; 38*0b7ac7c9SAndreas Gohr } 39*0b7ac7c9SAndreas Gohr } 40*0b7ac7c9SAndreas Gohr}); 41