RenderController.php 3.54 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 14 15
use yii\console\Controller;
use yii\helpers\Console;
use yii\helpers\FileHelper;
16 17
use yii\apidoc\components\OfflineRenderer;
use yii\apidoc\models\Context;
18 19 20
use Yii;

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

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

46
		$renderer = $this->findRenderer();
47 48 49
		if ($renderer === false) {
			return 1;
		}
50 51
		$renderer->targetDir = $targetDir;

52
		$this->stdout('Searching files to process... ');
53 54 55 56 57 58 59
		$files = [];
		foreach($sourceDirs as $source) {
			foreach($this->findFiles($source) as $fileName) {
				$files[$fileName] = $fileName;
			}
		}

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

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
		$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... ');
			foreach($context->files as $file => $sha) {
				if (sha1_file($file) === $sha) {
					unset($files[$file]);
				}
			}
			$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
		}

79
		$fileCount = count($files);
80
		$this->stdout($fileCount . ' file' . ($fileCount == 1 ? '' : 's') . ' to update.' . PHP_EOL);
81 82 83 84 85 86 87 88 89
		Console::startProgress(0, $fileCount, 'Processing files... ', false);
		$done = 0;
		foreach($files as $file) {
			$context->addFile($file);
			Console::updateProgress(++$done, $fileCount);
		}
		Console::endProgress(true);
		$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);

90 91 92 93 94 95
		// save processed data to cache
		if (!is_dir(dirname($cacheFile))) {
			mkdir(dirname($cacheFile));
		}
		file_put_contents($cacheFile, serialize($context));

96 97 98 99
		$this->stdout('Updating cross references and backlinks... ');
		$context->updateReferences();
		$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);

100
		// render models
101 102 103
		$renderer->render($context, $this);
	}

104 105 106 107 108
	/**
	 * @return BaseRenderer
	 */
	protected function findRenderer()
	{
109 110
		$rendererClass = 'yii\\apidoc\\templates\\' . $this->template . '\\Renderer';
		if (!class_exists($rendererClass)) {
111
			$this->stderr('Renderer not found.' . PHP_EOL);
112
			return false;
113 114 115 116
		}
		return new $rendererClass();
	}

117
	protected function findFiles($path, $except = ['/vendor/', '/tests/'])
118 119 120 121 122 123 124 125 126 127 128 129 130
	{
		$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'],
131
			'except' => $except,
132 133 134
		];
		return FileHelper::findFiles($path, $options);
	}
135 136 137 138 139 140 141 142

	/**
	 * @inheritdoc
	 */
	public function globalOptions()
	{
		return array_merge(parent::globalOptions(), ['template']);
	}
143
}