Skip to content

Added spl autoloader for toggle objects. #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/TogglAutoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Toggl SPL Autoloader
*
* Allow class autoloading using SPL. Only loads classes that begin with 'toggl' and is
* case insensitive.
*
* @file
* @link http://php.net/manual/en/function.spl-autoload-register.php
* @author Timothy M. Crider <timcrider@gmail.com>
*/

/**
* Use current directory of the autoloader file as the base toggl directory
*/
if (!defined('TOGGL_DIR')) {
define('TOGGL_DIR', dirname(__FILE__).'/');
}

/**
* Autoloader function
*/
function togglAutoload($class) {
// Do not try to load non toggl classes
if (!preg_match('/^toggl/i', $class)) {
return false;
}

$tryFile = sprintf("%s%s.php", TOGGL_DIR, $class);
return (file_exists($tryFile) && is_readable($tryFile)) ? include $tryFile : false;
}

/**
* Register the autoloader with PHP
*/
spl_autoload_register('togglAutoload');