11<?php
22/**
33 * Yaf Classes signature generator
4- *
4+ *
55 * @author Laruence
66 * @date 2012-07-21 13:46
7- * @version $Id$
8- */
7+ * @version $Id$
8+ */
99
10+ $ useNamespace = (bool ) ini_get ("yaf.use_namespace " );
11+ $ yafClassPrefix = sprintf ("Yaf%s " , $ useNamespace ? "\\" : "_ " );
1012$ classes = get_declared_classes ();
1113foreach ($ classes as $ key => $ value ) {
12- if (strncasecmp ($ value , " Yaf_ " , 3 )) {
14+ if (strncasecmp ($ value , $ yafClassPrefix , 4 )) {
1315 unset($ classes [$ key ]);
1416 }
1517}
1618
1719echo "<?php \n" ;
1820
1921foreach ($ classes as $ class_name ) {
20- $ class = new ReflectionClass ($ class_name );
21- $ indent = "" ;
22-
23- if ($ class ->isFinal ()) {
24- echo "final " ;
25- }
26-
27- if ($ class ->isAbstract ()) {
28- echo "abstract " ;
29- }
30-
31- if ($ class ->isInterface ()) {
32- echo "Interface " , $ class_name ;
33- } else {
34- echo "class " , $ class_name ;
35- }
36-
37- /* parent */
38- $ parent = $ class ->getParentClass ();
39- if ($ parent ) {
40- echo " extends " , $ parent ->getName ();
41- }
42-
43- /* interface */
44- $ interfaces = $ class ->getInterfaceNames ();
45- if (count ($ interfaces )) {
46- echo " implements " , join (", " , $ interfaces );
47- }
48- echo " { \n" ;
49-
50- $ indent .= "\t" ;
51- /* constants */
52- echo $ indent , "/* constants */ \n" ;
53- $ constatnts = $ class ->getConstants ();
54- foreach ($ constatnts as $ k => $ v ) {
55- echo $ indent , "const " , $ k , " = " , $ v , "; \n" ;
56- }
57- echo "\n" ;
58-
59- /* properties */
60- echo $ indent , "/* properties */ \n" ;
61- $ properties = $ class ->getProperties ();
62- $ values = $ class ->getDefaultProperties ();
63- foreach ($ properties as $ p ) {
64- echo $ indent ;
65- if ($ p ->isStatic ()) {
66- echo "static " ;
67- }
68-
69- if ($ p ->isPublic ()) {
70- echo "public " ;
71- } else if ($ p ->isProtected ()) {
72- echo "protected " ;
73- } else {
74- echo "private " ;
75- }
76-
77- echo '$ ' , $ p ->getName (), " = " ;
78-
79- if (isset ($ values [$ p ->getName ()])) {
80- echo '" ' , $ values [$ p ->getName ()], '" ' ;
81- } else {
82- echo "NULL " ;
83- }
84- echo "; \n" ;
85- }
86- echo "\n" ;
87-
88- /* methods */
89- echo $ indent , "/* methods */ \n" ;
90- $ methods = $ class ->getMethods ();
91- foreach ($ methods as $ m ) {
92- echo $ indent ;
93- echo implode (' ' , Reflection::getModifierNames ($ m ->getModifiers ()));
94- echo " function " , $ m ->getName (), "( " ;
95- $ parameters = $ m ->getParameters ();
96- $ number = count ($ parameters );
97- $ index = 0 ;
98- foreach ($ parameters as $ a ) {
99- if (($ type = $ a ->getClass ())) {
100- echo $ type ->getName (), " " ;
101- } else if ($ a ->isArray ()) {
102- echo "array " ;
103- }
104-
105- if ($ a ->isPassedByReference ()) {
106- echo "& " ;
107- }
108-
109- $ name = $ a ->getName ();
110- if ($ name == "... " ) {
111- echo '$_ = "..." ' ;
112- } else {
113- echo "$ " , $ name ;
114- }
115-
116- if ($ a ->isOptional ()) {
117- if ($ a ->isDefaultValueAvailable ()) {
118- echo " = " , $ a ->getDefaultValue ();
119- } else {
120- echo " = NULL " ;
121- }
122- }
123-
124- if (++$ index < $ number ) {
125- echo ", " ;
126- }
127- }
128-
129- echo ") { \n" ;
130- echo $ indent , "} \n" ;
131- }
132- echo "} \n\n" ;
133- }
22+ $ class = new ReflectionClass ($ class_name );
23+ $ indent = "" ;
24+
25+ if ($ useNamespace && false !== ($ backslash = strrpos ($ class_name , "\\" ))) {
26+ $ namespaceName = substr ($ class_name , 0 , $ backslash );
27+ $ class_name = substr ($ class_name , $ backslash + 1 );
28+ echo "namespace " , $ namespaceName , " { \n" ;
29+
30+ $ indent = "\t" ;
31+ }
32+
33+ $ classAttributes = "" ;
34+
35+ if ($ class ->isInterface ()) {
36+ $ classAttributes .= "interface " ;
37+ } else {
38+ if ($ class ->isFinal ()) {
39+ $ classAttributes .= "final " ;
40+ }
41+
42+ if ($ class ->isAbstract ()) {
43+ $ classAttributes .= "abstract " ;
44+ }
45+
46+ $ classAttributes .= "class " ;
47+ }
48+
49+ echo $ indent , $ classAttributes , $ class_name ;
50+
51+ /* parent */
52+ $ parent = $ class ->getParentClass ();
53+ if ($ parent ) {
54+ echo " extends " , $ parent ->getName ();
55+ }
56+
57+ /* interface */
58+ $ interfaces = $ class ->getInterfaceNames ();
59+ if (count ($ interfaces )) {
60+ echo " implements " , join (", " , $ interfaces );
61+ }
62+ echo " { \n" ;
63+
64+ $ indent .= "\t" ;
65+ /* constants */
66+ $ constants = $ class ->getConstants ();
67+ if (0 < count ($ constants )) {
68+ echo $ indent , "/* constants */ \n" ;
69+
70+ foreach ($ constants as $ k => $ v ) {
71+ echo $ indent , "const " , $ k , " = " , $ v , "; \n" ;
72+ }
73+ echo "\n" ;
74+ }
75+
76+ /* properties */
77+ $ properties = $ class ->getProperties ();
78+ if (0 < count ($ properties )) {
79+ echo $ indent , "/* properties */ \n" ;
80+ $ values = $ class ->getDefaultProperties ();
81+ foreach ($ properties as $ p ) {
82+ echo $ indent ;
83+
84+ if ($ p ->isStatic ()) {
85+ echo "static " ;
86+ }
87+
88+ if ($ p ->isPublic ()) {
89+ echo "public " ;
90+ } else if ($ p ->isProtected ()) {
91+ echo "protected " ;
92+ } else {
93+ echo "private " ;
94+ }
95+
96+ echo '$ ' , $ p ->getName (), " = " ;
97+
98+ if (isset ($ values [$ p ->getName ()])) {
99+ echo '" ' , $ values [$ p ->getName ()], '" ' ;
100+ } else {
101+ echo "NULL " ;
102+ }
103+ echo "; \n" ;
104+ }
105+ echo "\n" ;
106+ }
107+
108+ /* methods */
109+ $ methods = $ class ->getMethods ();
110+ if (0 < count ($ methods )) {
111+ echo $ indent , "/* methods */ \n" ;
112+
113+ foreach ($ methods as $ m ) {
114+ echo $ indent ;
115+ echo implode (' ' , Reflection::getModifierNames ($ m ->getModifiers ()));
116+ echo " function " , $ m ->getName (), "( " ;
117+
118+ if ($ m ->isAbstract ()) {
119+ // abstract methods are without a body "{ ... }"
120+ echo "); \n" ;
121+ continue ;
122+ }
123+
124+ $ parameters = $ m ->getParameters ();
125+ $ number = count ($ parameters );
126+ $ index = 0 ;
127+ foreach ($ parameters as $ a ) {
128+ if (($ type = $ a ->getClass ())) {
129+ echo $ type ->getName (), " " ;
130+ } else if ($ a ->isArray ()) {
131+ echo "array " ;
132+ }
133+
134+ if ($ a ->isPassedByReference ()) {
135+ echo "& " ;
136+ }
137+
138+ $ name = $ a ->getName ();
139+ if ($ name == "... " ) {
140+ echo '$_ = "..." ' ;
141+ } else {
142+ echo "$ " , $ name ;
143+ }
144+
145+ if ($ a ->isOptional ()) {
146+ if ($ a ->isDefaultValueAvailable ()) {
147+ echo " = " , $ a ->getDefaultValue ();
148+ } else {
149+ echo " = NULL " ;
150+ }
151+ }
152+
153+ if (++$ index < $ number ) {
154+ echo ", " ;
155+ }
156+ }
157+
158+ echo ") { \n" ;
159+ echo $ indent , "} \n" ;
160+ }
161+ }
162+
163+ $ indent = substr ($ indent , 0 , -1 );
164+ echo $ indent , "} \n" ;
165+
166+ if ($ useNamespace && false !== $ backslash ) {
167+ echo "} \n\n" ;
168+ }
169+ }
0 commit comments