BaseDoc.php 3.06 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
use yii\base\Object;
13
use yii\helpers\StringHelper;
14

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

28 29
	public $name;

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

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

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


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

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

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

64 65
		$docblock = $reflector->getDocBlock();
		if ($docblock !== null) {
66
			$this->shortDescription = ucfirst($docblock->getShortDescription());
67 68 69 70 71 72 73
			if (empty($this->shortDescription) && !($this instanceof PropertyDoc) && $context !== null) {
				$context->errors[] = [
					'line' => $this->startLine,
					'file' => $this->sourceFile,
					'message' => "No short description for " . substr(StringHelper::basename(get_class($this)), 0, -3) . " '{$this->name}'",
				];
			}
74
			$this->description = $docblock->getLongDescription();
75

76 77
			$this->phpDocContext = $docblock->getContext();

78 79 80 81 82 83 84 85 86 87 88
			$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]);
				}
			}
89 90 91 92 93 94
		} elseif ($context !== null) {
			$context->errors[] = [
				'line' => $this->startLine,
				'file' => $this->sourceFile,
				'message' => "No docblock for element '{$this->name}'",
			];
95
		}
96 97 98
	}


99
	// TODO
100 101
	public function loadSource($reflection)
	{
Luciano Baraglia committed
102 103 104
		$this->sourcePath = str_replace('\\', '/', str_replace(YII_PATH, '', $reflection->getFileName()));
		$this->startLine = $reflection->getStartLine();
		$this->endLine = $reflection->getEndLine();
105 106
	}

Luciano Baraglia committed
107
	public function getSourceUrl($baseUrl, $line=null)
108
	{
Luciano Baraglia committed
109 110
		if($line === null)
			return $baseUrl . $this->sourcePath;
111
		else
Luciano Baraglia committed
112
			return $baseUrl . $this->sourcePath . '#' . $line;
113 114 115 116
	}

	public function getSourceCode()
	{
Luciano Baraglia committed
117 118
		$lines = file(YII_PATH . $this->sourcePath);
		return implode("", array_slice($lines, $this->startLine - 1, $this->endLine - $this->startLine + 1));
119
	}
Luciano Baraglia committed
120
}