572f98638b81c9ccdab16175ac39f76e0c373d32
[atutor.git] / mods / phpdoc / PHPDoc / renderer / html / PhpdocHTMLClassRenderer.php
1 <?php\r
2 /**\r
3 * Renders classes.\r
4 *\r
5 * @version      $Id: PhpdocHTMLClassRenderer.php,v 1.4 2000/12/03 22:37:37 uw Exp $\r
6 */\r
7 class PhpdocHTMLClassRenderer extends PhpdocHTMLDocumentRenderer {\r
8 \r
9         /**\r
10         * Internal array of "&nbsp;" strings to format HTML output.\r
11         *\r
12         * @var  array   $indent\r
13         */\r
14         var $indent = array();\r
15 \r
16         /**\r
17         * Array of variables found in the xml document.\r
18         *\r
19         * @var  array   $variables\r
20         */\r
21         var $variables = array();\r
22 \r
23         /**\r
24         * Sets the xml and template root directory.\r
25         * \r
26         * @param        string  XML file path\r
27         * @param        string  Template file path\r
28         * @param        string  Name of the current application\r
29         * @param        string  Filename extension\r
30         * @see  setPath(), setTemplateRoot()\r
31         */\r
32         function PhpdocHTMLClassRenderer($path, $templateRoot, $application, $extension = ".html") {\r
33 \r
34                 $this->setPath($path);\r
35                 $this->setTemplateRoot($templateRoot);\r
36                 $this->application = $application;\r
37                 $this->file_extension = $extension;\r
38                 \r
39                 $this->accessor = new PhpdocClassAccessor;\r
40                 $this->tpl = new IntegratedTemplate($this->templateRoot);\r
41                 $this->fileHandler = new PhpdocFileHandler;\r
42 \r
43         } // end constructor\r
44 \r
45         /**\r
46         * Renders a class.\r
47         *\r
48         * @param        string  XML source file\r
49         * @param        string  Name of the HTML target file.\r
50         * @access       public\r
51         */      \r
52         function renderClass($xmlfile, $htmlfile = "") {\r
53 \r
54                 $this->tpl->loadTemplatefile("class.html");     \r
55                 if ("" == $htmlfile)\r
56                         $htmlfile = substr($xmlfile, 6, -4) . $this->file_extension;\r
57 \r
58                 $this->accessor->loadXMLFile($this->path.$xmlfile);\r
59                 \r
60                 $this->renderSubclasses();\r
61                 $this->renderInherited();\r
62                 $this->renderFunctions();\r
63                 $this->renderVariables();\r
64                 $this->renderUses();\r
65                 $this->renderConstants();\r
66                 \r
67                 $class = $this->accessor->getClassdata();\r
68                 $tplvars = array();\r
69 \r
70                 $tplvars["CLASS_FILE"]          = $class["file"]["value"];\r
71                 $tplvars["CLASS_NAME"]          = $class["name"];\r
72                 $tplvars["CLASS_ACCESS"]        = $class["access"];\r
73                 $tplvars["CLASS_PACKAGE"]       = $class["package"];\r
74 \r
75                 if ("" != $class["extends"])\r
76                         $tplvars["CLASS_EXTENDS"] = sprintf('extends <a href="%s">%s</a>', \r
77                                                                                                                                                                                 $class["extends"].$this->file_extension, \r
78                                                                                                                                                                                 $class["extends"]\r
79                                                                                                                                                                         );\r
80                         \r
81                 $tplvars["CLASS_UNDOC"]         = ("true" == $class["undoc"]) ? $this->undocumented : "";\r
82                 \r
83                 $tplvars["CLASS_ABSTRACT"] = ("true" == $class["abstract"]) ? "abstract" : "";\r
84                 $tplvars["CLASS_STATIC"]         = ("true" == $class["static"]) ? "static" : "";\r
85                 $tplvars["CLASS_FINAL"]          = ("true" == $class["final"]) ? "final" : "";\r
86                 \r
87                 $tplvars["CLASS_TREE"]          = $this->getClasstree($class["name"]);\r
88                 \r
89                 if (isset($class["doc"]["link"]))\r
90                         $this->renderLinks($class["doc"]["link"], "class_");\r
91                         \r
92                 if (isset($class["doc"]["author"]))\r
93                         $this->renderAuthors($class["doc"]["author"], "class_");\r
94                         \r
95                 if (isset($class["doc"]["see"]))\r
96                         $this->renderSee($class["doc"]["see"], "class_");\r
97                 \r
98                 $fields = array(        "version", "deprecated", "copyright",   "since", "magic");\r
99                 reset($fields);\r
100                 while (list($k, $field) = each($fields)) \r
101                         if (isset($class["doc"][$field])) {\r
102                                 $this->tpl->setCurrentBlock("class_".strtolower($field));\r
103                                 $this->tpl->setVariable(strtoupper($field), $class["doc"][$field]["value"]);\r
104                                 $this->tpl->parseCurrentBlock();\r
105                         }\r
106 \r
107                 $fields = array( "description", "shortdescription" );\r
108 \r
109                 reset($fields);\r
110                 while (list($k, $field)=each($fields)) \r
111                         if (isset($class["doc"][$field]))\r
112                                 $tplvars["CLASS_".strtoupper($field)] = $this->encode($class["doc"][$field]["value"]);\r
113 \r
114                 $this->tpl->setCurrentBlock("__global__");\r
115                 $this->tpl->setVariable($tplvars);\r
116                 $this->tpl->setVariable("APPNAME", $this->application);\r
117 \r
118                 $this->fileHandler->createFile($this->path.$htmlfile, $this->tpl->get() );\r
119                 $this->tpl->free();     \r
120 \r
121         } // end func renderClass\r
122 \r
123         \r
124 \r
125         /**\r
126         * Renders a list of inherited elements.\r
127         *\r
128         * @see  renderInheritedElements()\r
129         */\r
130         function renderInherited() {\r
131 \r
132                 $this->renderInheritedElements( $this->accessor->getInheritedFunctions(),\r
133                                                                                                                                                 "inheritedfunctions",\r
134                                                                                                                                                 "function"\r
135                                                                                                                                         );\r
136                                                                                                                                         \r
137                 $this->renderInheritedElements( $this->accessor->getInheritedVariables(),\r
138                                                                                                                                                 "inheritedvariables",\r
139                                                                                                                                                 "variable"\r
140                                                                                                                                         );\r
141 \r
142                 $this->renderInheritedElements( $this->accessor->getInheritedConstants(),\r
143                                                                                                                                                 "inheritedconstants",\r
144                                                                                                                                                 "constant"\r
145                                                                                                                                         );\r
146 \r
147                 $this->renderInheritedElements( $this->accessor->getInheritedUses(),\r
148                                                                                                                                                 "inheriteduses",\r
149                                                                                                                                                 "uses"\r
150                                                                                                                                         );\r
151 \r
152         } // end func renderInherited\r
153 \r
154         /**\r
155         * Renders a list of a certain inherited element.\r
156         *\r
157         * @param        array           List of inherited elements.\r
158         * @param        string  Templateblockname\r
159         * @param        string  Element type: function, variable...\r
160         * @see  renderInherited()\r
161         */\r
162         function renderInheritedElements($inherited, $block, $type) {\r
163                 \r
164                 if (0 == count($inherited))\r
165                         return;\r
166 \r
167                 $this->tpl->setCurrentBlock($block);\r
168 \r
169                 reset($inherited);\r
170                 while (list($source, $elements) = each($inherited)) {\r
171                         \r
172                         $value = "";\r
173                         \r
174                         reset($elements);\r
175                         while (list($k, $element) = each($elements))\r
176                                 $value .= sprintf('<a href="%s#%s_%s">%s</a>, ', \r
177                                                                                                                         $source.$this->file_extension,\r
178                                                                                                                         $type, \r
179                                                                                                                         $element, \r
180                                                                                                                         $element\r
181                                                                                                         );\r
182                         $value = substr($value, 0, -2);\r
183                         \r
184                         $this->tpl->setVariable("SOURCE", $source);\r
185                         $this->tpl->setVariable("ELEMENTS", $value);\r
186                         $this->tpl->parseCurrentBlock();\r
187                 }\r
188 \r
189         } // end func renderInheritedElements\r
190 \r
191         /**\r
192         * Renders a list of direct known subclasses.\r
193         */\r
194         function renderSubclasses() {\r
195                 \r
196                 $subclasses = $this->accessor->getSubclasses();\r
197                 if (0 == count($subclasses)) \r
198                         return;\r
199                 \r
200                 $elements = "";\r
201                 reset($subclasses);\r
202                 while (list($k, $subclass) = each($subclasses))\r
203                         $elements .= sprintf('<a href="%s">%s</a>, ', $subclass.$this->file_extension, $subclass);\r
204                 \r
205                 $elements       = substr($elements, 0, -2);\r
206                 \r
207                 if ("" != $elements) {\r
208                 \r
209                         $this->tpl->setCurrentBlock("subclasses");\r
210                         $this->tpl->setVariable("ELEMENTS", $elements);\r
211                         $this->tpl->parseCurrentBlock();\r
212                 }\r
213 \r
214         } // end func renderSubclasses\r
215 \r
216         /**\r
217         * Adds a summary and a detailed list of all variables to the template.\r
218         *\r
219         * @see  renderVariableSummary(), renderVariableDetail()\r
220         */\r
221         function renderVariables() {\r
222                 \r
223                 $this->variables["private"] = $this->accessor->getVariablesByAccess("private");\r
224                 $this->variables["public"]      = $this->accessor->getVariablesByAccess("public");\r
225                 \r
226                 if (0 == count($this->variables["private"]) && 0 == count($this->variables["public"]))\r
227                         return;\r
228                 \r
229                 $this->renderVariableSummary();\r
230                 $this->renderVariableDetail();\r
231 \r
232                 $this->variables = array();\r
233 \r
234         } // end func renderVariables\r
235 \r
236         /**\r
237         * Adds a summary of all variables to the template.\r
238         * \r
239         * The function assumes that there is a block named "variablesummary" and\r
240         * within it a block names "variablesummay_loop" in the template.\r
241         *\r
242         * @see  renderVariableDetail()\r
243         */      \r
244         function renderVariableSummary() {\r
245 \r
246                 reset($this->accessModifiers);\r
247                 while (list($k, $access) = each($this->accessModifiers)) {\r
248 \r
249                         if (0 == count($this->variables[$access]))\r
250                                 continue;\r
251                                 \r
252                         $this->tpl->setCurrentBlock("variablesummary_loop");\r
253                         \r
254                         reset($this->variables[$access]);\r
255                         while (list($name, $variable) = each($this->variables[$access])) {\r
256                                 \r
257                                 $this->tpl->setVariable("NAME", $name);\r
258                                 $this->tpl->setVariable("TYPE", $variable["type"]);\r
259                                 \r
260                                 if (isset($variable["doc"]["shortdescription"]))\r
261                                         $this->tpl->setVariable("SHORTDESCRIPTION", $this->encode($variable["doc"]["shortdescription"]["value"]));\r
262                         \r
263                                 $this->tpl->parseCurrentBlock();                                \r
264                                 \r
265                         }\r
266                         \r
267                         $this->tpl->setCurrentBlock("variablesummary");\r
268                         $this->tpl->setVariable("ACCESS", ucfirst($access));\r
269                         $this->tpl->parseCurrentBlock();\r
270                         \r
271                 }\r
272 \r
273         } // end func renderVariableSummary\r
274 \r
275         /**\r
276         * Adds a detailed list of all variables to the template.\r
277         * \r
278         * The function assumes that there is a block named "variabledetails"\r
279         * and within it a block names "variablesdetails_loop" in the template.\r
280         *\r
281         * @see  renderVariableSummary()\r
282         */      \r
283         function renderVariableDetail() {\r
284                 \r
285                 reset($this->accessModifiers);\r
286                 while (list($k, $access) = each($this->accessModifiers)) {\r
287 \r
288                         if (0 == count($this->variables[$access]))\r
289                                 continue;\r
290 \r
291                         reset($this->variables[$access]);\r
292                         while (list($name, $variable)=each($this->variables[$access])) {\r
293                         \r
294                                 $tplvars = array();\r
295                                 $tplvars["NAME"]                =       $variable["name"];\r
296                                 $tplvars["ACCESS"]      = $variable["access"];\r
297                                 $tplvars["TYPE"]                = $variable["type"];\r
298                                 $tplvars["VALUE"]               = htmlentities($variable["value"]);\r
299 \r
300                                 if ("true" == $variable["undoc"]) \r
301                                         $tplvars["UNDOC"] = $this->undocumented;\r
302 \r
303                                 if ("true" == $variable["static"])\r
304                                         $tplvars["STATIC"] = "static";\r
305 \r
306                                 if ("true" == $variable["final"])\r
307                                         $tplvars["FINAL"] = "final";\r
308 \r
309                                 if (isset($variable["doc"]["shortdescription"]))\r
310                                         $tplvars["SHORTDESCRIPTION"] = $this->encode($variable["doc"]["shortdescription"]["value"]);\r
311 \r
312                                 if (isset($variable["doc"]["description"]))\r
313                                         $tplvars["DESCRIPTION"] = $this->encode($variable["doc"]["description"]["value"]);\r
314 \r
315                                 $this->renderCommonDocfields("variabledetails_", $variable);\r
316 \r
317                                 $this->tpl->setCurrentBlock("variabledetails_loop");    \r
318                                 $this->tpl->setVariable($tplvars);\r
319                                 $this->tpl->parseCurrentBlock();        \r
320 \r
321                         }\r
322 \r
323                         $this->tpl->setCurrentBlock("variabledetails");\r
324                         $this->tpl->setVariable("ACCESS", ucfirst($access) );\r
325                         $this->tpl->parseCurrentBlock();\r
326 \r
327                 }\r
328 \r
329         } // end func renderVariableDetail\r
330 \r
331         /**\r
332         * Returns a html string that shows the class tree.\r
333         *\r
334         * @param        string  name of the current class\r
335         * @return       string  HTML that shows the tree\r
336         */\r
337         function getClasstree($class) {\r
338                 \r
339                 $path = $this->accessor->getClasstree();\r
340                 $level = 0;\r
341                 $num = count($path) - 1;\r
342                 \r
343                 for ($i = $num; $i >= 0; --$i) {\r
344 \r
345                         $indent = $this->getIndent($level);\r
346 \r
347                         if ($level > 0)\r
348                                 $value.= sprintf("%s |<br>%s+-- ", $indent, $indent);\r
349 \r
350                         $value.= sprintf('<a href="%s">%s</a><br>', \r
351                                                                                                 $path[$i].$this->file_extension,\r
352                                                                                                 $path[$i]\r
353                                                                                         );\r
354                         ++$level;\r
355                         \r
356                 }\r
357 \r
358                 $indent = $this->getIndent($level);\r
359 \r
360                 if ($level > 0)\r
361                         $value.= sprintf("%s |<br>%s+-- ", $indent, $indent);\r
362 \r
363                 $value.= sprintf('%s<br>', $class);\r
364 \r
365                 return $value;\r
366         } // end func getClasstree\r
367 \r
368         /**\r
369         * Returns a certain number of "&nbsp;"s.\r
370         *\r
371         * @param        int     number of "&nbsp;" required.\r
372         * @see          $indent\r
373         * @return       string  A string with the requested number of nunbreakable html spaces\r
374         */\r
375         function getIndent($level) {\r
376 \r
377                 if (!isset($this->indent[$level])) {\r
378 \r
379                         $html = "";\r
380                         for ($i = 0; $i < $level; ++$i)\r
381                                 $html .= "&nbsp;&nbsp;";\r
382 \r
383                         $this->indent[$level] = $html;\r
384 \r
385                 }\r
386 \r
387                 return $this->indent[$level];\r
388         } // end func getIndent\r
389 \r
390 } // end class PhpdocHTMLClassRenderer\r
391 \r
392 ?>