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

8
namespace yii\apidoc\commands;
9 10

use phpDocumentor\Reflection\FileReflector;
11 12
use TokenReflection\ReflectionFile;
use yii\apidoc\templates\BaseRenderer;
13
use yii\console\Controller;
14
use yii\helpers\ArrayHelper;
15 16
use yii\helpers\Console;
use yii\helpers\FileHelper;
17 18
use yii\apidoc\components\OfflineRenderer;
use yii\apidoc\models\Context;
19 20 21
use Yii;

/**
22
 * Command to render API Documentation files
23 24 25 26
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
27
class RenderController extends Controller
28
{
29 30 31
	public $template = 'bootstrap';

	public $guide;
32

33 34 35 36 37 38 39
	/**
	 * Renders API documentation files
	 * @param array $sourceDirs
	 * @param string $targetDir
	 * @return int
	 */
	public function actionIndex(array $sourceDirs, $targetDir)
40
	{
41
		$targetDir = rtrim(Yii::getAlias($targetDir), '\\/');
42 43 44
		if (is_dir($targetDir) && !$this->confirm('TargetDirectory already exists. Overwrite?')) {
			return 2;
		}
45 46 47
		if (!is_dir($targetDir)) {
			mkdir($targetDir);
		}
48

49
		$renderer = $this->findRenderer();
50 51 52
		if ($renderer === false) {
			return 1;
		}
53
		$renderer->targetDir = $targetDir;
54 55
		if ($this->guide !== null && $renderer->hasProperty('guideUrl')) {
			$renderer->guideUrl = './';
56
			$renderer->markDownFiles = $this->findMarkdownFiles($this->guide, ['README.md']);
57
		}
58

59
		$this->stdout('Searching files to process... ');
60
		$files = [];
AlexGx committed
61 62
		foreach ($sourceDirs as $source) {
			foreach ($this->findFiles($source) as $fileName) {
63 64 65 66
				$files[$fileName] = $fileName;
			}
		}

67 68
		$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);

69 70 71 72 73
		if (empty($files)) {
			$this->stderr('Error: No php files found to process.' . PHP_EOL);
			return 1;
		}

74 75 76 77 78 79 80 81 82
		$context = new Context();

		$cacheFile = $targetDir . '/cache/' . md5(serialize($files)) . '.tmp';
		if (file_exists($cacheFile)) {
			$this->stdout('Loading processed data from cache... ');
			$context = unserialize(file_get_contents($cacheFile));
			$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);

			$this->stdout('Checking for updated files... ');
AlexGx committed
83
			foreach ($context->files as $file => $sha) {
84 85 86 87 88 89 90
				if (sha1_file($file) === $sha) {
					unset($files[$file]);
				}
			}
			$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
		}

91
		$fileCount = count($files);
92
		$this->stdout($fileCount . ' file' . ($fileCount == 1 ? '' : 's') . ' to update.' . PHP_EOL);
93 94
		Console::startProgress(0, $fileCount, 'Processing files... ', false);
		$done = 0;
AlexGx committed
95
		foreach ($files as $file) {
96 97 98 99 100 101
			$context->addFile($file);
			Console::updateProgress(++$done, $fileCount);
		}
		Console::endProgress(true);
		$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);

102 103 104 105 106 107
		// save processed data to cache
		if (!is_dir(dirname($cacheFile))) {
			mkdir(dirname($cacheFile));
		}
		file_put_contents($cacheFile, serialize($context));

108 109 110 111
		$this->stdout('Updating cross references and backlinks... ');
		$context->updateReferences();
		$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);

112
		// render models
113 114
		$renderer->renderApi($context, $this);

115
		ArrayHelper::multisort($context->errors, 'file');
116 117
		print_r($context->errors);

118 119
		// render guide if specified
		if ($this->guide !== null) {
120
			$renderer->renderMarkdownFiles($this);
Carsten Brandt committed
121 122 123 124

			$this->stdout('Publishing images...');
			FileHelper::copyDirectory(rtrim($this->guide, '/\\') . '/images', $targetDir . '/images');
			$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
125
		}
126 127
	}

128 129 130 131 132
	/**
	 * @return BaseRenderer
	 */
	protected function findRenderer()
	{
133 134
		$rendererClass = 'yii\\apidoc\\templates\\' . $this->template . '\\Renderer';
		if (!class_exists($rendererClass)) {
135
			$this->stderr('Renderer not found.' . PHP_EOL);
136
			return false;
137 138 139 140
		}
		return new $rendererClass();
	}

141
	protected function findFiles($path, $except = ['vendor/', 'tests/'])
142 143 144 145 146 147 148 149 150 151 152 153
	{
		$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;
			},
154
			'only' => ['*.php'],
155
			'except' => $except,
156 157 158
		];
		return FileHelper::findFiles($path, $options);
	}
159

160 161 162 163
	protected function findMarkdownFiles($path, $except = [])
	{
		$path = FileHelper::normalizePath($path);
		$options = [
164
			'only' => ['*.md'],
165 166 167 168 169
			'except' => $except,
		];
		return FileHelper::findFiles($path, $options);
	}

170 171 172 173 174
	/**
	 * @inheritdoc
	 */
	public function globalOptions()
	{
175
		return array_merge(parent::globalOptions(), ['template', 'guide']);
176
	}
177
}