1
\$\begingroup\$

I'm open to any advice to help improve this code for general use.

/**
*
* This code was created by Joshua Getner.
* @license http://opensource.org/licenses/bsd-license.php BSD
* 
*
*/

namespace Blacksmith;

class Autoloader
{

    // an array that maps classnames to there filepaths
    protected static $classes = array();

   /**
    * register your classnames and class paths to the autoloader.
    * keeps the loading quick and accurate.
    * @param string $class
    * @param string $path
    * @return null
    */
    public static function setClassPath($className, $path)
    {
        static::$classes[$className] = str_replace('\\' , '/' , $path);
    }

   /**
    * register an array of classname with paths to the file
    * keeps the loading quick and accurate.
    * @param array $classes
    * @return null
    */
    public static function setClassPaths(array $classes)
    {
        static::$classes = array_merge($classes, static::$classes);
    }

   /**
    * return the path to the classname registered.
    * @param string $class
    * @return string
    */
    public static function getClassPath($className)
    {
        return((isset(static::$classes[$className]) ? 
            static::$classes[$className] : false));
    }

   /**
    * return the array of all the classes registered.
    * @return array
    */
    public static function getClassPaths()
    {
        return static::$classes;
    }

   /**
    * return a formated file path according to the PSR standard
    * @param string $class
    * @return string
    */
    public static function formatPSRClass($class)
    {
        return  str_replace(array('\\', '_'), '/', strtolower($class)) . '.php';
    }   

   /**
    * attempt to load the registered class or interface.
    * @param string $class
    * @return bool
    */
    public static function loadClass($class)
    {
        $path = "";

        // check if the class has been mapped
        if(static::getClassPath($class) !== false)
        {
            // set the path as the mapped path
            $path = static::getClassPath($class);
        }

        // so no class mapped, lets try the PSR way
        else
        {
            // get the path from the class name according to PSR-0
            $path = str_replace('\\', '/', dirname(__FILE__)) . '/' . static::formatPSRClass($class);
        }

        // lets attempt to load the file and check if the class or interface exists 
        if(file_exists($path))
        {
            @require $path;

            if(class_exists($class, false) || interface_exists($class , false))
            {
                return true;
            }
        }

        // throw an exception no class was loaded.
        else
        {
            throw new \Exception(sprintf('unable to load the requested class: %s at path %s', $class, $path));
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$
  1. I don't see a reason to use static methods here, consider using plain 'ol methods.
  2. Why throw an exception? PHP will issue a fatal error when a class is used but not defined.
  3. Since you are not using spl_autoload_register(), I'm not sure yours will play nicely with other autoloaders
  4. Why not simply use the PSR-0 sample autoloader?
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.