You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					349 lines
				
				8.8 KiB
			
		
		
			
		
	
	
					349 lines
				
				8.8 KiB
			| 
											12 years ago
										 | <?php
 | ||
|  | /**
 | ||
| 
											12 years ago
										 |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
| 
											12 years ago
										 |  */
 | ||
|  | 
 | ||
| 
											12 years ago
										 | namespace yii\apidoc\templates\html;
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | use yii\apidoc\models\BaseDoc;
 | ||
| 
											12 years ago
										 | use yii\apidoc\models\ConstDoc;
 | ||
|  | use yii\apidoc\models\EventDoc;
 | ||
|  | use yii\apidoc\models\MethodDoc;
 | ||
|  | use yii\apidoc\models\PropertyDoc;
 | ||
| 
											12 years ago
										 | use yii\apidoc\models\TypeDoc;
 | ||
| 
											12 years ago
										 | use yii\apidoc\models\ClassDoc;
 | ||
|  | use yii\apidoc\models\Context;
 | ||
|  | use yii\apidoc\models\InterfaceDoc;
 | ||
|  | use yii\apidoc\models\TraitDoc;
 | ||
|  | use yii\apidoc\templates\BaseRenderer;
 | ||
| 
											12 years ago
										 | use yii\base\ViewContextInterface;
 | ||
|  | use yii\console\Controller;
 | ||
|  | use yii\helpers\Console;
 | ||
|  | use yii\helpers\Html;
 | ||
|  | use Yii;
 | ||
| 
											12 years ago
										 | use yii\web\AssetManager;
 | ||
|  | use yii\web\View;
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | /**
 | ||
|  |  * The base class for HTML API documentation renderers.
 | ||
|  |  *
 | ||
|  |  * @author Carsten Brandt <mail@cebe.cc>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
|  | abstract class Renderer extends BaseRenderer implements ViewContextInterface
 | ||
| 
											12 years ago
										 | {
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @var string directory to use for output of html files. Can be a path alias.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public $targetDir;
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @var string string to use as the title of the generated page.
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public $pageTitle = 'Yii Framework 2.0 API Documentation';
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * @var string path or alias of the layout file to use.
 | ||
|  | 	 */
 | ||
|  | 	public $layout;
 | ||
|  | 	/**
 | ||
|  | 	 * @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 Context the [[Context]] currently being rendered.
 | ||
| 
											12 years ago
										 | 	 */
 | ||
|  | 	protected $context;
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @var View
 | ||
|  | 	 */
 | ||
|  | 	private $_view;
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return View the view instance
 | ||
|  | 	 */
 | ||
|  | 	public function getView()
 | ||
