BaseController.php 2.92 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\components;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

use yii\apidoc\renderers\BaseRenderer;
use yii\console\Controller;
use yii\helpers\Console;
use yii\apidoc\models\Context;
use Yii;

/**
 * Command to render API Documentation files
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
abstract class BaseController extends Controller
{
	/**
	 * @var string template to use for rendering
	 */
	public $template = 'bootstrap';
	/**
29
	 * @var string|array files to exclude.
30
	 */
31
	public $exclude;
32 33 34 35 36 37


	protected function normalizeTargetDir($target)
	{
		$target = rtrim(Yii::getAlias($target), '\\/');
		if (file_exists($target)) {
38
			if (is_dir($target) && !$this->confirm('TargetDirectory already exists. Overwrite?', true)) {
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
				$this->stderr('User aborted.' . PHP_EOL);
				return false;
			}
			if (is_file($target)) {
				$this->stderr("Error: Target directory \"$target\" is a file!" . PHP_EOL);
				return false;
			}
		} else {
			mkdir($target, 0777, true);
		}
		return $target;
	}

	protected function searchFiles($sourceDirs)
	{
		$this->stdout('Searching files to process... ');
		$files = [];
56 57 58 59 60 61 62 63 64

		if (is_array($this->exclude)) {
			$exclude = $this->exclude;
		} elseif (is_string($this->exclude)) {
			$exclude = explode(',', $this->exclude);
		} else {
			$exclude = [];
		}

Luciano Baraglia committed
65 66
		foreach ($sourceDirs as $source) {
			foreach ($this->findFiles($source, $exclude) as $fileName) {
67 68 69 70 71 72 73
				$files[$fileName] = $fileName;
			}
		}
		$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);

		if (empty($files)) {
			$this->stderr('Error: No files found to process.' . PHP_EOL);
74
			return false;
75 76 77 78
		}
		return $files;
	}

79
	protected abstract function findFiles($dir, $except = []);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

	protected function loadContext($location)
	{
		$context = new Context();

		$cacheFile = $location . '/cache/apidoc.data';
		$this->stdout('Loading apidoc data from cache... ');
		if (file_exists($cacheFile)) {
			$context = unserialize(file_get_contents($cacheFile));
			$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
		} else {
			$this->stdout('no data available.' . PHP_EOL, Console::FG_YELLOW);
		}
		return $context;
	}

	protected function storeContext($context, $location)
	{
		$cacheFile = $location . '/cache/apidoc.data';
		if (!is_dir($dir = dirname($cacheFile))) {
			mkdir($dir, 0777, true);
		}
		file_put_contents($cacheFile, serialize($context));
	}

	/**
	 * @param Context $context
	 */
	protected function updateContext($context)
	{
		$this->stdout('Updating cross references and backlinks... ');
		$context->updateReferences();
		$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
	}

	/**
Carsten Brandt committed
116
	 * @param string $template
117 118 119 120 121 122 123
	 * @return BaseRenderer
	 */
	protected abstract function findRenderer($template);

	/**
	 * @inheritdoc
	 */
124
	public function options($id)
125
	{
126
		return array_merge(parent::options($id), ['template', 'exclude']);
127 128
	}
}