From 5982544678787119b13a4f699a928dfc5cf2b68d Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sun, 5 Jan 2014 22:11:54 +0100 Subject: [PATCH] finished render command, added cli script --- extensions/yii/apidoc/apidoc | 49 +++++++++ extensions/yii/apidoc/apidoc.bat | 20 ++++ .../yii/apidoc/commands/PhpdocController.php | 88 --------------- .../yii/apidoc/commands/RenderController.php | 118 +++++++++++++++++++++ extensions/yii/apidoc/composer.json | 3 +- extensions/yii/apidoc/models/Context.php | 47 +------- 6 files changed, 193 insertions(+), 132 deletions(-) create mode 100755 extensions/yii/apidoc/apidoc create mode 100644 extensions/yii/apidoc/apidoc.bat delete mode 100644 extensions/yii/apidoc/commands/PhpdocController.php create mode 100644 extensions/yii/apidoc/commands/RenderController.php diff --git a/extensions/yii/apidoc/apidoc b/extensions/yii/apidoc/apidoc new file mode 100755 index 0000000..91f7981 --- /dev/null +++ b/extensions/yii/apidoc/apidoc @@ -0,0 +1,49 @@ +#!/usr/bin/env php + 'yii2-apidoc', + 'basePath' => __DIR__, + 'enableCoreCommands' => false, + 'controllerNamespace' => 'yii\\apidoc\\commands', + 'controllerPath' => '@yii/apidoc/commands', +]); +$exitCode = $application->run(); +exit($exitCode); diff --git a/extensions/yii/apidoc/apidoc.bat b/extensions/yii/apidoc/apidoc.bat new file mode 100644 index 0000000..ae00407 --- /dev/null +++ b/extensions/yii/apidoc/apidoc.bat @@ -0,0 +1,20 @@ +@echo off + +rem ------------------------------------------------------------- +rem Yii command line bootstrap script for Windows. +rem +rem @author Qiang Xue +rem @link http://www.yiiframework.com/ +rem @copyright Copyright © 2012 Yii Software LLC +rem @license http://www.yiiframework.com/license/ +rem ------------------------------------------------------------- + +@setlocal + +set YII_PATH=%~dp0 + +if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe + +"%PHP_COMMAND%" "%YII_PATH%apidoc" %* + +@endlocal diff --git a/extensions/yii/apidoc/commands/PhpdocController.php b/extensions/yii/apidoc/commands/PhpdocController.php deleted file mode 100644 index 9236580..0000000 --- a/extensions/yii/apidoc/commands/PhpdocController.php +++ /dev/null @@ -1,88 +0,0 @@ - - * @since 2.0 - */ -class PhpdocController extends Controller -{ - public function actionIndex($targetDir) - { - echo "hi\n"; - - $targetDir = Yii::getAlias($targetDir); - if (is_dir($targetDir) && !$this->confirm('TargetDirectory already exists. Overwrite?')) { - return 2; - } - - // TODO determine files to analyze - $this->stdout('Searching files to process... '); - $files = $this->findFiles(YII_PATH); -// $files = array_slice($files, 0, 42); // TODO remove this line - $this->stdout('done.' . PHP_EOL, Console::FG_GREEN); - - $fileCount = count($files); - Console::startProgress(0, $fileCount, 'Processing files... ', false); - $context = new Context(); - $done = 0; - foreach($files as $file) { - $context->addFile($file); - Console::updateProgress(++$done, $fileCount); - } - Console::endProgress(true); - $this->stdout('done.' . PHP_EOL, Console::FG_GREEN); - - $this->stdout('Updating cross references and backlinks... '); - $context->updateReferences(); - $this->stdout('done.' . PHP_EOL, Console::FG_GREEN); - - - // TODO LATER analyze for dead links and similar stuff - - // TODO render models - $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); - } - -} \ No newline at end of file diff --git a/extensions/yii/apidoc/commands/RenderController.php b/extensions/yii/apidoc/commands/RenderController.php new file mode 100644 index 0000000..33aee50 --- /dev/null +++ b/extensions/yii/apidoc/commands/RenderController.php @@ -0,0 +1,118 @@ + + * @since 2.0 + */ +class RenderController extends Controller +{ + /** + * Renders API documentation files + * @param array $sourceDirs + * @param string $targetDir + * @return int + */ + public function actionIndex(array $sourceDirs, $targetDir) + { + $targetDir = Yii::getAlias($targetDir); + if (is_dir($targetDir) && !$this->confirm('TargetDirectory already exists. Overwrite?')) { + return 2; + } + if (!is_dir($targetDir)) { + mkdir($targetDir); + } + + $this->stdout('Searching files to process... '); + $files = []; + foreach($sourceDirs as $source) { + foreach($this->findFiles($source) as $fileName) { + $files[$fileName] = $fileName; + } + } + + $this->stdout('done.' . PHP_EOL, Console::FG_GREEN); + + $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); + } + + $fileCount = count($files); + $this->stdout($fileCount . ' file' . ($fileCount == 1 ? '' : 's') . ' to update.' . PHP_EOL); + 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); + + // save processed data to cache + if (!is_dir(dirname($cacheFile))) { + mkdir(dirname($cacheFile)); + } + file_put_contents($cacheFile, serialize($context)); + + $this->stdout('Updating cross references and backlinks... '); + $context->updateReferences(); + $this->stdout('done.' . PHP_EOL, Console::FG_GREEN); + + // render models + $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); + } +} \ No newline at end of file diff --git a/extensions/yii/apidoc/composer.json b/extensions/yii/apidoc/composer.json index 30608a1..bf1c4b5 100644 --- a/extensions/yii/apidoc/composer.json +++ b/extensions/yii/apidoc/composer.json @@ -25,5 +25,6 @@ "autoload": { "psr-0": { "yii\\apidoc\\": "" } }, - "target-dir": "yii/apidoc" + "target-dir": "yii/apidoc", + "bin": ["apidoc"] } diff --git a/extensions/yii/apidoc/models/Context.php b/extensions/yii/apidoc/models/Context.php index 89bfcfd..549c318 100644 --- a/extensions/yii/apidoc/models/Context.php +++ b/extensions/yii/apidoc/models/Context.php @@ -47,10 +47,7 @@ class Context extends Component public function addFile($fileName) { - if (isset($this->files[$fileName])) { - return; - } - $this->files[$fileName] = $fileName; + $this->files[$fileName] = sha1_file($fileName); $reflection = new FileReflector($fileName, true); $reflection->process(); @@ -58,54 +55,18 @@ class Context extends Component foreach($reflection->getClasses() as $class) { $class = new ClassDoc($class); $class->sourceFile = $fileName; - $this->addClass($class); + $this->classes[$class->name] = $class; } foreach($reflection->getInterfaces() as $interface) { $interface = new InterfaceDoc($interface); $interface->sourceFile = $fileName; - $this->addInterface($interface); + $this->interfaces[$interface->name] = $interface; } foreach($reflection->getTraits() as $trait) { $trait = new TraitDoc($trait); $trait->sourceFile = $fileName; - $this->addTrait($trait); - } - } - - /** - * @param ClassDoc $class - * @throws \yii\base\Exception when class is already part of this context - */ - public function addClass($class) - { - if (isset($this->classes[$class->name])) { - throw new Exception('Duplicate class definition: ' . $class->name . ' in file ' . $class->sourceFile . '.'); - } - $this->classes[$class->name] = $class; - } - - /** - * @param InterfaceDoc $interface - * @throws \yii\base\Exception when interface is already part of this context - */ - public function addInterface($interface) - { - if (isset($this->interfaces[$interface->name])) { - throw new Exception('Duplicate interface definition: ' . $interface->name . ' in file ' . $interface->sourceFile); - } - $this->interfaces[$interface->name] = $interface; - } - - /** - * @param TraitDoc $trait - * @throws \yii\base\Exception when trait is already part of this context - */ - public function addTrait($trait) - { - if (isset($this->traits[$trait->name])) { - throw new Exception('Duplicate trait definition: ' . $trait->name . ' in file ' . $trait->sourceFile); + $this->traits[$trait->name] = $trait; } - $this->traits[$trait->name] = $trait; } public function updateReferences()