|  | 	{
 | ||
|  | 		if ($this->_view === null) {
 | ||
|  | 			$this->_view = new View();
 | ||
|  | 			$assetPath = Yii::getAlias($this->targetDir) . '/assets';
 | ||
|  | 			if (!is_dir($assetPath)) {
 | ||
|  | 				mkdir($assetPath);
 | ||
|  | 			}
 | ||
|  | 			$this->_view->assetManager = new AssetManager([
 | ||
|  | 				'basePath' => $assetPath,
 | ||
|  | 				'baseUrl' => '/assets',
 | ||
|  | 			]);
 | ||
|  | 		}
 | ||
|  | 		return $this->_view;
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * Renders a given [[Context]].
 | ||
|  | 	 *
 | ||
|  | 	 * @param Context $context the api documentation context to render.
 | ||
|  | 	 * @param Controller $controller the apidoc controller instance. Can be used to control output.
 | ||
| 
											12 years ago
										 | 	 */
 | ||
|  | 	public function render($context, $controller)
 | ||
|  | 	{
 | ||
|  | 		$this->context = $context;
 | ||
|  | 		$dir = Yii::getAlias($this->targetDir);
 | ||
|  | 		if (!is_dir($dir)) {
 | ||
|  | 			mkdir($dir);
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 		$types = array_merge($context->classes, $context->interfaces, $context->traits);
 | ||
|  | 		$typeCount = count($types) + 1;
 | ||
|  | 		Console::startProgress(0, $typeCount, 'Rendering files: ', false);
 | ||
| 
											12 years ago
										 | 		$done = 0;
 | ||
| 
											12 years ago
										 | 		foreach($types as $type) {
 | ||
|  | 			$fileContent = $this->renderWithLayout($this->typeView, [
 | ||
|  | 				'type' => $type,
 | ||
| 
											12 years ago
										 | 				'docContext' => $context,
 | ||
|  | 			]);
 | ||
| 
											12 years ago
										 | 			file_put_contents($dir . '/' . $this->generateFileName($type->name), $fileContent);
 | ||
|  | 			Console::updateProgress(++$done, $typeCount);
 | ||
| 
											12 years ago
										 | 		}
 | ||
|  | 		$indexFileContent = $this->renderWithLayout($this->indexView, [
 | ||
|  | 			'docContext' => $context,
 | ||
| 
											12 years ago
										 | 			'types' => $types,
 | ||
| 
											12 years ago
										 | 		]);
 | ||
|  | 		file_put_contents($dir . '/index.html', $indexFileContent);
 | ||
| 
											12 years ago
										 | 		Console::updateProgress(++$done, $typeCount);
 | ||
| 
											12 years ago
										 | 		Console::endProgress(true);
 | ||
|  | 		$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;
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * creates a link to a type (class, interface or trait)
 | ||
|  | 	 * @param ClassDoc|InterfaceDoc|TraitDoc $types
 | ||
| 
											12 years ago
										 | 	 * @param BaseDoc $context
 | ||
| 
											12 years ago
										 | 	 * @return string
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function typeLink($types, $context = null)
 | ||
| 
											12 years ago
										 | 	{
 | ||
|  | 		if (!is_array($types)) {
 | ||
|  | 			$types = [$types];
 | ||
|  | 		}
 | ||
|  | 		$links = [];
 | ||
|  | 		foreach($types as $type) {
 | ||
| 
											12 years ago
										 | 			$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, '\\');
 | ||
|  | 				}
 | ||
| 
											12 years ago
										 | 			}
 | ||
|  | 			if (!is_object($type)) {
 | ||
|  | 				$links[] = $type;
 | ||
|  | 			} else {
 | ||
|  | 				$links[] = Html::a(
 | ||
| 
											12 years ago
										 | 					$type->name,
 | ||
| 
											12 years ago
										 | 					null,
 | ||
|  | 					['href' => $this->generateFileName($type->name)]
 | ||
| 
											12 years ago
										 | 				) . $postfix;
 | ||
| 
											12 years ago
										 | 			}
 | ||
|  | 		}
 | ||
|  | 		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)
 | ||
