BaseDoc.php 2.44 KB
Newer Older
1
<?php
2 3 4 5 6
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
7

8
namespace yii\apidoc\models;
9

10 11
use phpDocumentor\Reflection\DocBlock\Tag\DeprecatedTag;
use phpDocumentor\Reflection\DocBlock\Tag\SinceTag;
12 13
use yii\base\Object;

14
/**
15
 * Base class for API documentation information.
16 17 18 19
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
20 21
class BaseDoc extends Object
{
22 23 24 25 26
	/**
	 * @var \phpDocumentor\Reflection\DocBlock\Context
	 */
	public $phpDocContext;

27 28
	public $name;

29 30 31
	public $sourceFile;
	public $startLine;
	public $endLine;
32 33 34

	public $shortDescription;
	public $description;
35 36 37 38
	public $since;
	public $deprecatedSince;
	public $deprecatedReason;

39 40 41
	/**
	 * @var \phpDocumentor\Reflection\DocBlock\Tag[]
	 */
42
	public $tags = [];
43 44


45 46 47 48
	/**
	 * @param \phpDocumentor\Reflection\BaseReflector $reflector
	 * @param array $config
	 */
49
	public function __construct($reflector = null, $config = [])
50
	{
51 52 53 54 55 56
		parent::__construct($config);

		if ($reflector === null) {
			return;
		}

57 58 59 60 61
		// base properties
		$this->name = ltrim($reflector->getName(), '\\');
		$this->startLine = $reflector->getNode()->getAttribute('startLine');
		$this->endLine = $reflector->getNode()->getAttribute('endLine');

62 63
		$docblock = $reflector->getDocBlock();
		if ($docblock !== null) {
64
			$this->shortDescription = ucfirst($docblock->getShortDescription());
65
			$this->description = $docblock->getLongDescription();
66

67 68
			$this->phpDocContext = $docblock->getContext();

69 70 71 72 73 74 75 76 77 78 79 80
			$this->tags = $docblock->getTags();
			foreach($this->tags as $i => $tag) {
				if ($tag instanceof SinceTag) {
					$this->since = $tag->getVersion();
					unset($this->tags[$i]);
				} elseif ($tag instanceof DeprecatedTag) {
					$this->deprecatedSince = $tag->getVersion();
					$this->deprecatedReason = $tag->getDescription();
					unset($this->tags[$i]);
				}
			}
		}
81 82 83
	}


84
	// TODO
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	public function loadSource($reflection)
	{
		$this->sourcePath=str_replace('\\','/',str_replace(YII_PATH,'',$reflection->getFileName()));
		$this->startLine=$reflection->getStartLine();
		$this->endLine=$reflection->getEndLine();
	}

	public function getSourceUrl($baseUrl,$line=null)
	{
		if($line===null)
			return $baseUrl.$this->sourcePath;
		else
			return $baseUrl.$this->sourcePath.'#'.$line;
	}

	public function getSourceCode()
	{
		$lines=file(YII_PATH.$this->sourcePath);
		return implode("",array_slice($lines,$this->startLine-1,$this->endLine-$this->startLine+1));
	}
}