PhpStorm autoload live template

Yet another autoload live template for PHP 5.2 code.

The PhpStorm live template

Simply copy and paste the code below in PhpStorm live template editor:

/**
 * Takes care of the attempted autoloading of a class.
 *
 * Presumes classes are under the `/src` dir relative to the dir this file lives
 * in and that classes are properly named.
 * Given `$prefix` as `some_plugin`:
 *
 *      some_plugin_SomeClass
 *
 * will map to:
 *
 *      /src/SomeClass.php
 *
 *  Modify the `$sep` variable to map class names to sub-folders.
 *  Given `$sep` as `_`:
 *
 *      some_Plugin_PseudoNamespace_SomeClass
 *
 * will map to
 *
 *      /src/PseudoNamespace/SomeClass.php
 *
 * Class names will be long no matter what.
 *
 * @param string $class The class name
 */
function $prefix$_autoload($class)
{
    $prefix = '$prefix$_';
    $sep = '_';

    if (!strpos($class, $prefix) === 0) {
        return;
    }
    $class_name = str_replace($sep, DIRECTORY_SEPARATOR, str_replace($prefix, '', $class));
    if (!(file_exists($class_file = dirname(__FILE__) . '/src/' . $class_name . '.php'))) {
        return;
    }
    require $class_file;
}

spl_autoload_register('$prefix$_autoload');

and save under a meaningful abbreviation (like ‘autoloadspl’). Stop manually requiring.