BaseController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\components;
  8. use yii\apidoc\renderers\BaseRenderer;
  9. use yii\console\Controller;
  10. use yii\helpers\Console;
  11. use yii\apidoc\models\Context;
  12. use Yii;
  13. /**
  14. * Command to render API Documentation files
  15. *
  16. * @author Carsten Brandt <mail@cebe.cc>
  17. * @since 2.0
  18. */
  19. abstract class BaseController extends Controller
  20. {
  21. /**
  22. * @var string template to use for rendering
  23. */
  24. public $template = 'bootstrap';
  25. /**
  26. * @var string|array files to exclude.
  27. */
  28. public $exclude;
  29. /**
  30. * @var string page title
  31. */
  32. public $pageTitle;
  33. /**
  34. * Checks that target directory is valid. Asks questions in tricky cases.
  35. * @param string $target
  36. * @return bool|string
  37. */
  38. protected function normalizeTargetDir($target)
  39. {
  40. $target = rtrim(Yii::getAlias($target), '\\/');
  41. if (file_exists($target)) {
  42. if (is_dir($target) && !$this->confirm('TargetDirectory already exists. Overwrite?', true)) {
  43. $this->stderr('User aborted.' . PHP_EOL);
  44. return false;
  45. }
  46. if (is_file($target)) {
  47. $this->stderr("Error: Target directory \"$target\" is a file!" . PHP_EOL);
  48. return false;
  49. }
  50. } else {
  51. mkdir($target, 0777, true);
  52. }
  53. return $target;
  54. }
  55. /**
  56. * Finds files to process
  57. * @param array $sourceDirs
  58. * @return array|bool list of files to process or false on failure
  59. */
  60. protected function searchFiles($sourceDirs)
  61. {
  62. $this->stdout('Searching files to process... ');
  63. $files = [];
  64. if (is_array($this->exclude)) {
  65. $exclude = $this->exclude;
  66. } elseif (is_string($this->exclude)) {
  67. $exclude = explode(',', $this->exclude);
  68. } else {
  69. $exclude = [];
  70. }
  71. foreach ($sourceDirs as $source) {
  72. foreach ($this->findFiles($source, $exclude) as $fileName) {
  73. $files[$fileName] = $fileName;
  74. }
  75. }
  76. $this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
  77. if (empty($files)) {
  78. $this->stderr('Error: No files found to process.' . PHP_EOL);
  79. return false;
  80. }
  81. return $files;
  82. }
  83. /**
  84. * Finds files
  85. *
  86. * @param string $dir directory to search files in.
  87. * @param array $except list of names to exclude from search.
  88. * @return array files found.
  89. */
  90. abstract protected function findFiles($dir, $except = []);
  91. /**
  92. * Loads context from cache
  93. * @param string $location
  94. * @return Context
  95. */
  96. protected function loadContext($location)
  97. {
  98. $context = new Context();
  99. $cacheFile = $location . '/cache/apidoc.data';
  100. $this->stdout('Loading apidoc data from cache... ');
  101. if (file_exists($cacheFile)) {
  102. $context = unserialize(file_get_contents($cacheFile));
  103. $this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
  104. } else {
  105. $this->stdout('no data available.' . PHP_EOL, Console::FG_YELLOW);
  106. }
  107. return $context;
  108. }
  109. /**
  110. * Writes context into cache file
  111. * @param Context $context
  112. * @param string $location
  113. */
  114. protected function storeContext($context, $location)
  115. {
  116. $cacheFile = $location . '/cache/apidoc.data';
  117. if (!is_dir($dir = dirname($cacheFile))) {
  118. mkdir($dir, 0777, true);
  119. }
  120. file_put_contents($cacheFile, serialize($context));
  121. }
  122. /**
  123. * @param Context $context
  124. */
  125. protected function updateContext($context)
  126. {
  127. $this->stdout('Updating cross references and backlinks... ');
  128. $context->updateReferences();
  129. $this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
  130. }
  131. /**
  132. * @param string $template
  133. * @return BaseRenderer
  134. */
  135. abstract protected function findRenderer($template);
  136. /**
  137. * @inheritdoc
  138. */
  139. public function options($actionID)
  140. {
  141. return array_merge(parent::options($actionID), ['template', 'exclude', 'pageTitle']);
  142. }
  143. }