ApiRenderer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\bootstrap;
  8. use Yii;
  9. use yii\apidoc\helpers\ApiIndexer;
  10. use yii\helpers\Console;
  11. use yii\helpers\FileHelper;
  12. /**
  13. *
  14. * @author Carsten Brandt <mail@cebe.cc>
  15. * @since 2.0
  16. */
  17. class ApiRenderer extends \yii\apidoc\templates\html\ApiRenderer
  18. {
  19. use RendererTrait;
  20. public $layout = '@yii/apidoc/templates/bootstrap/layouts/api.php';
  21. public $indexView = '@yii/apidoc/templates/bootstrap/views/index.php';
  22. /**
  23. * @inheritdoc
  24. */
  25. public function render($context, $targetDir)
  26. {
  27. $types = array_merge($context->classes, $context->interfaces, $context->traits);
  28. $extTypes = [];
  29. foreach ($this->extensions as $k => $ext) {
  30. $extType = $this->filterTypes($types, $ext);
  31. if (empty($extType)) {
  32. unset($this->extensions[$k]);
  33. continue;
  34. }
  35. $extTypes[$ext] = $extType;
  36. }
  37. // render view files
  38. parent::render($context, $targetDir);
  39. if ($this->controller !== null) {
  40. $this->controller->stdout('generating extension index files...');
  41. }
  42. foreach ($extTypes as $ext => $extType) {
  43. $readme = @file_get_contents("https://raw.github.com/yiisoft/yii2-$ext/master/README.md");
  44. $indexFileContent = $this->renderWithLayout($this->indexView, [
  45. 'docContext' => $context,
  46. 'types' => $extType,
  47. 'readme' => $readme ?: null,
  48. ]);
  49. file_put_contents($targetDir . "/ext-{$ext}-index.html", $indexFileContent);
  50. }
  51. $yiiTypes = $this->filterTypes($types, 'yii');
  52. if (empty($yiiTypes)) {
  53. // $readme = @file_get_contents("https://raw.github.com/yiisoft/yii2-framework/master/README.md");
  54. $indexFileContent = $this->renderWithLayout($this->indexView, [
  55. 'docContext' => $context,
  56. 'types' => $this->filterTypes($types, 'app'),
  57. 'readme' => null,
  58. ]);
  59. } else {
  60. $readme = @file_get_contents("https://raw.github.com/yiisoft/yii2-framework/master/README.md");
  61. $indexFileContent = $this->renderWithLayout($this->indexView, [
  62. 'docContext' => $context,
  63. 'types' => $yiiTypes,
  64. 'readme' => $readme ?: null,
  65. ]);
  66. }
  67. file_put_contents($targetDir . '/index.html', $indexFileContent);
  68. if ($this->controller !== null) {
  69. $this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
  70. $this->controller->stdout('generating search index...');
  71. }
  72. $indexer = new ApiIndexer();
  73. $indexer->indexFiles(FileHelper::findFiles($targetDir, ['only' => ['*.html']]), $targetDir);
  74. $js = $indexer->exportJs();
  75. file_put_contents($targetDir . '/jssearch.index.js', $js);
  76. if ($this->controller !== null) {
  77. $this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
  78. }
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. public function getSourceUrl($type, $line = null)
  84. {
  85. if (is_string($type)) {
  86. $type = $this->apiContext->getType($type);
  87. }
  88. switch ($this->getTypeCategory($type)) {
  89. case 'yii':
  90. $baseUrl = 'https://github.com/yiisoft/yii2/blob/master';
  91. if ($type->name == 'Yii') {
  92. $url = "$baseUrl/framework/Yii.php";
  93. } else {
  94. $url = "$baseUrl/framework/" . str_replace('\\', '/', substr($type->name, 4)) . '.php';
  95. }
  96. break;
  97. case 'app':
  98. return null;
  99. default:
  100. $parts = explode('\\', substr($type->name, 4));
  101. $ext = $parts[0];
  102. unset($parts[0]);
  103. $url = "https://github.com/yiisoft/yii2-$ext/blob/master/" . implode('/', $parts) . '.php';
  104. break;
  105. }
  106. if ($line === null)
  107. return $url;
  108. else
  109. return $url . '#L' . $line;
  110. }
  111. }