Last active
January 14, 2016 20:00
-
-
Save sshilko/edaf7863d4738fb51868 to your computer and use it in GitHub Desktop.
autoload_psr0_lazy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PSR-0 Autoloader | |
* http://www.php-fig.org/psr/psr-0/ | |
* works PHP5 & PHP7 | |
* @sshilko | |
*/ | |
function autoload_psr0($className) | |
{ | |
$oname = $className; | |
$className = ltrim($className, '\\'); | |
$fileName = ''; | |
if ($lastNsPos = strrpos($className, '\\')) { | |
$namespace = substr($className, 0, $lastNsPos); | |
$className = substr($className, $lastNsPos + 1); | |
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; | |
} | |
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; | |
$filePath = stream_resolve_include_path($fileName); | |
if ($filePath) { | |
/** | |
* put your mostly used files high in include_path | |
* some frequent packages are directly mapped | |
* to prevent full composer autoloaded | |
*/ | |
return include $filePath; | |
} else { | |
/** | |
* Only need autoloader once | |
* check correct paths | |
*/ | |
if (!isset($GLOBALS['composer_autoloaded'])) { | |
$GLOBALS['composer_autoloaded'] = true; | |
$loader = require __DIR__ . '/../composer/autoload.php'; | |
$loader->loadClass($oname); | |
} | |
return false; | |
} | |
} | |
//include __DIR__ . '/../composer/autoload.php'; | |
spl_autoload_register('autoload_psr0'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment