You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					118 lines
				
				3.0 KiB
			
		
		
			
		
	
	
					118 lines
				
				3.0 KiB
			| 
								 
											12 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @link http://www.yiiframework.com/
							 | 
						||
| 
								 | 
							
								 * @copyright Copyright (c) 2008 Yii Software LLC
							 | 
						||
| 
								 | 
							
								 * @license http://www.yiiframework.com/license/
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								namespace yii\apidoc\commands;
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								use phpDocumentor\Reflection\FileReflector;
							 | 
						||
| 
								 | 
							
								use yii\console\Controller;
							 | 
						||
| 
								 | 
							
								use yii\helpers\Console;
							 | 
						||
| 
								 | 
							
								use yii\helpers\FileHelper;
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								use yii\apidoc\components\OfflineRenderer;
							 | 
						||
| 
								 | 
							
								use yii\apidoc\models\Context;
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								use Yii;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 * Command to render API Documentation files
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * @author Carsten Brandt <mail@cebe.cc>
							 | 
						||
| 
								 | 
							
								 * @since 2.0
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								class RenderController extends Controller
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Renders API documentation files
							 | 
						||
| 
								 | 
							
									 * @param array $sourceDirs
							 | 
						||
| 
								 | 
							
									 * @param string $targetDir
							 | 
						||
| 
								 | 
							
									 * @return int
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function actionIndex(array $sourceDirs, $targetDir)
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 | 
							
										$targetDir = Yii::getAlias($targetDir);
							 | 
						||
| 
								 | 
							
										if (is_dir($targetDir) && !$this->confirm('TargetDirectory already exists. Overwrite?')) {
							 | 
						||
| 
								 | 
							
											return 2;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										if (!is_dir($targetDir)) {
							 | 
						||
| 
								 | 
							
											mkdir($targetDir);
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
										$this->stdout('Searching files to process... ');
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$files = [];
							 | 
						||
| 
								 | 
							
										foreach($sourceDirs as $source) {
							 | 
						||
| 
								 | 
							
											foreach($this->findFiles($source) as $fileName) {
							 | 
						||
| 
								 | 
							
												$files[$fileName] = $fileName;
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$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);
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$fileCount = count($files);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->stdout($fileCount . ' file' . ($fileCount == 1 ? '' : 's') . ' to update.' . PHP_EOL);
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										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);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										// save processed data to cache
							 | 
						||
| 
								 | 
							
										if (!is_dir(dirname($cacheFile))) {
							 | 
						||
| 
								 | 
							
											mkdir(dirname($cacheFile));
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
										file_put_contents($cacheFile, serialize($context));
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$this->stdout('Updating cross references and backlinks... ');
							 | 
						||
| 
								 | 
							
										$context->updateReferences();
							 | 
						||
| 
								 | 
							
										$this->stdout('done.' . PHP_EOL, Console::FG_GREEN);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										// render models
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
										$renderer = new OfflineRenderer();
							 | 
						||
| 
								 | 
							
										$renderer->targetDir = $targetDir;
							 | 
						||
| 
								 | 
							
										$renderer->render($context, $this);
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									protected function findFiles($path, $except = [])
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										$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' => array_merge($except, [
							 | 
						||
| 
								 | 
							
												'/views/',
							 | 
						||
| 
								 | 
							
												'/requirements/',
							 | 
						||
| 
								 | 
							
												'/gii/generators/',
							 | 
						||
| 
								 | 
							
											]),
							 | 
						||
| 
								 | 
							
										];
							 | 
						||
| 
								 | 
							
										return FileHelper::findFiles($path, $options);
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |