Last active
August 29, 2015 14:04
-
-
Save niraj-shah/444f00f5527cdd633ed6 to your computer and use it in GitHub Desktop.
Autoloader for Official Parse PHP SDK
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 | |
/** | |
* You only need this file if you are not using composer. | |
* Adapted from the Facebook PHP SDK 4.0.x autoloader | |
*/ | |
if (version_compare(PHP_VERSION, '5.4.0', '<')) { | |
throw new Exception('The Parse SDK requires PHP version 5.4 or higher.'); | |
} | |
/** | |
* Register the autoloader for the Parse SDK | |
* Based off the official PSR-4 autoloader example found here: | |
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md | |
* | |
* @param string $class The fully-qualified class name. | |
* @return void | |
*/ | |
spl_autoload_register(function ($class) | |
{ | |
// Parse class prefix | |
$prefix = 'Parse\\'; | |
// base directory for the namespace prefix | |
$base_dir = defined('PARSE_SDK_DIR') ? PARSE_SDK_DIR : __DIR__ . '/Parse/'; | |
// does the class use the namespace prefix? | |
$len = strlen( $prefix ); | |
if ( strncmp($prefix, $class, $len) !== 0 ) { | |
// no, move to the next registered autoloader | |
return; | |
} | |
// get the relative class name | |
$relative_class = substr( $class, $len ); | |
// replace the namespace prefix with the base directory, replace namespace | |
// separators with directory separators in the relative class name, append | |
// with .php | |
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php'; | |
// echo $relative_class . '<br/>'; | |
// if the file exists, require it | |
if ( file_exists( $file ) ) { | |
require $file; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment