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

8
namespace yii\apidoc\templates\html;
9

Carsten Brandt committed
10
use yii\apidoc\helpers\ApiMarkdown;
11 12
use yii\apidoc\models\MethodDoc;
use yii\apidoc\models\PropertyDoc;
13 14
use yii\apidoc\models\ClassDoc;
use yii\apidoc\models\Context;
15
use yii\apidoc\renderers\ApiRenderer as BaseApiRenderer;
16 17 18
use yii\base\ViewContextInterface;
use yii\helpers\Console;
use yii\helpers\Html;
19 20
use yii\web\AssetManager;
use yii\web\View;
21
use Yii;
22

23 24 25
/**
 * The base class for HTML API documentation renderers.
 *
26 27
 * @property View $view The view instance. This property is read-only.
 *
28 29 30
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
31
class ApiRenderer extends BaseApiRenderer implements ViewContextInterface
32
{
33 34 35
	/**
	 * @var string string to use as the title of the generated page.
	 */
36
	public $pageTitle;
37
	/**
38 39
	 * @var string path or alias of the layout file to use.
	 */
40
	public $layout;
41 42 43 44 45 46 47 48 49 50 51 52
	/**
	 * @var string path or alias of the view file to use for rendering types (classes, interfaces, traits).
	 */
	public $typeView = '@yii/apidoc/templates/html/views/type.php';
	/**
	 * @var string path or alias of the view file to use for rendering the index page.
	 */
	public $indexView = '@yii/apidoc/templates/html/views/index.php';
	/**
	 * @var View
	 */
	private $_view;
53
	private $_targetDir;
54

55 56 57

	public function init()
	{
58 59 60 61 62
		parent::init();

		if ($this->pageTitle === null) {
			$this->pageTitle = 'Yii Framework 2.0 API Documentation'; // TODO guess page title
		}
63 64
	}

65 66 67 68 69 70 71
	/**
	 * @return View the view instance
	 */
	public function getView()
	{
		if ($this->_view === null) {
			$this->_view = new View();
72
			$assetPath = Yii::getAlias($this->_targetDir) . '/assets';
73 74 75 76 77
			if (!is_dir($assetPath)) {
				mkdir($assetPath);
			}
			$this->_view->assetManager = new AssetManager([
				'basePath' => $assetPath,
Carsten Brandt committed
78
				'baseUrl' => './assets',
79 80 81 82
			]);
		}
		return $this->_view;
	}
83 84

	/**
85 86 87
	 * Renders a given [[Context]].
	 *
	 * @param Context $context the api documentation context to render.
88
	 * @param $targetDir
89
	 */
90
	public function render($context, $targetDir)
91
	{
92 93
		$this->apiContext = $context;
		$this->_targetDir = $targetDir;
94

Carsten Brandt committed
95 96
		$types = array_merge($context->classes, $context->interfaces, $context->traits);
		$typeCount = count($types) + 1;
97 98 99 100

		if ($this->controller !== null) {
			Console::startProgress(0, $typeCount, 'Rendering files: ', false);
		}
101
		$done = 0;
Luciano Baraglia committed
102
		foreach ($types as $type) {
Carsten Brandt committed
103 104
			$fileContent = $this->renderWithLayout($this->typeView, [
				'type' => $type,
105
				'apiContext' => $context,
106
				'types' => $types,
107
			]);
108 109 110 111 112
			file_put_contents($targetDir . '/' . $this->generateFileName($type->name), $fileContent);

			if ($this->controller !== null) {
				Console::updateProgress(++$done, $typeCount);
			}
113
		}
114

115
		$indexFileContent = $this->renderWithLayout($this->indexView, [
116
			'apiContext' => $context,
Carsten Brandt committed
117
			'types' => $types,
118
		]);
119 120 121 122 123 124 125
		file_put_contents($targetDir . '/index.html', $indexFileContent);

		if ($this->controller !== null) {
			Console::updateProgress(++$done, $typeCount);
			Console::endProgress(true);
			$this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
		}
126 127 128 129 130
	}

	protected function renderWithLayout($viewFile, $params)
	{
		$output = $this->getView()->render($viewFile, $params, $this);
131
		if ($this->layout !== false) {
132
			$params['content'] = $output;
133
			return $this->getView()->renderFile($this->layout, $params, $this);
134 135 136 137 138 139 140 141 142 143 144
		} else {
			return $output;
		}
	}

	/**
	 * @param ClassDoc $class
	 * @return string
	 */
	public function renderInheritance($class)
	{
145
		$parents = [];
146
		$parents[] = $this->createTypeLink($class);
147
		while ($class->parentClass !== null) {
Luciano Baraglia committed
148
			if (isset($this->apiContext->classes[$class->parentClass])) {
149 150
				$class = $this->apiContext->classes[$class->parentClass];
				$parents[] = $this->createTypeLink($class);
151
			} else {
152
				$parents[] = $this->createTypeLink($class->parentClass);
153 154 155
				break;
			}
		}
Luciano Baraglia committed
156
		return implode(" &raquo;\n", $parents);
157 158 159
	}

	/**
160
	 * @param array $names
161 162
	 * @return string
	 */
163
	public function renderInterfaces($names)
164 165
	{
		$interfaces = [];
166
		sort($names, SORT_STRING);
Luciano Baraglia committed
167 168
		foreach ($names as $interface) {
			if (isset($this->apiContext->interfaces[$interface])) {
169
				$interfaces[] = $this->createTypeLink($this->apiContext->interfaces[$interface]);
170
			} else {
171
				$interfaces[] = $this->createTypeLink($interface);
172 173
			}
		}
Luciano Baraglia committed
174
		return implode(', ', $interfaces);
175 176 177
	}

	/**
178
	 * @param array $names
179 180
	 * @return string
	 */
181
	public function renderTraits($names)
182 183
	{
		$traits = [];
184
		sort($names, SORT_STRING);
Luciano Baraglia committed
185 186
		foreach ($names as $trait) {
			if (isset($this->apiContext->traits[$trait])) {
187
				$traits[] = $this->createTypeLink($this->apiContext->traits[$trait]);
188
			} else {
189
				$traits[] = $this->createTypeLink($trait);
190 191
			}
		}
Luciano Baraglia committed
192
		return implode(', ', $traits);
193 194
	}

195 196 197 198 199
	/**
	 * @param array $names
	 * @return string
	 */
	public function renderClasses($names)
200
	{
201 202
		$classes = [];
		sort($names, SORT_STRING);
Luciano Baraglia committed
203 204
		foreach ($names as $class) {
			if (isset($this->apiContext->classes[$class])) {
205
				$classes[] = $this->createTypeLink($this->apiContext->classes[$class]);
206
			} else {
207
				$classes[] = $this->createTypeLink($class);
208 209
			}
		}
Luciano Baraglia committed
210
		return implode(', ', $classes);
211 212
	}

213 214 215 216 217 218
	/**
	 * @param PropertyDoc $property
	 * @return string
	 */
	public function renderPropertySignature($property)
	{
219 220 221 222 223 224 225 226 227
		if ($property->getter !== null || $property->setter !== null) {
			$sig = [];
			if ($property->getter !== null) {
				$sig[] = $this->renderMethodSignature($property->getter);
			}
			if ($property->setter !== null) {
				$sig[] = $this->renderMethodSignature($property->setter);
			}
			return implode('<br />', $sig);
228
		}
Carsten Brandt committed
229 230
		return $this->createTypeLink($property->types) . ' ' . $this->createSubjectLink($property, $property->name) . ' '
				. ApiMarkdown::highlight('= ' . ($property->defaultValue === null ? 'null' : $property->defaultValue), 'php');
231 232 233 234 235 236 237 238 239
	}

	/**
	 * @param MethodDoc $method
	 * @return string
	 */
	public function renderMethodSignature($method)
	{
		$params = [];
Luciano Baraglia committed
240
		foreach ($method->params as $param) {
241 242 243 244 245 246 247
			$params[] = (empty($param->typeHint) ? '' : $param->typeHint . ' ')
				. ($param->isPassedByReference ? '<b>&</b>' : '')
				. $param->name
				. ($param->isOptional ? ' = ' . $param->defaultValue : '');
		}

		return ($method->isReturnByReference ? '<b>&</b>' : '')
248
			. ($method->returnType === null ? 'void' : $this->createTypeLink($method->returnTypes))
Carsten Brandt committed
249 250
			. ' <strong>' . $this->createSubjectLink($method, $method->name) . '</strong>'
			. ApiMarkdown::highlight(str_replace('  ', ' ', '( ' . implode(', ', $params) . ' )'), 'php');
251
	}
252

253
	public function generateApiUrl($typeName)
254 255 256 257
	{
		return $this->generateFileName($typeName);
	}

258
	protected function generateFileName($typeName)
259
	{
260
		return strtolower(str_replace('\\', '-', $typeName)) . '.html';
261 262 263 264 265 266 267 268 269
	}

	/**
	 * Finds the view file corresponding to the specified relative view name.
	 * @param string $view a relative view name. The name does NOT start with a slash.
	 * @return string the view file path. Note that the file may not exist.
	 */
	public function findViewFile($view)
	{
270
		return Yii::getAlias('@yii/apidoc/templates/html/views/' . $view);
271
	}
272 273

	/**
Carsten Brandt committed
274
	 * @inheritdoc
275
	 */
Carsten Brandt committed
276
	protected function generateLink($text, $href, $options = [])
277
	{
Carsten Brandt committed
278 279
		$options['href'] = $href;
		return Html::a($text, null, $options);
280
	}
281 282 283 284 285

	public function getSourceUrl($type)
	{
		return null;
	}
Luciano Baraglia committed
286
}