Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * // set configuration settings
- *
- * autoloader([[
- * 'debug' => true, // set debug mode on/off
- * 'basepath' => '/var/www/myproject', // set project base path
- * 'extensions' => ['.php'], // set allowed class extension(s) to load
- * // 'extensions' => ['.php', '.php4', '.php5'], // example of multiple extensions
- * // use namespace if autoloader function is in namespace for registering autoloader
- * 'namespace' => 'My\Namespace',
- 'verbose' => false // will print internal messages (for debugging)
- * ]]);
- *
- * // add class paths to autoload
- * autoloader([
- * 'lib', // really '/var/www/myproject/lib' when using basepath in config settings
- * 'models/data' // really '/var/www/myproject/models/data' when using basepath in config settings
- * ]);
- *
- * // get array registered class paths (when in debug mode any autoloaded classes will show up as 'loaded')
- * $registered_class_paths = autoloader();
- *
- * // or configuration settings AND class paths to autoload, example:
- * autoloader([
- * // set configuration settings
- * [
- * 'debug' => true,
- * 'basepath' => '/var/www/myproject'
- * ],
- * // set class paths to autoload
- * 'lib',
- * 'models/data'
- * ]);
- */
- function Autoloader($class_paths = NULL, $use_base_dir = true){
- static $is_init = false;
- static $conf = [
- 'basepath' => '',
- 'debug' => false,
- 'extensions' => ['.php'], // multiple extensions ex: ['.php', '.class.php']
- 'namespace' => '',
- 'verbose' => false
- ];
- static $paths = [];
- // autoloader(); returns paths (for debugging)
- if(\is_null($class_paths)){
- return $paths;
- }
- if(\is_array($class_paths) && isset($class_paths[0]) && \is_array($class_paths[0])) // conf settings
- {
- foreach($class_paths[0] as $k => $v)
- {
- if(isset($conf[$k]) || \array_key_exists($k, $conf))
- {
- $conf[$k] = $v; // set conf setting
- }
- }
- unset($class_paths[0]); // rm conf from class paths
- }
- // init autoloader
- if(!$is_init){
- \spl_autoload_extensions(implode(',', $conf['extensions']));
- \spl_autoload_register(NULL, false); // flush existing autoloads
- $is_init = true;
- }
- if($conf['debug']){
- $paths['conf'] = $conf; // add conf for debugging
- }
- // autoload class
- if(!\is_array($class_paths)){
- // class with namespaces, ex: 'MyPack\MyClass' => 'MyPack/MyClass' (directories)
- $class_path = \str_replace('\\', \DIRECTORY_SEPARATOR, $class_paths);
- foreach($paths as $path){
- // do not allow cached 'loaded' paths
- if(!\is_array($path)){
- foreach($conf['extensions'] as &$ext){
- $ext = \trim($ext);
- if(\file_exists($path . $class_path . $ext)){
- if($conf['debug']){
- if(!isset($paths['loaded']))
- {
- $paths['loaded'] = [];
- }
- $paths['loaded'][] = $path . $class_path . $ext;
- }
- require $path . $class_path . $ext;
- if($conf['verbose']){
- echo '<div>' . __METHOD__ . ': autoloaded class "' . $path
- . $class_path . $ext . '"</div>';
- }
- return true;
- }
- }
- if($conf['verbose']){
- echo '<div>' . __METHOD__ . ': failed to autoload class "' . $path
- . $class_path . $ext . '"</div>';
- }
- }
- }
- return false; // failed to autoload class
- } else {
- // register class path
- $is_unregistered = true;
- if(count($class_paths) > 0){
- foreach($class_paths as $path){
- $tmp_path = ( $use_base_dir ? \rtrim($conf['basepath'], \DIRECTORY_SEPARATOR)
- . \DIRECTORY_SEPARATOR : '' ) . \trim(\rtrim($path, \DIRECTORY_SEPARATOR))
- . \DIRECTORY_SEPARATOR;
- if(!\in_array($tmp_path, $paths)){
- $paths[] = $tmp_path;
- if($conf['verbose']){
- echo '<div>' . __METHOD__ . ': registered path "' . $tmp_path . '"</div>';
- }
- }
- }
- if(\spl_autoload_register(( strlen($conf['namespace']) > 0 // add namespace
- ? rtrim($conf['namespace'], '\\') . '\\' : '' ) . 'autoloader', (bool)$conf['debug'])){
- if($conf['verbose']){
- echo '<div>' . __METHOD__ . ': autoload registered</div>';
- }
- $is_unregistered = false; // flag unable to register
- } else if($conf['verbose']){
- echo '<div>' . __METHOD__ . ': autoload register failed</div>';
- }
- }
- return !$conf['debug'] ? !$is_unregistered : $paths;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement