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

namespace yii\apidoc\models;

use phpDocumentor\Reflection\DocBlock\Tag\AuthorTag;
use yii\base\Exception;
12
use yii\helpers\StringHelper;
13

14
/**
15
 * Base class for API documentation information for classes, interfaces and traits.
16
 *
17 18 19 20 21 22 23
 * @property MethodDoc[] $nativeMethods This property is read-only.
 * @property PropertyDoc[] $nativeProperties This property is read-only.
 * @property MethodDoc[] $protectedMethods This property is read-only.
 * @property PropertyDoc[] $protectedProperties This property is read-only.
 * @property MethodDoc[] $publicMethods This property is read-only.
 * @property PropertyDoc[] $publicProperties This property is read-only.
 *
24 25 26
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
27 28 29
class TypeDoc extends BaseDoc
{
	public $authors = [];
30 31 32
	/**
	 * @var MethodDoc[]
	 */
33
	public $methods = [];
34 35 36
	/**
	 * @var PropertyDoc[]
	 */
37 38
	public $properties = [];

39
	public $namespace;
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

	public function findSubject($subjectName)
	{
		if ($subjectName[0] != '$') {
			foreach($this->methods as $name => $method) {
				if (rtrim($subjectName, '()') == $name) {
					return $method;
				}
			}
		}
		if (substr($subjectName, -2, 2) == '()') {
			return null;
		}
		if ($this->properties === null) {
			return null;
		}
		foreach($this->properties as $name => $property) {
			if (ltrim($subjectName, '$') == ltrim($name, '$')) {
				return $property;
			}
		}
		return null;
	}

65 66 67 68 69 70 71 72 73 74 75
	/**
	 * @return MethodDoc[]
	 */
	public function getNativeMethods()
	{
		return $this->getFilteredMethods(null, $this->name);
	}

	/**
	 * @return MethodDoc[]
	 */
76 77 78 79 80
	public function getPublicMethods()
	{
		return $this->getFilteredMethods('public');
	}

81 82 83
	/**
	 * @return MethodDoc[]
	 */
84 85 86 87 88
	public function getProtectedMethods()
	{
		return $this->getFilteredMethods('protected');
	}

89 90 91 92 93 94
	/**
	 * @param null $visibility
	 * @param null $definedBy
	 * @return MethodDoc[]
	 */
	private function getFilteredMethods($visibility = null, $definedBy = null)
95 96
	{
		$methods = [];
97
		foreach($this->methods as $name => $method) {
98 99 100 101 102
			if ($visibility !== null && $method->visibility != $visibility) {
				continue;
			}
			if ($definedBy !== null && $method->definedBy != $definedBy) {
				continue;
103
			}
104
			$methods[$name] = $method;
105 106 107 108
		}
		return $methods;
	}

109 110 111 112 113 114 115 116 117 118 119
	/**
	 * @return PropertyDoc[]
	 */
	public function getNativeProperties()
	{
		return $this->getFilteredProperties(null, $this->name);
	}

	/**
	 * @return PropertyDoc[]
	 */
120 121 122 123 124
	public function getPublicProperties()
	{
		return $this->getFilteredProperties('public');
	}

125 126 127
	/**
	 * @return PropertyDoc[]
	 */
128 129 130 131 132
	public function getProtectedProperties()
	{
		return $this->getFilteredProperties('protected');
	}

133 134 135 136 137 138
	/**
	 * @param null $visibility
	 * @param null $definedBy
	 * @return PropertyDoc[]
	 */
	private function getFilteredProperties($visibility = null, $definedBy = null)
139 140 141 142 143
	{
		if ($this->properties === null) {
			return [];
		}
		$properties = [];
144
		foreach($this->properties as $name => $property) {
145 146 147 148 149
			if ($visibility !== null && $property->visibility != $visibility) {
				continue;
			}
			if ($definedBy !== null && $property->definedBy != $definedBy) {
				continue;
150
			}
151
			$properties[$name] = $property;
152 153 154 155
		}
		return $properties;
	}

156 157
	/**
	 * @param \phpDocumentor\Reflection\InterfaceReflector $reflector
158
	 * @param Context $context
159 160
	 * @param array $config
	 */
161
	public function __construct($reflector = null, $context = null, $config = [])
162
	{
163
		parent::__construct($reflector, $context, $config);
164

165
		$this->namespace = StringHelper::dirname($this->name);
166

167 168 169 170 171 172 173 174 175 176 177 178 179
		if ($reflector === null) {
			return;
		}

		foreach($this->tags as $i => $tag) {
			if ($tag instanceof AuthorTag) {
				$this->authors[$tag->getAuthorName()] = $tag->getAuthorEmail();
				unset($this->tags[$i]);
			}
		}

		foreach($reflector->getProperties() as $propertyReflector) {
			if ($propertyReflector->getVisibility() != 'private') {
180
				$property = new PropertyDoc($propertyReflector, $context, ['sourceFile' => $this->sourceFile]);
181 182 183 184 185 186 187
				$property->definedBy = $this->name;
				$this->properties[$property->name] = $property;
			}
		}

		foreach($reflector->getMethods() as $methodReflector) {
			if ($methodReflector->getVisibility() != 'private') {
188
				$method = new MethodDoc($methodReflector, $context, ['sourceFile' => $this->sourceFile]);
189 190 191 192 193 194
				$method->definedBy = $this->name;
				$this->methods[$method->name] = $method;
			}
		}
	}
}