ApiController.php 4.15 KB
Newer Older
1 2
<?php
/**
3 4 5
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
6 7 8 9
 */

namespace yii\apidoc\commands;

10
use yii\apidoc\components\BaseController;
11
use yii\apidoc\models\Context;
12
use yii\apidoc\renderers\ApiRenderer;
13
use yii\apidoc\renderers\BaseRenderer;
14 15 16 17
use yii\helpers\ArrayHelper;
use yii\helpers\Console;
use yii\helpers\FileHelper;

18
/**
19
 * Generate class API documentation.
20
 *
21 22
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
23
 */
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
class ApiController extends BaseController
{
	/**
	 * @var string url to where the guide files are located
	 */
	public $guide;

	// TODO add force update option

	/**
	 * Renders API documentation files
	 * @param array $sourceDirs
	 * @param string $targetDir
	 * @return int
	 */
	public function actionIndex(array $sourceDirs, $targetDir)
	{
		$renderer = $this->findRenderer($this->template);
		$targetDir = $this->normalizeTargetDir($targetDir);
		if ($targetDir === false || $renderer === false) {
			return 1;
		}

47 48
		$renderer->apiUrl = './';

49 50
		// setup reference to guide
		if ($this->guide !== null) {
51 52 53 54 55 56 57 58 59
			$guideUrl = $this->guide;
			$referenceFile = $guideUrl . '/' . BaseRenderer::GUIDE_PREFIX . 'references.txt';
		} else {
			$guideUrl = './';
			$referenceFile = $targetDir . '/' . BaseRenderer::GUIDE_PREFIX . 'references.txt';
		}
		if (file_exists($referenceFile)) {
			$renderer->guideUrl = $guideUrl;
			$renderer->guideReferences = [];
Luciano Baraglia committed
60
			foreach (explode("\n", file_get_contents($referenceFile)) as $reference) {
61
				$renderer->guideReferences[BaseRenderer::GUIDE_PREFIX . $reference]['url'] = $renderer->generateGuideUrl($reference);
62
			}
63 64 65
		}

		// search for files to process
66 67 68
		if (($files = $this->searchFiles($sourceDirs)) === false) {
			return 1;
		}
69 70 71 72

		// load context from cache
		$context = $this->loadContext($targetDir);
		$this->stdout('Checking for updated files... ');
Luciano Baraglia committed
73
		foreach ($context->files as $file => $sha) {
74 75 76
			if (!file_exists($file)) {
				$this->stdout('At least one file has been removed. Rebuilding the context...');
				$context = new Context();
77 78 79
				if (($files = $this->searchFiles($sourceDirs)) === false) {
					return 1;
				}
80 81
				break;
			}
82 83 84 85 86 87 88 89 90 91 92
			if (sha1_file($file) === $sha) {
				unset($files[$file]);
			}
		}
		$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);

		// process files
		$fileCount = count($files);
		$this->stdout($fileCount . ' file' . ($fileCount == 1 ? '' : 's') . ' to update.' . PHP_EOL);
		Console::startProgress(0, $fileCount, 'Processing files... ', false);
		$done = 0;
Luciano Baraglia committed
93
		foreach ($files as $file) {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
			$context->addFile($file);
			Console::updateProgress(++$done, $fileCount);
		}
		Console::endProgress(true);
		$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);

		// save processed data to cache
		$this->storeContext($context, $targetDir);

		$this->updateContext($context);

		// render models
		$renderer->controller = $this;
		$renderer->render($context, $targetDir);

109 110 111 112 113
		if (!empty($context->errors)) {
			ArrayHelper::multisort($context->errors, 'file');
			file_put_contents($targetDir . '/errors.txt', print_r($context->errors, true));
			$this->stdout(count($context->errors) . " errors have been logged to $targetDir/errors.txt\n", Console::FG_RED, Console::BOLD);
		}
114 115
	}

116 117 118
	/**
	 * @inheritdoc
	 */
119
	protected function findFiles($path, $except = [])
120
	{
121 122 123
		if (empty($except)) {
			$except = ['vendor/', 'tests/'];
		}
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
		$path = FileHelper::normalizePath($path);
		$options = [
			'filter' => function ($path) {
					if (is_file($path)) {
						$file = basename($path);
						if ($file[0] < 'A' || $file[0] > 'Z') {
							return false;
						}
					}
					return null;
				},
			'only' => ['*.php'],
			'except' => $except,
		];
		return FileHelper::findFiles($path, $options);
	}

	/**
142
	 * @inheritdoc
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	 * @return ApiRenderer
	 */
	protected function findRenderer($template)
	{
		$rendererClass = 'yii\\apidoc\\templates\\' . $template . '\\ApiRenderer';
		if (!class_exists($rendererClass)) {
			$this->stderr('Renderer not found.' . PHP_EOL);
			return false;
		}
		return new $rendererClass();
	}

	/**
	 * @inheritdoc
	 */
158
	public function options($id)
159
	{
160
		return array_merge(parent::options($id), ['template', 'guide']);
161
	}
162
}