/** * <p> * Derives the class name based on the <tt>$filename</tt> passed. This * assumes the following: there is only one class per file, and that the * class name follows Java's dot-path convention of naming files, namespace * and all, according to their physical path with respect to the project's * class path. * </p> * <p> * The class name returned can be returned as a fully qualified class name * if the <tt>$fullyQualified</tt> flag is set to <tt>true</tt> and a * <tt>$baseDir</tt> given. If both of these conditions are met, this * method will be able to reliably differentiate between a fully qualified * PEAR class name, and one using the PHP native namespace separator. * </p> * * 1. If an underscore exists as part of the filename, e.g. Foo_Bar.php * do not bother with PEAR * 2. If fully qualified is TRUE, we will always add namespace information. * 3. * * @param string $filename the name of the file * @param boolean $baseDir * @param string $extension */ public static final function getClassFromFileName($filename, $baseDir = null, $extension = '.php') { $pos = 0; $className = null; if (!is_null($baseDir)) { $pos = strpos($filename, $baseDir); $filename = substr($filename, $pos + strlen($baseDir)); } $_filename = str_replace('\\', DIRECTORY_SEPARATOR, $filename); $_filename = str_replace('/', DIRECTORY_SEPARATOR, $_filename); $extensionPattern = ClassUtils::escapePattern($extension); $pattern = '/^(?P<className>[a-zA-Z0-9\\/\\\\_]+)([\\.]?' . $extensionPattern . '){1}$/'; if (preg_match($pattern, $_filename, $matches)) { $className = ltrim($matches['className'], DIRECTORY_SEPARATOR); } $className = str_replace(DIRECTORY_SEPARATOR, self::PHP_NAMESPACE_SEPARATOR, $className); return $className; }