$directory) { if (starts_with($class, $namespace)) { return $directory; } } } /** * Attempt to resolve a class using the PSR-0 standard. * * @param string $class * @param string $directory * @return void */ protected static function load_psr($class, $directory = null) { // The PSR-0 standard indicates that class namespace slashes or // underscores should be used to indicate the directory tree in // which the class resides, so we'll convert the namespace // slashes to directory slashes. $file = str_replace(array('\\', '_'), '/', $class); if (is_null($directory)) { $directories = static::$psr; } else { $directories = array($directory); } // Once we have formatted the class name, we will simply spin // through the registered PSR-0 directories and attempt to // locate and load the class into the script. // // We will check for both lowercase and CamelCase files as // Laravel uses a lowercase version of PSR-0, while true // PSR-0 uses CamelCase for all file names. $lower = strtolower($file); foreach ($directories as $directory) { if (file_exists($path = $directory.$lower.EXT)) { return require $path; } elseif (file_exists($path = $directory.$file.EXT)) { return require $path; } } } /** * Register an array of class to path mappings. * * * // Register a class mapping with the Autoloader * Autoloader::map(array('User' => path('app').'models/user.php')); * * * @param array $mappings * @return void */ public static function map($mappings) { static::$mappings = array_merge(static::$mappings, $mappings); } /** * Register a class alias with the auto-loader. * * @param string $class * @param string $alias * @return void */ public static function alias($class, $alias) { static::$aliases[$alias] = $class; } /** * Register directories to be searched as a PSR-0 library. * * @param string|array $directory * @return void */ public static function psr($directory) { $directories = static::format($directory); static::$psr = array_unique(array_merge(static::$psr, $directories)); } /** * Map namespaces to directories. * * @param array $mappings * @return void */ public static function namespaces($mappings) { $directories = static::format(array_values($mappings)); $mappings = array_combine(array_keys($mappings), $directories); static::$namespaces = array_merge(static::$namespaces, $mappings); } /** * Format an array of directories with the proper trailing slashes. * * @param array $directories * @return array */ protected static function format($directories) { return array_map(function($directory) { return rtrim($directory, DS).DS; }, (array) $directories); } }