Created
December 14, 2012 19:54
-
-
Save mertenvg/4288127 to your computer and use it in GitHub Desktop.
Extract a fully qualfied (namespaced) classname from a php file. This function assumes PSR-0 compliance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Extract a fully qualfied (namespaced) classname from a php file. | |
* This function assumes PSR-0 compliance. | |
* | |
* @param string $filePath Path to the php file. | |
* @return string|false The resulting classname | |
*/ | |
function extractFullClassNameFromFile($filePath) | |
{ | |
if (!file_exists($filePath)) { | |
return false; | |
} | |
$namespace = null; | |
$classname = null; | |
$cnMatches = null; | |
$nsMatches = null; | |
$file = file_get_contents($filePath); | |
if (preg_match_all('/\n\s*(abstract\s|final\s)*class\s+(?<name>[^\s;]+)\s*/i', $file, $cnMatches, PREG_PATTERN_ORDER)) { | |
$classname = array_pop($cnMatches['name']); | |
if (preg_match_all('/namespace\s+(?<name>[^\s;]+)\s*;/i', $file, $nsMatches, PREG_PATTERN_ORDER)) { | |
$namespace = array_pop($nsMatches['name']); | |
} | |
} | |
if (empty($classname)) { | |
return false; | |
} | |
return "$namespace\\$classname"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment