C:\xampp2_not used\php\pear\PHP\UML\Output\Php\DocElement.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
 * PHP_UML
 *
 * PHP version 5
 *
 * @category PHP
 * @package  PHP_UML
 * @author   Baptiste Autin <ohlesbeauxjours@yahoo.fr> 
 * @license  http://www.gnu.org/licenses/lgpl.html LGPL License 3
 * @version  SVN: $Revision$
 * @link     http://pear.php.net/package/PHP_UML
 * @since    $Date$
 */

/**
 * General class for an renderer in the PHP implementation
 *
 * @category   PHP
 * @package    PHP_UML
 * @subpackage Output
 * @subpackage Php
 * @author     Baptiste Autin <ohlesbeauxjours@yahoo.fr> 
 * @license    http://www.gnu.org/licenses/lgpl.html LGPL License 3
 */
abstract class PHP_UML_Output_Php_DocElement extends PHP_UML_Output_ApiRenderer
{
    const 
FILE_EXT          'php';
    const 
TEMPLATES_DIRNAME 'templates';

    
/**
     * Constructor
     *
     * @param PHP_UML_Output_ExporterAPI $exporter Reference to an exporter
     */
    
public function __construct(PHP_UML_Output_ExporterAPI $exporter)
    {
        
parent::__construct($exporter);
        
$this->mainTpl $this->getTemplate('main.php');
    }
    
    protected function 
getDescription(PHP_UML_Metamodel_Stereotype $s$annotatedElement='')
    {
        
$tag PHP_UML_Metamodel_Helper::getStereotypeTag($s'description');
        if (!
is_null($tag))
            return 
$tag->value;
        else
            return 
'';
    }

    
/**
     * Renders the operation's parameters, as a comma-sep list, between brackets
     * 
     * @param PHP_UML_Metamodel_Operation $operation The operation
     * @param bool                        $withType  If true, adds an hyperlink
     * 
     * @return string
     */
    
protected function getParameterList(PHP_UML_Metamodel_Operation $operation$withType false)
    {
        
$str '(';
        
$n   count($operation->ownedParameter);
        for (
$i=0$i<$n$i++) {
            
$parameter $operation->ownedParameter[$i];
            if (
substr($parameter->direction02)=='in') {
                if (
$withType && isset($parameter->type) && !($parameter->type instanceof PHP_UML_Metamodel_Datatype)) {
                    if (
is_object($parameter->type))
                        
$str .= $this->getLinkTo($parameter->type).' ';
                    else if (
strcasecmp($parameter->type'array')==0)
                        
$str .= $this->displayUnresolved($parameter->type).' ';
                }
                if (
$parameter->direction=='inout') {
                    
$str .= '&';
                }
                if (
$parameter->name[0] != '$')
                    
$str .= '$';
                
$str .= $parameter->name;
                
$str .= $this->getDefaultValue($parameter);
                if (
$i<($n-1))
                    
$str .= ', ';
            }
        }
        
$str .= ')';
        return 
$str;
    }

    protected function 
getDefaultValue(PHP_UML_Metamodel_TypedElement $obj)
    {
        if (
$obj->default!='')
            return 
'='.$obj->default;
        else
            return 
'';
    }

    
/**
     * Renders a link towards a given element
     * (since datatypes don't own to a "package",  we suppose they are located in
     * the top package)
     * 
     * @param PHP_UML_Metamodel_Classifier $t        The element
     * @param string                       $cssStyle CSS style to use
     * 
     * @return string
     */
    
protected function getLinkTo(PHP_UML_Metamodel_Classifier $t$hideDatatype=true)
    {
        if (
$hideDatatype && ($t instanceof PHP_UML_Metamodel_Datatype))
            return 
'';
        
        
$ns $t instanceof PHP_UML_Metamodel_Datatype '' self::T_NAMESPACE;
        if (isset(
$t->package)) {
            
$ns .= $this->getAbsPath($t->packageself::T_NAMESPACE);
        }
        return 
$ns.$t->name;
    }

    
/**
     * Renders an unresolved type as an HTML span
     * 
     * @param string $type Type, provided as a string
     * 
     * @return string
     */
    
protected function displayUnresolved($type)
    {
        return 
$type;
    }
    

    protected function 
getTagsAsList(PHP_UML_Metamodel_Stereotype $s)
    {
        return 
$this->getDocblocks($s0);
    }
    
    
/**
     * Renders the properties of a given stereotype.
     * Docblocks in $ignoredTag are not shown.
     * 
     * @param PHP_UML_Metamodel_Stereotype $s        A stereotype
     * @param int                          $nbSpacer Number of spacers to add
     * 
     * @return string
     */
    
protected function getDocblocks(PHP_UML_Metamodel_Stereotype $s$nbSpacer 0)
    {
        
$str    '';
        
$spacer str_repeat(chr(9), $nbSpacer);
        foreach (
$s->ownedAttribute as $tag) {
            if (!(
in_array($tag->name$this->ignoredTag))) {
                
$str .= $spacer;
                if (
$tag->name!='description') {
                    
$str .= ' * @'.$tag->name.' ';
                } else {
                    
$str .= ' * ';
                }
                if (
strlen($tag->value)>0)
                    
$str .= str_replace($this->getNl(), $this->getNl().$spacer.' * '$tag->value);
                if (
$tag->name=='description') {
                    
$str .= $this->getNl().$spacer.' *';
                }
                
$str .= $this->getNl();
            }
        }
        if (
$str != '') {
            
$str $spacer.'/**'.$this->getNl().$str.$spacer.' */'.$this->getNl();
        }
        return 
$str;
    }


    
/**
     * Renders the block "Properties" of a package or a class as HTML
     * 
     * @param PHP_UML_Metamodel_NamedElement $p A classifier/a package
     * 
     * @return string
     */
    
protected function getPropertyBlock(PHP_UML_Metamodel_NamedElement $p)
    {
        if (empty(
$p->ownedAttribute))
            return 
'';

        
$str    '';
        
$spacer chr(9);
        foreach (
$p->ownedAttribute as $o) {
                           
            if (!
is_null($o->description) && $this->exporter->getDocblocks()) {
                
// we add var/return docblocks if they are missing
                
$this->addVarDocblock($o);
                
$str .= $this->getDocblocks($o->description1);
            }
            
            
$str .= $spacer;
            if (
$o->isReadOnly)
                
$str .= 'const ';
            else {
                
$str .= $o->visibility.' ';
                if (!
$o->isInstantiable)
                    
$str .= 'static ';
            }
               
            
// type display;
            /*if (is_object($o->type))
                $str .= $this->getLinkTo($o->type).' ';
            else
                $str .= $this->displayUnresolved($o->type);*/
            
if ((!empty($o->name)) && ($o->name[0]!='$' && !$o->isReadOnly))
                
$str .= '$';
            
            
$str .= $o->name.''.$this->getDefaultValue($o).';';

            
$str .= $this->getNl().$this->getNl();
        }
        
$str .= '';
        return 
$str;
    }
    