| 
											12 years ago
										 | 	{
 | ||
|  | 		if ($title === null) {
 | ||
| 
											12 years ago
										 | 			$title = $subject->name;
 | ||
|  | 		}
 | ||
|  | 		if (($type = $this->context->getType($subject->definedBy)) === null) {
 | ||
|  | 			return $subject->name;
 | ||
|  | 		} else {
 | ||
| 
											12 years ago
										 | 			$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)
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		// TODO use phpdoc Context for this
 | ||
| 
											12 years ago
										 | 		if ($context === null) {
 | ||
|  | 			return '';
 | ||
|  | 		}
 | ||
|  | 		if ($context instanceof TypeDoc) {
 | ||
|  | 			return $context->namespace;
 | ||
| 
											12 years ago
										 | 		}
 | ||
| 
											12 years ago
										 | 		if ($context->hasProperty('definedBy')) {
 | ||
|  | 			$type = $this->context->getType($context);
 | ||
|  | 			if ($type !== null) {
 | ||
|  | 				return $type->namespace;
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return '';
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @param ClassDoc $class
 | ||
|  | 	 * @return string
 | ||
|  | 	 */
 | ||
|  | 	public function renderInheritance($class)
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		$parents[] = $this->typeLink($class);
 | ||
| 
											12 years ago
										 | 		while ($class->parentClass !== null) {
 | ||
|  | 			if(isset($this->context->classes[$class->parentClass])) {
 | ||
|  | 				$class = $this->context->classes[$class->parentClass];
 | ||
| 
											12 years ago
										 | 				$parents[] = $this->typeLink($class);
 | ||
| 
											12 years ago
										 | 			} else {
 | ||
|  | 				$parents[] = $class->parentClass; // TODO link to php.net
 | ||
|  | 				break;
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return implode(" »\n",$parents);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * @param array $names
 | ||
| 
											12 years ago
										 | 	 * @return string
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function renderInterfaces($names)
 | ||
| 
											12 years ago
										 | 	{
 | ||
|  | 		$interfaces = [];
 | ||
| 
											12 years ago
										 | 		sort($names, SORT_STRING);
 | ||
|  | 		foreach($names as $interface) {
 | ||
| 
											12 years ago
										 | 			if(isset($this->context->interfaces[$interface])) {
 | ||
| 
											12 years ago
										 | 				$interfaces[] = $this->typeLink($this->context->interfaces[$interface]);
 | ||
| 
											12 years ago
										 | 			} else {
 | ||
|  | 				$interfaces[] = $interface; // TODO link to php.net
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return implode(', ',$interfaces);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											12 years ago
										 | 	 * @param array $names
 | ||
| 
											12 years ago
										 | 	 * @return string
 | ||
|  | 	 */
 | ||
| 
											12 years ago
										 | 	public function renderTraits($names)
 | ||
| 
											12 years ago
										 | 	{
 | ||
|  | 		$traits = [];
 | ||
| 
											12 years ago
										 | 		sort($names, SORT_STRING);
 | ||
|  | 		foreach($names as $trait) {
 | ||
| 
											12 years ago
										 | 			if(isset($this->context->traits[$trait])) {
 | ||
| 
											12 years ago
										 | 				$traits[] = $this->typeLink($this->context->traits[$trait]);
 | ||
| 
											12 years ago
										 | 			} else {
 | ||
|  | 				$traits[] = $trait; // TODO link to php.net
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return implode(', ',$traits);
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @param array $names
 | ||
|  | 	 * @return string
 | ||
|  | 	 */
 | ||
|  | 	public function renderClasses($names)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$classes = [];
 | ||
|  | 		sort($names, SORT_STRING);
 | ||
|  | 		foreach($names as $class) {
 | ||
|  | 			if(isset($this->context->classes[$class])) {
 | ||
|  | 				$classes[] = $this->typeLink($this->context->classes[$class]);
 | ||
| 
											12 years ago
										 | 			} else {
 | ||
| 
											12 years ago
										 | 				$classes[] = $class; // TODO link to php.net
 | ||
| 
											12 years ago
										 | 			}
 | ||
|  | 		}
 | ||
| 
											12 years ago
										 | 		return implode(', ',$classes);
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 	/**
 | ||
|  | 	 * @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 : '');
 | ||
|  | 		}
 | ||
|  | 
 | ||
|  | 		return ($method->isReturnByReference ? '<b>&</b>' : '')
 | ||
|  | 			. ($method->returnType === null ? 'void' : $this->typeLink($method->returnTypes))
 | ||
|  | 			. ' ' . $method->name . '( '
 | ||
|  | 			. implode(', ', $params)
 | ||
|  | 			. ' )';
 | ||
|  | 	}
 | ||
| 
											12 years ago
										 | 
 | ||
| 
											12 years ago
										 | 	protected function generateFileName($typeName)
 | ||
| 
											12 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		return strtolower(str_replace('\\', '_', $typeName)) . '.html';
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * 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)
 | ||
|  | 	{
 | ||
| 
											12 years ago
										 | 		return Yii::getAlias('@yii/apidoc/templates/html/views/' . $view);
 | ||
| 
											12 years ago
										 | 	}
 | ||
|  | }
 |