OfflineRenderer.php 5.67 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 12 13 14
use yii\apidoc\models\ConstDoc;
use yii\apidoc\models\EventDoc;
use yii\apidoc\models\MethodDoc;
use yii\apidoc\models\PropertyDoc;
15 16 17 18 19
use yii\base\ViewContextInterface;
use yii\console\Controller;
use yii\helpers\Console;
use yii\helpers\FileHelper;
use yii\helpers\Html;
20 21
use yii\apidoc\models\ClassDoc;
use yii\apidoc\models\Context;
22
use Yii;
23 24
use yii\apidoc\models\InterfaceDoc;
use yii\apidoc\models\TraitDoc;
25 26 27 28 29

class OfflineRenderer extends BaseRenderer implements ViewContextInterface
{
	public $targetDir;

30 31 32
	public $layout = '@yii/apidoc/views/layouts/offline.php';
	public $itemView = '@yii/apidoc/views/class.php';
	public $indexView = '@yii/apidoc/views/index.php';
33 34 35 36 37 38 39 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

	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);
		}

		$items = array_merge($context->classes, $context->interfaces, $context->traits);
		$itemCount = count($items) + 1;
		Console::startProgress(0, $itemCount, 'Rendering files: ', false);
		$done = 0;
		foreach($items as $item) {
			$fileContent = $this->renderWithLayout($this->itemView, [
				'item' => $item,
				'docContext' => $context,
			]);
			file_put_contents($dir . '/' . $this->generateFileName($item->name), $fileContent);
			Console::updateProgress(++$done, $itemCount);
		}
		$indexFileContent = $this->renderWithLayout($this->indexView, [
			'docContext' => $context,
			'items' => $items,
		]);
		file_put_contents($dir . '/index.html', $indexFileContent);
		Console::updateProgress(++$done, $itemCount);
		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;
		}
	}

	/**
92 93
	 * creates a link to a type (class, interface or trait)
	 * @param ClassDoc|InterfaceDoc|TraitDoc $types
94 95 96
	 * @param string $title
	 * @return string
	 */
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	public function typeLink($types, $title = null)
	{
		if (!is_array($types)) {
			$types = [$types];
		}
		$links = [];
		foreach($types as $type) {
			if (!is_object($type) && ($t = $this->context->getType($type)) !== null) {
				$type = $t;
			}
			if (!is_object($type)) {
				$links[] = $type;
			} else {
				$links[] = Html::a(
					$title !== null ? $title : $type->name,
					null,
					['href' => $this->generateFileName($type->name)]
				);
			}
		}
		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)
127 128
	{
		if ($title === null) {
129 130 131 132 133 134
			$title = $subject->name;
		}
		if (($type = $this->context->getType($subject->definedBy)) === null) {
			return $subject->name;
		} else {
			return Html::a($title, null, ['href' => $this->generateFileName($type->name) . '#' . $subject->name . '-detail']);
135 136 137 138 139 140 141 142 143
		}
	}

	/**
	 * @param ClassDoc $class
	 * @return string
	 */
	public function renderInheritance($class)
	{
144
		$parents[] = $this->typeLink($class);
145 146 147
		while ($class->parentClass !== null) {
			if(isset($this->context->classes[$class->parentClass])) {
				$class = $this->context->classes[$class->parentClass];
148
				$parents[] = $this->typeLink($class);
149 150 151 152 153 154 155 156 157
			} else {
				$parents[] = $class->parentClass; // TODO link to php.net
				break;
			}
		}
		return implode(" &raquo;\n",$parents);
	}

	/**
158
	 * @param array $names
159 160
	 * @return string
	 */
161
	public function renderInterfaces($names)
162 163
	{
		$interfaces = [];
164 165
		sort($names, SORT_STRING);
		foreach($names as $interface) {
166
			if(isset($this->context->interfaces[$interface])) {
167
				$interfaces[] = $this->typeLink($this->context->interfaces[$interface]);
168 169 170 171 172 173 174 175
			} else {
				$interfaces[] = $interface; // TODO link to php.net
			}
		}
		return implode(', ',$interfaces);
	}

	/**
176
	 * @param array $names
177 178
	 * @return string
	 */
179
	public function renderTraits($names)
180 181
	{
		$traits = [];
182 183
		sort($names, SORT_STRING);
		foreach($names as $trait) {
184
			if(isset($this->context->traits[$trait])) {
185
				$traits[] = $this->typeLink($this->context->traits[$trait]);
186 187 188 189 190 191 192
			} else {
				$traits[] = $trait; // TODO link to php.net
			}
		}
		return implode(', ',$traits);
	}

193 194 195 196 197
	/**
	 * @param array $names
	 * @return string
	 */
	public function renderClasses($names)
198
	{
199 200 201 202 203
		$classes = [];
		sort($names, SORT_STRING);
		foreach($names as $class) {
			if(isset($this->context->classes[$class])) {
				$classes[] = $this->typeLink($this->context->classes[$class]);
204
			} else {
205
				$classes[] = $class; // TODO link to php.net
206 207
			}
		}
208
		return implode(', ',$classes);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	}


	public function generateFileName($itemName)
	{
		return strtolower(str_replace('\\', '_', $itemName)) . '.html';
	}

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