/
DocCommand.php
590 lines (542 loc) · 20.5 KB
/
DocCommand.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
/**
* DocCommand class file.
*
* @author Marc Busqué <marc@lamarciana.com>, heavily based on previous work by Qiang Xue <qiang.xue@gmail.com>
* @link http://github.com/laMarciana/yiiDocumentor
* @copyright Copyright © 2012 Marc Busqué, on previously © 2008-2011 Yii Software LLC
* @license GNU LESSER GPL 3
* @version 1.0
*/
Yii::import('application.commands.doc.DocModel');
/**
* APP_PATH refers to the application base path
*/
defined("APP_PATH") or define("APP_PATH", dirname(dirname(__FILE__)));
/**
* EXTENSIONS_PATH refers to the extensions base path
*/
defined("EXTENSIONS_PATH") or define("EXTENSIONS_PATH", dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'extensions');
class DocCommand extends CConsoleCommand
{
const URL_PATTERN='/\{\{([^\}]+)\|([^\}]+)\}\}/';
public $pageTitle;
public $classes;
public $packages;
public $themePath;
public $currentClass;
/**
* @var string base path for the source code to document, that's Yii framework directory, application directory or an extension directory
*/
public $basePath;
/**
* @var string url to the base source of Yii code
*/
public $yiiBaseSourceUrl="https://github.com/yiisoft/yii/blob";
/**
* @var string url to the base source of the code documented. Equal to $yiiBaseSourceUrl if what is being documented is Yii itself
*/
public $baseSourceUrl;
/**
* @var string text preffixed to the line number in the anchor used in the url to point to a line number in Yii framework source url. For example, in URLS of the type https://github.com/yiisoft/yii/blob/1.1.8/framework/i18n/CChoiceFormat.php#L72 it's L
*/
public $yiiAnchorLinePreffix = 'L';
/**
* @var string text preffixed to the line number in the anchor used in the url to point to a line number in base source url. For example, in URLS of the type https://github.com/user/myProject/blob/1.0/myClass.php#L72 it's L
*/
public $anchorLinePreffix;
/**
* @var string when an extension is being documented, its directory name
*/
public $extensionDirectory;
/**
* @var boolean whether to include yii framework class reference when documenting an aplication or an extension
*/
public $withYii;
/**
* @var boolean if $whitYii is false, whether to include external links to Yii framework class reference online
*/
public $withYiiLinks;
/**
* @var string name of the project being documented
*/
public $name;
/**
* @var string url of the project being documented
*/
public $url;
public $version;
/**
* @var array options used for CFileHelper::findFiles when scanning Yii framework directory
*/
public $yiiOptions=array(
'fileTypes'=>array('php'),
'exclude'=>array(
'.git',
'.gitignore',
'.svn',
'/yiilite.php',
'/yiit.php',
'/cli',
'/i18n/data',
'/messages',
'/test',
'/vendors',
'/views',
'/web/js',
'/web/widgets/views',
'/utils/mimeTypes.php',
'/gii/assets',
'/gii/components',
'/gii/controllers',
'/gii/generators',
'/gii/models',
'/gii/views',
)
);
/**
* @var array options used for CFileHelper::findFiles when scanning an aplication directory
*/
public $appOptions=array(
'fileTypes' => array('php'),
'exclude' => array(
'.svn',
'.git',
'/yiic.php',
'/commands/doc',
'/commands/DocCommand.php',
'/config',
'/data',
'/extensions',
'/messages',
'/migrations',
'/runtime',
'/vendors',
'/views',
'/tests',
)
);
/**
* @var array options used for CFileHelper::findFiles when scanning an extension directory
*/
public $extOptions = array(
'fileTypes' => array('php'),
'exclude' => array(
'.svn',
'.git',
'views',
),
);
public function getHelp()
{
return <<<EOD
USAGE
yiic doc yii <output-path>
yiic doc app <output-path>
yiic doc ext <output-path> <ext-directory-name>
yiic doc yii check
yiic doc app check
yiic doc ext check <ext-directory-name>
DESCRIPTION
This command generates API reference documentation for the Yii framework, a Yii application or a Yii extension.
When building api reference for an application or an extension, the output can be configured using an 'api.php' configuration file. Look at the documentation at https://github.com/laMarciana/yiiDocumentor for details.
PARAMETERS
* yii: builds API reference documentation for the Yii framework.
* app: builds API reference documentation for a Yii application.
* ext: builds API reference documentation for a Yii extension.
* <output-path>: the directory where generated documentation will be saved. It must already exists.
* <ext-directory-name>: to be used when first parameter is 'ext'. The name of the extension directory relative to 'extensions/'.
* check: check PHPDoc for proper @param syntax for the Yii framework, a Yii application or a Yii extension.
EXAMPLES
* yiic doc yii doc - builds api reference documentation in folder doc for the Yii framework
* yiic doc app doc - builds api reference documentation in folder doc for a Yii application
* yiic doc ext doc MyExtension - builds api reference documentation in folder doc for an extension located in 'extensions/MyExtension'
* yiic doc app check - cheks PHPDoc @param directives for a Yii application
EOD;
}
/**
* Execute the action.
* @param array command line parameters specific for this command
*/
public function run($args)
{
if(!isset($args[0])) {
$this->usageError("api reference target must be specified. It should be 'yii' for the framework, 'app' for an application or 'ext' for an extension.");
}
/*set base path and file options*/
if ($args[0] === 'yii') {
$this->basePath = YII_PATH;
$options = $this->yiiOptions;
} elseif ($args[0] === 'app') {
$this->basePath = APP_PATH;
$options = $this->appOptions;
} elseif ($args[0] === 'ext') {
if (!isset($args[2])) {
$this->usageError("the extension directory name (relative to 'extensions/') must be provided.");
}
$extDirectoryName = $args[2];
$this->basePath = EXTENSIONS_PATH.DIRECTORY_SEPARATOR.$extDirectoryName;
if (!is_dir($this->basePath))
$this->usageError("the extension directory $this->basePath does not exist.");
$options = $this->extOptions;
} else {
$this->usageError("invalid argument value '{$args[0]}' as api reference target. It should be 'yii' for the framework, 'app' for an application or 'ext' for an extension.");
}
if (!isset($args[1]))
$this->usageError("output directory must be specified.");
if($args[1]=='check') {
$checkFiles=CFileHelper::findFiles($this->basePath,$options);
$model=new DocModel;
$model->check($checkFiles);
exit();
}
if(!is_dir($docPath=$args[1]))
$this->usageError("the output directory '{$docPath}' does not exist.");
/*configuration*/
if ($args[0] === 'yii') {
$this->name = 'Yii Framework';
$this->withYii = false;
$this->withYiiLinks = true;
$this->baseSourceUrl = $this->yiiBaseSourceUrl;
$this->anchorLinePreffix = 'L';
} else {
$config_path = $this->basePath.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'api.php';
if (file_exists($config_path)) {
$config = require_once($config_path);
} else {
$config = array();
}
//Add yii reference?
if (isset($config['with_yii'])) {
$this->withYii = $config['with_yii'] === true ? true : false;
} else {
$this->withYii = false;
}
//Add links to external yii class reference?
if (isset($config['with_yii_links'])) {
$this->withYiiLinks = $config['with_yii_links'] === false ? false : true;
} else {
$this->withYiiLinks = true;
}
//Name
if (isset($config['name'])) {
$this->name = $config['name'];
} else {
if ($args[0] === 'app') {
if (isset($config['console_application']) && $config['console_application'] === true) {
$this->name = Yii::app()->name;
} else {
$web_conf = include('config/main.php');
$this->name = isset($web_conf['name']) ? $web_conf['name'] : 'My Application';
}
} elseif ($args[0] === 'ext') {
$this->name = $args[2];
}
}
//url
$this->url = isset($config['url']) ? $config['url'] : null;
//source_url
$this->baseSourceUrl = isset($config['source_url']) ? $config['source_url'] : null;
//line_anchor_preffix
$this->anchorLinePreffix = isset($config['anchor_line_preffix']) ? $config['anchor_line_preffix'] : null;
//exclude options
if (isset($config['exclude'])) {
if (is_array($config['exclude'])) {
$options['exclude'] = array_merge($options['exclude'], $config['exclude']);
}
}
}
/*For an application whic yii and extensions are not included, we need to add extensions folder to the include path*/
if ($this->basePath === APP_PATH) {
self::import_rec('application.extensions.*');
}
$this->version=Yii::getVersion();
/*
* development version - link to master
* release version link to tags
*/
if(substr($this->version,-3)=='dev')
$this->yiiBaseSourceUrl .= '/master/framework';
else
$this->yiiBaseSourceUrl .= '/'.$this->version.'/framework';
$themePath=dirname(__FILE__).'/doc';
echo "\nBuilding.. : " . $this->name." Class Reference \n";
echo "Yii Version... : " . $this->version."\n";
if ($this->baseSourceUrl !== null )echo "Source URL : " . $this->baseSourceUrl."\n\n";
echo "Building model...\n";
$model=$this->buildModel($this->basePath,$options);
$this->classes=$model->classes;
$this->packages=$model->packages;
echo "Building pages...\n";
$this->buildOfflinePages($docPath.DIRECTORY_SEPARATOR.'api',$themePath);
echo "Done.\n\n";
}
protected function buildPackages($docPath)
{
file_put_contents($docPath.'/api/packages.txt',serialize($this->packages));
}
protected function buildKeywords($docPath)
{
$keywords=array();
foreach($this->classes as $class)
$keywords[]=$class->name;
foreach($this->classes as $class)
{
$name=$class->name;
foreach($class->properties as $property)
{
if(!$property->isInherited)
$keywords[]=$name.'.'.$property->name;
}
foreach($class->methods as $method)
{
if(!$method->isInherited)
$keywords[]=$name.'.'.$method->name.'()';
}
}
file_put_contents($docPath.'/api/keywords.txt',implode(',',$keywords));
}
public function render($view,$data=null,$return=false,$layout='main')
{
$viewFile=$this->themePath."/views/{$view}.php";
$layoutFile=$this->themePath."/layouts/{$layout}.php";
$content=$this->renderFile($viewFile,$data,true);
return $this->renderFile($layoutFile,array('content'=>$content),$return);
}
public function renderPartial($view,$data=null,$return=false)
{
$viewFile=$this->themePath."/views/{$view}.php";
return $this->renderFile($viewFile,$data,$return);
}
/**
* @param mixed $sourcePathType type of the source path. 'yii' if it's a Yii framework source path, 'app' if it's for a yii application, 'ext' if it's for an extension or 'current-ext' if it's for an extension and this extension is what is beig documented
*/
public function renderSourceLink($sourcePath,$line=null,$sourcePathType=null)
{
if ($sourcePathType === 'yii') {
$text = 'framework'.$sourcePath;
$url = ($this->withYiiLinks) ? $this->yiiBaseSourceUrl.$sourcePath : null;
$anchorLinePreffix = $this->yiiAnchorLinePreffix;
} elseif ($sourcePathType === 'app' || $sourcePathType === 'ext') {
$text = 'protected'.$sourcePath;
$url = (isset($this->baseSourceUrl) ? $this->baseSourceUrl.$sourcePath : null);
$anchorLinePreffix = $this->anchorLinePreffix;
} elseif ($sourcePathType === 'current-ext') {
$text = 'extensions'.$this->extensionDirectory.$sourcePath;
$url = (isset($this->baseSourceUrl) ? $this->baseSourceUrl.$sourcePath : null);
$anchorLinePreffix = $this->anchorLinePreffix;
}
if ($url === null)
return '';
else {
if($line===null) {
return CHtml::link($text,$url,array('class'=>'sourceLink'));
} else {
return CHtml::link($text.'#'.$anchorLinePreffix.$line,$url.'#'.$anchorLinePreffix.$line,array('class'=>'sourceLink'));
}
}
}
public function highlight($code,$limit=20)
{
$code=preg_replace("/^ /m",'',rtrim(str_replace("\t"," ",$code)));
$code=highlight_string("<?php\n".$code,true);
return preg_replace('/<\\?php<br \\/>/','',$code,1);
}
protected function buildOfflinePages($docPath,$themePath)
{
$this->themePath=$themePath;
@mkdir($docPath);
$content=$this->render('index',null,true);
$content=preg_replace_callback(self::URL_PATTERN,array($this,'fixOfflineLink'),$content);
file_put_contents($docPath.'/index.html',$content);
foreach($this->classes as $name=>$class)
{
$this->currentClass=$name;
$this->pageTitle=$name;
$content=$this->render('class',array('class'=>$class),true);
$content=preg_replace_callback(self::URL_PATTERN,array($this,'fixOfflineLink'),$content);
file_put_contents($docPath.'/'.$name.'.html',$content);
}
CFileHelper::copyDirectory($this->themePath.'/assets',$docPath);
$content=$this->renderPartial('chmProject',null,true);
file_put_contents($docPath.'/manual.hhp',$content);
$content=$this->renderPartial('chmIndex',null,true);
file_put_contents($docPath.'/manual.hhk',$content);
$content=$this->renderPartial('chmContents',null,true);
file_put_contents($docPath.'/manual.hhc',$content);
}
protected function buildModel($sourcePath,$options)
{
$files=CFileHelper::findFiles($sourcePath,$options);
if ($this->withYii) {
foreach(CFileHelper::findFiles(YII_PATH,$this->yiiOptions) as $file) {
$files[] = $file;
}
/*If it's an application with yii, extensions must be included, too*/
if ($this->basePath === APP_PATH) {
foreach(CFileHelper::findFiles(EXTENSIONS_PATH,$this->extOptions) as $file) {
$files[] = $file;
}
}
}
$model=new DocModel(array(
'basePath' => $this->basePath,
'name' => $this->name,
'withYii' => $this->withYii,
'withYiiLinks' => $this->withYiiLinks,
'url' => $this->url,
));
$model->build($files);
return $model;
}
public function renderInheritance($class)
{
$parents=array($class->signature);
foreach($class->parentClasses as $parent)
{
if(isset($this->classes[$parent]))
$parents[]='{{'.$parent.'|'.$parent.'}}';
else
$parents[] = $this->getExcludedClassName($parent);
}
return implode(" »\n",$parents);
}
public function renderImplements($class)
{
$interfaces=array();
foreach($class->interfaces as $interface)
{
if(isset($this->classes[$interface]))
$interfaces[]='{{'.$interface.'|'.$interface.'}}';
else
$interfaces[] = $this->getExcludedClassName($interface);
}
return implode(', ',$interfaces);
}
public function renderSubclasses($class)
{
$subclasses=array();
foreach($class->subclasses as $subclass)
{
if(isset($this->classes[$subclass]))
$subclasses[]='{{'.$subclass.'|'.$subclass.'}}';
else
$subclasses[] = $this->getExcludedClassName($subclass);
}
return implode(', ',$subclasses);
}
public function renderTypeUrl($type,$sourcePathType='')
{
if(isset($this->classes[$type]) && $type!==$this->currentClass)
return '{{'.$type.'|'.$type.'}}';
else {
if ($sourcePathType==='yii' && $this->withYiiLinks && !in_array($type,array('string','integer','int','boolean','bool','float','double','object','mixed','array','resource','void','null','callback','false','true','self'))) {
return self::renderYiiApiLink($type, $type);
} else {
return $type;
}
}
}
/**
* @param mixed $sourcePathType type of the source path. 'yii' if it's a Yii framework source path, 'app' if it's for a yii application, 'ext' if it's for an extension or 'current-ext' if it's for an extension and this extension is what is beig documented
*/
public function renderSubjectUrl($type,$subject,$text=null,$sourcePathType='')
{
if($text===null)
$text=$subject;
if(isset($this->classes[$type])) {
return '{{'.$type.'::'.$subject.'-detail'.'|'.$text.'}}';
}
else {
if ($sourcePathType==='yii' && $this->withYiiLinks) {
return self::renderYiiApiLink($type, $text, $subject);
} else {
return $text;
}
}
}
public function renderPropertySignature($property)
{
if(!empty($property->signature))
return $property->signature;
$sig='';
if(!empty($property->getter))
$sig=$property->getter->signature;
if(!empty($property->setter))
{
if($sig!=='')
$sig.='<br/>';
$sig.=$property->setter->signature;
}
return $sig;
}
public function fixMethodAnchor($class,$name)
{
if(isset($this->classes[$class]->properties[$name]))
return $name."()";
else
return $name;
}
protected function fixOfflineLink($matches)
{
if(($pos=strpos($matches[1],'::'))!==false)
{
$className=substr($matches[1],0,$pos);
$method=substr($matches[1],$pos+2);
return "<a href=\"{$className}.html#{$method}\">{$matches[2]}</a>";
}
else
return "<a href=\"{$matches[1]}.html\">{$matches[2]}</a>";
}
protected function getExcludedClassName($class)
{
$reflection = new ReflectionClass($class);
if ($this->withYiiLinks && (strpos($reflection->getFileName(), YII_PATH)!==false)) {
return self::renderYiiApiLink($class, $class);
} else {
return $class;
}
}
/**
* Render an external link to Yii class reference for a subject
* @param string $class the class name
* @param string $text the text for the link
* @param member $member the class member if any. Null by default
*
* @return string external link to Yii class reference for a subject
*/
static protected function renderYiiApiLink($class, $text, $member=null)
{
$link = '<a href="http://www.yiiframework.com/doc/api/'.substr(Yii::getVersion(), 0, 3).'/'.$class;
if ($member) {
$link .= '/#'.$member.'-detail';
}
$link .= '">'.$text.'</a>';
return $link;
}
/**
* import recursively a directory. Based on issue #1568 of Yii framework (http://code.google.com/p/yii/issues/detail?id=1568)
*/
private static function import_rec($alias)
{
$includePaths=array_unique(explode(PATH_SEPARATOR,get_include_path()));
$path = YiiBase::getPathOfAlias($alias);
array_unshift($includePaths,self::expandPath($path));
set_include_path('.'.PATH_SEPARATOR.implode(PATH_SEPARATOR,$includePaths));
}
/**
* import recursively a directory. Based on issue #1568 of Yii framework (http://code.google.com/p/yii/issues/detail?id=1568)
*/
private static function expandPath($path)
{
$paths='';
$folder=opendir($path);
while(($file=readdir($folder))!==false)
{
if($file[0]!=='.' && is_dir($dir=$path.DIRECTORY_SEPARATOR.$file))
$paths.=self::expandPath($dir).PATH_SEPARATOR;
}
closedir($folder);
return $paths==='' ? $path : $paths.$path;
}
}