1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: function xu_get_class_name( $file ) {
14: if ( ! is_string( $file ) ) {
15: throw new InvalidArgumentException( 'Invalid argument. Must be string.' );
16: }
17:
18: if ( ! file_exists( $file ) ) {
19: throw new Exception( sprintf( '`%s`does not exist.', $file ) );
20: }
21:
22: $content = file_get_contents( $file );
23: $tokens = token_get_all( $content );
24: $class_name = '';
25: $namespace_name = '';
26: $i = 0;
27: $len = count( $tokens );
28:
29: for ( ; $i < $len; $i++ ) {
30: if ( $tokens[$i][0] === T_NAMESPACE ) {
31: for ( $j = $i + 1; $j < $len; $j++ ) {
32: if ( $tokens[$j][0] === T_STRING ) {
33: $namespace_name .= '\\' . $tokens[$j][1];
34: } else if ( $tokens[$j] === '{' || $tokens[$j] === ';' ) {
35: break;
36: }
37: }
38: }
39:
40: if ( $tokens[$i][0] === T_CLASS ) {
41: for ( $j = $i + 1; $j < $len; $j++ ) {
42: if ( $tokens[$j] === '{' ) {
43: $class_name = $tokens[$i + 2][1];
44: }
45: }
46: }
47: }
48:
49: if ( empty( $class_name ) ) {
50: return '';
51: }
52:
53: if ( empty( $namespace_name ) ) {
54: return $class_name;
55: }
56:
57: return $namespace_name . '\\' . $class_name;
58: }
59: