Skip to content

Commit

Permalink
Class autoloader added
Browse files Browse the repository at this point in the history
  • Loading branch information
SteeinSource committed Jul 26, 2017
1 parent cc59b21 commit 6738724
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/Steein/SDK/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* You only need this file if you are not using composer.
* Why are you not using composer?
* https://getcomposer.org/
*/

if (version_compare(PHP_VERSION, '5.5.0', '<')) {
throw new Exception('The Steein SDK requires PHP version 5.5 or higher.');
}

require_once __DIR__ . '/functions.php';

/**
* Register the autoloader for the Steein SDK classes.
*
* 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) {
// project-specific namespace prefix
$prefix = 'Steein\\SDK\\';

// For backwards compatibility
$customBaseDir = '';
// @todo v6: Remove support for 'Steein_SDK_V4_SRC_DIR'
if (defined('Steein_SDK_V4_SRC_DIR')) {
$customBaseDir = Steein_SDK_V4_SRC_DIR;
} elseif (defined('Steein_SDK_SRC_DIR')) {
$customBaseDir = Steein_SDK_SRC_DIR;
}
// base directory for the namespace prefix
$baseDir = $customBaseDir ?: __DIR__ . '/';

// 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
$relativeClass = 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 = rtrim($baseDir, '/') . '/' . str_replace('\\', '/', $relativeClass) . '.php';

// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});

0 comments on commit 6738724

Please sign in to comment.