OfflineRenderer.php 7.77 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\components;
9 10


11
use yii\apidoc\models\BaseDoc;
12 13 14 15
use yii\apidoc\models\ConstDoc;
use yii\apidoc\models\EventDoc;
use yii\apidoc\models\MethodDoc;
use yii\apidoc\models\PropertyDoc;
16
use yii\apidoc\models\TypeDoc;
17 18 19 20 21
use yii\base\ViewContextInterface;
use yii\console\Controller;
use yii\helpers\Console;
use yii\helpers\FileHelper;
use yii\helpers\Html;
22 23
use yii\apidoc\models\ClassDoc;
use yii\apidoc\models\Context;
24
use Yii;
25 26
use yii\apidoc\models\InterfaceDoc;
use yii\apidoc\models\TraitDoc;
27 28 29 30 31

class OfflineRenderer extends BaseRenderer implements ViewContextInterface
{
	public $targetDir;

32
	public $layout = '@yii/apidoc/views/layouts/offline.php';
Carsten Brandt committed
33
	public $typeView = '@yii/apidoc/views/type.php';
34
	public $indexView = '@yii/apidoc/views/index.php';
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

	public $pageTitle = 'Yii Framework 2.0 API Documentation';

	/**
	 * @var Context
	 */
	protected $context;

	/**
	 * @param Context $context
	 * @param Controller $controller
	 */
	public function render($context, $controller)
	{
		$this->context = $context;
		$dir = Yii::getAlias($this->targetDir);
		if (!is_dir($dir)) {
			mkdir($dir);
		}

Carsten Brandt committed
55 56 57
		$types = array_merge($context->classes, $context->interfaces, $context->traits);
		$typeCount = count($types) + 1;
		Console::startProgress(0, $typeCount, 'Rendering files: ', false);
58
		$done = 0;
Carsten Brandt committed
59 60 61
		foreach($types as $type) {
			$fileContent = $this->renderWithLayout($this->typeView, [
				'type' => $type,
62 63
				'docContext' => $context,
			]);
Carsten Brandt committed
64 65
			file_put_contents($dir . '/' . $this->generateFileName($type->name), $fileContent);
			Console::updateProgress(++$done, $typeCount);
66 67 68
		}
		$indexFileContent = $this->renderWithLayout($this->indexView, [
			'docContext' => $context,
Carsten Brandt committed
69
			'types' => $types,
70 71
		]);
		file_put_contents($dir . '/index.html', $indexFileContent);
Carsten Brandt committed
72
		Console::updateProgress(++$done, $typeCount);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		Console::endProgress(true);
		$controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);

		$controller->stdout('Copying asset files... ');
		FileHelper::copyDirectory(__DIR__ . '/../assets/css', $dir . '/css');
		$controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);

	}

	protected function renderWithLayout($viewFile, $params)
	{
		$output = $this->getView()->render($viewFile, $params, $this);
		if ($this->layout !== false) {
			$params['content'] = $output;
			return $this->getView()->renderFile($this->layout, $params, $this);
		} else {
			return $output;
		}
	}

	/**
94 95
	 * creates a link to a type (class, interface or trait)
	 * @param ClassDoc|InterfaceDoc|TraitDoc $types
96 97 98
	 * @param string $title
	 * @return string
	 */
99
	public function typeLink($types, $context = null)
100 101 102 103 104 105
	{
		if (!is_array($types)) {
			$types = [$types];
		}
		$links = [];
		foreach($types as $type) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119
			$postfix = '';
			if (!is_object($type)) {
				if (substr($type, -2, 2) == '[]') {
					$postfix = '[]';
					$type = substr($type, 0, -2);
				}

				if (($t = $this->context->getType(ltrim($type, '\\'))) !== null) {
					$type = $t;
				} elseif ($type[0] !== '\\' && ($t = $this->context->getType($this->resolveNamespace($context) . '\\' . ltrim($type, '\\'))) !== null) {
					$type = $t;
				} else {
					ltrim($type, '\\');
				}
120 121 122 123 124
			}
			if (!is_object($type)) {
				$links[] = $type;
			} else {
				$links[] = Html::a(
125
					$type->name,
126 127
					null,
					['href' => $this->generateFileName($type->name)]
128
				) . $postfix;
129 130 131 132 133 134 135 136 137 138 139 140
			}
		}
		return implode('|', $links);
	}

	/**
	 * creates a link to a subject
	 * @param PropertyDoc|MethodDoc|ConstDoc|EventDoc $subject
	 * @param string $title
	 * @return string
	 */
	public function subjectLink($subject, $title = null)
141 142
	{
		if ($title === null) {
143 144 145 146 147
			$title = $subject->name;
		}
		if (($type = $this->context->getType($subject->definedBy)) === null) {
			return $subject->name;
		} else {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
			$link = $this->generateFileName($type->name);
			if ($subject instanceof MethodDoc) {
				$link .= '#' . $subject->name . '()';
			} else {
				$link .= '#' . $subject->name;
			}
			$link .= '-detail';
			return Html::a($title, null, ['href' => $link]);
		}
	}

	/**
	 * @param BaseDoc $context
	 */
	private function resolveNamespace($context)
	{
		if ($context === null) {
			return '';
		}
		if ($context instanceof TypeDoc) {
			return $context->namespace;
169
		}
170 171 172 173 174 175 176
		if ($context->hasProperty('definedBy')) {
			$type = $this->context->getType($context);
			if ($type !== null) {
				return $type->namespace;
			}
		}
		return '';
177 178 179 180 181 182 183 184
	}

	/**
	 * @param ClassDoc $class
	 * @return string
	 */
	public function renderInheritance($class)
	{
185
		$parents[] = $this->typeLink($class);
186 187 188
		while ($class->parentClass !== null) {
			if(isset($this->context->classes[$class->parentClass])) {
				$class = $this->context->classes[$class->parentClass];
189
				$parents[] = $this->typeLink($class);
190 191 192 193 194 195 196 197 198
			} else {
				$parents[] = $class->parentClass; // TODO link to php.net
				break;
			}
		}
		return implode(" &raquo;\n",$parents);
	}

	/**
199
	 * @param array $names
200 201
	 * @return string
	 */
202
	public function renderInterfaces($names)
203 204
	{
		$interfaces = [];
205 206
		sort($names, SORT_STRING);
		foreach($names as $interface) {
207
			if(isset($this->context->interfaces[$interface])) {
208
				$interfaces[] = $this->typeLink($this->context->interfaces[$interface]);
209 210 211 212 213 214 215 216
			} else {
				$interfaces[] = $interface; // TODO link to php.net
			}
		}
		return implode(', ',$interfaces);
	}

	/**
217
	 * @param array $names
218 219
	 * @return string
	 */
220
	public function renderTraits($names)
221 222
	{
		$traits = [];
223 224
		sort($names, SORT_STRING);
		foreach($names as $trait) {
225
			if(isset($this->context->traits[$trait])) {
226
				$traits[] = $this->typeLink($this->context->traits[$trait]);
227 228 229 230 231 232 233
			} else {
				$traits[] = $trait; // TODO link to php.net
			}
		}
		return implode(', ',$traits);
	}

234 235 236 237 238
	/**
	 * @param array $names
	 * @return string
	 */
	public function renderClasses($names)
239
	{
240 241 242 243 244
		$classes = [];
		sort($names, SORT_STRING);
		foreach($names as $class) {
			if(isset($this->context->classes[$class])) {
				$classes[] = $this->typeLink($this->context->classes[$class]);
245
			} else {
246
				$classes[] = $class; // TODO link to php.net
247 248
			}
		}
249
		return implode(', ',$classes);
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
	/**
	 * @param PropertyDoc $property
	 * @return string
	 */
	public function renderPropertySignature($property)
	{
		return $this->typeLink($property->types) . ' ' . $property->name . ' = ' . ($property->defaultValue === null ? 'null' : $property->defaultValue);
		// TODO
		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;
	}

	/**
	 * @param MethodDoc $method
	 * @return string
	 */
	public function renderMethodSignature($method)
	{
		$params = [];
		foreach($method->params as $param) {
			$params[] = (empty($param->typeHint) ? '' : $param->typeHint . ' ')
				. ($param->isPassedByReference ? '<b>&</b>' : '')
				. $param->name
				. ($param->isOptional ? ' = ' . $param->defaultValue : '');
		}

		//<?php echo preg_replace('/\{\{([^\{\}]*?)\|([^\{\}]*?)\}\}\(/','$2(',$method->signature);

		return ($method->isReturnByReference ? '<b>&</b>' : '')
			. ($method->returnType === null ? 'void' : $this->typeLink($method->returnTypes))
			. ' ' . $method->name . '( '
			. implode(', ', $params)
			. ' )';
	}
296

Carsten Brandt committed
297
	public function generateFileName($typeName)
298
	{
Carsten Brandt committed
299
		return strtolower(str_replace('\\', '_', $typeName)) . '.html';
300 301 302 303 304 305 306 307 308
	}

	/**
	 * 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)
	{
309
		return Yii::getAlias('@yii/apidoc/views/' . $view);
310 311
	}
}