ApiRenderer.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\apidoc\templates\html;
  8. use yii\apidoc\helpers\ApiMarkdown;
  9. use yii\apidoc\models\MethodDoc;
  10. use yii\apidoc\models\PropertyDoc;
  11. use yii\apidoc\models\ClassDoc;
  12. use yii\apidoc\models\Context;
  13. use yii\apidoc\renderers\ApiRenderer as BaseApiRenderer;
  14. use yii\base\ViewContextInterface;
  15. use yii\helpers\Console;
  16. use yii\helpers\Html;
  17. use yii\helpers\StringHelper;
  18. use yii\web\AssetManager;
  19. use yii\web\View;
  20. use Yii;
  21. /**
  22. * The base class for HTML API documentation renderers.
  23. *
  24. * @property View $view The view instance. This property is read-only.
  25. *
  26. * @author Carsten Brandt <mail@cebe.cc>
  27. * @since 2.0
  28. */
  29. class ApiRenderer extends BaseApiRenderer implements ViewContextInterface
  30. {
  31. /**
  32. * @var string path or alias of the layout file to use.
  33. */
  34. public $layout;
  35. /**
  36. * @var string path or alias of the view file to use for rendering types (classes, interfaces, traits).
  37. */
  38. public $typeView = '@yii/apidoc/templates/html/views/type.php';
  39. /**
  40. * @var string path or alias of the view file to use for rendering the index page.
  41. */
  42. public $indexView = '@yii/apidoc/templates/html/views/index.php';
  43. /**
  44. * @var View
  45. */
  46. private $_view;
  47. private $_targetDir;
  48. public function init()
  49. {
  50. parent::init();
  51. if ($this->pageTitle === null) {
  52. $this->pageTitle = 'Yii Framework 2.0 API Documentation';
  53. }
  54. }
  55. /**
  56. * @return View the view instance
  57. */
  58. public function getView()
  59. {
  60. if ($this->_view === null) {
  61. $this->_view = new View();
  62. $assetPath = Yii::getAlias($this->_targetDir) . '/assets';
  63. if (!is_dir($assetPath)) {
  64. mkdir($assetPath);
  65. }
  66. $this->_view->assetManager = new AssetManager([
  67. 'basePath' => $assetPath,
  68. 'baseUrl' => './assets',
  69. ]);
  70. }
  71. return $this->_view;
  72. }
  73. /**
  74. * Renders a given [[Context]].
  75. *
  76. * @param Context $context the api documentation context to render.
  77. * @param $targetDir
  78. */
  79. public function render($context, $targetDir)
  80. {
  81. $this->apiContext = $context;
  82. $this->_targetDir = $targetDir;
  83. $types = array_merge($context->classes, $context->interfaces, $context->traits);
  84. $typeCount = count($types) + 1;
  85. if ($this->controller !== null) {
  86. Console::startProgress(0, $typeCount, 'Rendering files: ', false);
  87. }
  88. $done = 0;
  89. foreach ($types as $type) {
  90. $fileContent = $this->renderWithLayout($this->typeView, [
  91. 'type' => $type,
  92. 'apiContext' => $context,
  93. 'types' => $types,
  94. ]);
  95. file_put_contents($targetDir . '/' . $this->generateFileName($type->name), $fileContent);
  96. if ($this->controller !== null) {
  97. Console::updateProgress(++$done, $typeCount);
  98. }
  99. }
  100. $indexFileContent = $this->renderWithLayout($this->indexView, [
  101. 'apiContext' => $context,
  102. 'types' => $types,
  103. ]);
  104. file_put_contents($targetDir . '/index.html', $indexFileContent);
  105. if ($this->controller !== null) {
  106. Console::updateProgress(++$done, $typeCount);
  107. Console::endProgress(true);
  108. $this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
  109. }
  110. }
  111. /**
  112. * Renders file applying layout
  113. * @param string $viewFile the view name
  114. * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
  115. * @return string
  116. */
  117. protected function renderWithLayout($viewFile, $params)
  118. {
  119. $output = $this->getView()->render($viewFile, $params, $this);
  120. if ($this->layout !== false) {
  121. $params['content'] = $output;
  122. return $this->getView()->renderFile($this->layout, $params, $this);
  123. } else {
  124. return $output;
  125. }
  126. }
  127. /**
  128. * @param ClassDoc $class
  129. * @return string
  130. */
  131. public function renderInheritance($class)
  132. {
  133. $parents = [];
  134. $parents[] = $this->createTypeLink($class);
  135. while ($class->parentClass !== null) {
  136. if (isset($this->apiContext->classes[$class->parentClass])) {
  137. $class = $this->apiContext->classes[$class->parentClass];
  138. $parents[] = $this->createTypeLink($class);
  139. } else {
  140. $parents[] = $this->createTypeLink($class->parentClass);
  141. break;
  142. }
  143. }
  144. return implode(" &raquo;\n", $parents);
  145. }
  146. /**
  147. * @param array $names
  148. * @return string
  149. */
  150. public function renderInterfaces($names)
  151. {
  152. $interfaces = [];
  153. sort($names, SORT_STRING);
  154. foreach ($names as $interface) {
  155. if (isset($this->apiContext->interfaces[$interface])) {
  156. $interfaces[] = $this->createTypeLink($this->apiContext->interfaces[$interface]);
  157. } else {
  158. $interfaces[] = $this->createTypeLink($interface);
  159. }
  160. }
  161. return implode(', ', $interfaces);
  162. }
  163. /**
  164. * @param array $names
  165. * @return string
  166. */
  167. public function renderTraits($names)
  168. {
  169. $traits = [];
  170. sort($names, SORT_STRING);
  171. foreach ($names as $trait) {
  172. if (isset($this->apiContext->traits[$trait])) {
  173. $traits[] = $this->createTypeLink($this->apiContext->traits[$trait]);
  174. } else {
  175. $traits[] = $this->createTypeLink($trait);
  176. }
  177. }
  178. return implode(', ', $traits);
  179. }
  180. /**
  181. * @param array $names
  182. * @return string
  183. */
  184. public function renderClasses($names)
  185. {
  186. $classes = [];
  187. sort($names, SORT_STRING);
  188. foreach ($names as $class) {
  189. if (isset($this->apiContext->classes[$class])) {
  190. $classes[] = $this->createTypeLink($this->apiContext->classes[$class]);
  191. } else {
  192. $classes[] = $this->createTypeLink($class);
  193. }
  194. }
  195. return implode(', ', $classes);
  196. }
  197. /**
  198. * @param PropertyDoc $property
  199. * @return string
  200. */
  201. public function renderPropertySignature($property, $context = null)
  202. {
  203. if ($property->getter !== null || $property->setter !== null) {
  204. $sig = [];
  205. if ($property->getter !== null) {
  206. $sig[] = $this->renderMethodSignature($property->getter, $context);
  207. }
  208. if ($property->setter !== null) {
  209. $sig[] = $this->renderMethodSignature($property->setter, $context);
  210. }
  211. return implode('<br />', $sig);
  212. }
  213. $definition = [];
  214. $definition[] = $property->visibility;
  215. if ($property->isStatic) {
  216. $definition[] = 'static';
  217. }
  218. return '<span class="signature-defs">' . implode(' ', $definition) . '</span> '
  219. . '<span class="signature-type">' . $this->createTypeLink($property->types, $context) . '</span>'
  220. . ' ' . $this->createSubjectLink($property, $property->name) . ' '
  221. . ApiMarkdown::highlight('= ' . ($property->defaultValue === null ? 'null' : $property->defaultValue), 'php');
  222. }
  223. /**
  224. * @param MethodDoc $method
  225. * @return string
  226. */
  227. public function renderMethodSignature($method, $context = null)
  228. {
  229. $params = [];
  230. foreach ($method->params as $param) {
  231. $params[] = (empty($param->typeHint) ? '' : '<span class="signature-type">' . $this->createTypeLink($param->typeHint, $context) . '</span> ')
  232. . ($param->isPassedByReference ? '<b>&</b>' : '')
  233. . ApiMarkdown::highlight(
  234. $param->name
  235. . ($param->isOptional ? ' = ' . $param->defaultValue : ''),
  236. 'php'
  237. );
  238. }
  239. $definition = [];
  240. $definition[] = $method->visibility;
  241. if ($method->isAbstract) {
  242. $definition[] = 'abstract';
  243. }
  244. if ($method->isStatic) {
  245. $definition[] = 'static';
  246. }
  247. return '<span class="signature-defs">' . implode(' ', $definition) . '</span> '
  248. . '<span class="signature-type">' . ($method->isReturnByReference ? '<b>&</b>' : '')
  249. . ($method->returnType === null ? 'void' : $this->createTypeLink($method->returnTypes, $context)) . '</span> '
  250. . '<strong>' . $this->createSubjectLink($method, $method->name) . '</strong>'
  251. . str_replace(' ', ' ', ' ( ' . implode(', ', $params) . ' )');
  252. }
  253. /**
  254. * @inheritdoc
  255. */
  256. public function generateApiUrl($typeName)
  257. {
  258. return $this->generateFileName($typeName);
  259. }
  260. /**
  261. * Generates file name for API page for a given type
  262. * @param string $typeName
  263. * @return string
  264. */
  265. protected function generateFileName($typeName)
  266. {
  267. return strtolower(str_replace('\\', '-', $typeName)) . '.html';
  268. }
  269. /**
  270. * @inheritdoc
  271. */
  272. public function getViewPath()
  273. {
  274. return Yii::getAlias('@yii/apidoc/templates/html/views');
  275. }
  276. /**
  277. * @inheritdoc
  278. */
  279. protected function generateLink($text, $href, $options = [])
  280. {
  281. $options['href'] = $href;
  282. return Html::a($text, null, $options);
  283. }
  284. /**
  285. * @inheritdoc
  286. */
  287. public function getSourceUrl($type, $line = null)
  288. {
  289. return null;
  290. }
  291. }