    private function 
addVarDocblock(PHP_UML_Metamodel_Property $o)
    {
        
$found false;
        foreach (
$o->description->ownedAttribute as $tag) {
            if (
$tag->name=='var') {
                
$found true;
                break;
            }
        }
        if (!
$found) {
            
$st       = new PHP_UML_Metamodel_Stereotype();
            
$st->name 'var';
            if (
is_object($o->type))
                
$st->value $this->getLinkTo($o->typefalse);
            else
                
$st->value $this->displayUnresolved($o->type);
            
$o->description->ownedAttribute[] = $st;
        }
    }
    
    private function 
addReturnDocblock(PHP_UML_Metamodel_Operation $o)
    {
        
$found false;
        foreach (
$o->description->ownedAttribute as $tag) {
            if (
$tag->name=='return') {
                
$found true;
                break;
            }
        }
        if (!
$found) {
            
$st       = new PHP_UML_Metamodel_Stereotype();
            
$st->name 'return';
            foreach (
$o->ownedParameter as $parameter) {
                if (
$parameter->direction != 'in') {
                    if (
is_object($parameter->type))
                        
$st->value .= $this->getLinkTo($parameter->typefalse).' ';
                    else
                        
$st->value .= $this->displayUnresolved($parameter->type);
                }
            }
            
$o->description->ownedAttribute[] = $st;
        }
    }

    
    
/**
     * Renders the block "Function" of a package or a classifier as HTML
     * 
     * @param PHP_UML_Metamodel_NamedElement $p A classifier or a package
     * 
     * @return string
     */
    
protected function getFunctionBlock(PHP_UML_Metamodel_NamedElement $p)
    {
        if (empty(
$p->ownedOperation))
            return
'';

        
$str    '';
        
$spacer chr(9);
        
        foreach (
$p->ownedOperation as $o) {

            if (!
is_null($o->description) && $this->exporter->getDocblocks()) {
                
$this->addReturnDocblock($o);
                
$str .= $this->getDocblocks($o->description1);
            }
            
            
$str .= $spacer.($o->visibility).' ';
            if (!
$o->isInstantiable)
                
$str .= 'static ';
            if (
$o->isAbstract)
                
$str .= 'abstract ';
            
            
$str .= 'function '.$o->name;
            
            
/*type hint
            $return = $this->getReturnParam($o);
            if (is_object($return->type))
                $str .= $this->getLinkTo($return->type).' ';
            else
                $str .= $this->displayUnresolved($return->type);*/
            
$str .= $this->getParameterList($otrue);

            if (
$o->isAbstract || $p instanceof PHP_UML_Metamodel_Interface)
                
$str .= ';'.$this->getNl().$this->getNl();
            else
                
$str .= $this->getNl().$spacer.'{'.$this->getNl().
                    
$spacer.'}'.$this->getNl().$this->getNl();
        }
        
$str .= '';
        return 
$str;
    }
    
    
/**
     * Returns the HTML code for the "File" information tag
     * 
     * @param PHP_UML_Metamodel_NamedElement $p An element
     * 
     * @return string
     */
    
protected function getFileInfo(PHP_UML_Metamodel_NamedElement $p)
    {
        if (!empty(
$p->file->package))
            return 
''.$this->getAbsPath($p->file->package).$p->file->name.'';
        else
            return 
'';
    }
    
    protected function 
getNl()
    {
        return 
PHP_EOL;
    }
    
    
/**
     * Replace the template's placeholders with their value
     *
     * @param string $main   Main HTML content (generated by PHP_UML)
     * @param string $header Navigation HTML content (navig bar)
     * @param string $ns     Title content
     * @param string $name   Element name
     *
     * @return string
     */
    
protected function replaceInTpl($main$header$ns$name)
    {
        
$str str_replace('#HEADER'$header$this->mainTpl);
        
$str str_replace('#NAMESPACE'$ns$str);
        
$str str_replace('#DETAIL'$main$str);
        
$str str_replace('#NAME'$this->getTypeName().' '.$name$str);
        return 
$str;
    }
    
    protected function 
getTemplateDirectory()
    {
        return 
dirname(__FILE__).DIRECTORY_SEPARATOR.self::TEMPLATES_DIRNAME;
    }
    
    protected function 
save($elementName$str)
    {
        
$fic $this->getContextPackage()->dir.$elementName.'.'.self::FILE_EXT;
        
file_put_contents($fic$str);
    }
}
?>
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586