GuideController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\commands;
  8. use yii\apidoc\components\BaseController;
  9. use yii\apidoc\helpers\ApiMarkdown;
  10. use yii\apidoc\models\Context;
  11. use yii\apidoc\renderers\GuideRenderer;
  12. use yii\helpers\Console;
  13. use yii\helpers\FileHelper;
  14. use Yii;
  15. use yii\helpers\Json;
  16. /**
  17. * This command can render documentation stored as markdown files such as the yii guide
  18. * or your own applications documentation setup.
  19. *
  20. * @author Carsten Brandt <mail@cebe.cc>
  21. * @since 2.0
  22. */
  23. class GuideController extends BaseController
  24. {
  25. /**
  26. * @var string path or URL to the api docs to allow links to classes and properties/methods.
  27. */
  28. public $apiDocs;
  29. /**
  30. * @var string prefix to prepend to all output file names generated for the guide.
  31. */
  32. public $guidePrefix = 'guide-';
  33. /**
  34. * Renders API documentation files
  35. * @param array $sourceDirs
  36. * @param string $targetDir
  37. * @return int
  38. */
  39. public function actionIndex(array $sourceDirs, $targetDir)
  40. {
  41. $renderer = $this->findRenderer($this->template);
  42. $targetDir = $this->normalizeTargetDir($targetDir);
  43. if ($targetDir === false || $renderer === false) {
  44. return 1;
  45. }
  46. if ($this->pageTitle !== null) {
  47. $renderer->pageTitle = $this->pageTitle;
  48. }
  49. if ($renderer->guideUrl === null) {
  50. $renderer->guideUrl = './';
  51. }
  52. $renderer->guidePrefix = $this->guidePrefix;
  53. // setup reference to apidoc
  54. if ($this->apiDocs !== null) {
  55. $path = $this->apiDocs;
  56. if ($renderer->apiUrl === null) {
  57. $renderer->apiUrl = $path;
  58. }
  59. // use relative paths relative to targetDir
  60. if (strncmp($path, '.', 1) === 0) {
  61. $renderer->apiContext = $this->loadContext("$targetDir/$path");
  62. } else {
  63. $renderer->apiContext = $this->loadContext($path);
  64. }
  65. } elseif (file_exists($targetDir . '/cache/apidoc.data')) {
  66. if ($renderer->apiUrl === null) {
  67. $renderer->apiUrl = './';
  68. }
  69. $renderer->apiContext = $this->loadContext($targetDir);
  70. } else {
  71. $renderer->apiContext = new Context();
  72. }
  73. $this->updateContext($renderer->apiContext);
  74. // read blocktypes translations
  75. ApiMarkdown::$blockTranslations = [];
  76. foreach($sourceDirs as $dir) {
  77. if (is_file("$dir/blocktypes.json")) {
  78. ApiMarkdown::$blockTranslations = Json::decode(file_get_contents("$dir/blocktypes.json"), true);
  79. }
  80. }
  81. // search for files to process
  82. if (($files = $this->searchFiles($sourceDirs)) === false) {
  83. return 1;
  84. }
  85. $renderer->controller = $this;
  86. $renderer->render($files, $targetDir);
  87. $this->stdout('Publishing images...');
  88. foreach ($sourceDirs as $source) {
  89. $imageDir = rtrim($source, '/\\') . '/images';
  90. if (file_exists($imageDir)) {
  91. FileHelper::copyDirectory($imageDir, $targetDir . '/images');
  92. }
  93. }
  94. $this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
  95. }
  96. /**
  97. * @inheritdoc
  98. */
  99. protected function findFiles($path, $except = [])
  100. {
  101. $path = FileHelper::normalizePath($path);
  102. $options = [
  103. 'only' => ['*.md'],
  104. 'except' => $except,
  105. ];
  106. return FileHelper::findFiles($path, $options);
  107. }
  108. /**
  109. * @inheritdoc
  110. * @return GuideRenderer
  111. */
  112. protected function findRenderer($template)
  113. {
  114. // find renderer by class name
  115. if (class_exists($template)) {
  116. return new $template();
  117. }
  118. $rendererClass = 'yii\\apidoc\\templates\\' . $template . '\\GuideRenderer';
  119. if (!class_exists($rendererClass)) {
  120. $this->stderr('Renderer not found.' . PHP_EOL);
  121. return false;
  122. }
  123. return new $rendererClass();
  124. }
  125. /**
  126. * @inheritdoc
  127. */
  128. public function options($actionID)
  129. {
  130. return array_merge(parent::options($actionID), ['apiDocs', 'guidePrefix']);
  131. }
  132. }