Commit ca6bfdbb by Carsten Brandt

final changes for apidoc refactoring

parent acde64c8
...@@ -9,6 +9,7 @@ namespace yii\apidoc\commands; ...@@ -9,6 +9,7 @@ namespace yii\apidoc\commands;
use yii\apidoc\components\BaseController; use yii\apidoc\components\BaseController;
use yii\apidoc\models\Context;
use yii\apidoc\renderers\ApiRenderer; use yii\apidoc\renderers\ApiRenderer;
use yii\apidoc\renderers\BaseRenderer; use yii\apidoc\renderers\BaseRenderer;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
...@@ -42,6 +43,8 @@ class ApiController extends BaseController ...@@ -42,6 +43,8 @@ class ApiController extends BaseController
return 1; return 1;
} }
$renderer->apiUrl = './';
// setup reference to guide // setup reference to guide
if ($this->guide !== null) { if ($this->guide !== null) {
$guideUrl = $this->guide; $guideUrl = $this->guide;
...@@ -54,7 +57,7 @@ class ApiController extends BaseController ...@@ -54,7 +57,7 @@ class ApiController extends BaseController
$renderer->guideUrl = $guideUrl; $renderer->guideUrl = $guideUrl;
$renderer->guideReferences = []; $renderer->guideReferences = [];
foreach(explode("\n", file_get_contents($referenceFile)) as $reference) { foreach(explode("\n", file_get_contents($referenceFile)) as $reference) {
$renderer->guideReferences[BaseRenderer::GUIDE_PREFIX . $reference] = $renderer->generateGuideUrl($reference); $renderer->guideReferences[BaseRenderer::GUIDE_PREFIX . $reference]['url'] = $renderer->generateGuideUrl($reference);
} }
} }
...@@ -65,6 +68,12 @@ class ApiController extends BaseController ...@@ -65,6 +68,12 @@ class ApiController extends BaseController
$context = $this->loadContext($targetDir); $context = $this->loadContext($targetDir);
$this->stdout('Checking for updated files... '); $this->stdout('Checking for updated files... ');
foreach($context->files as $file => $sha) { foreach($context->files as $file => $sha) {
if (!file_exists($file)) {
$this->stdout('At least one file has been removed. Rebuilding the context...');
$context = new Context();
$files = $this->searchFiles($sourceDirs);
break;
}
if (sha1_file($file) === $sha) { if (sha1_file($file) === $sha) {
unset($files[$file]); unset($files[$file]);
} }
...@@ -92,8 +101,11 @@ class ApiController extends BaseController ...@@ -92,8 +101,11 @@ class ApiController extends BaseController
$renderer->controller = $this; $renderer->controller = $this;
$renderer->render($context, $targetDir); $renderer->render($context, $targetDir);
if (!empty($context->errors)) {
ArrayHelper::multisort($context->errors, 'file'); ArrayHelper::multisort($context->errors, 'file');
print_r($context->errors); file_put_contents($targetDir . '/errors.txt', print_r($context->errors, true));
$this->stdout(count($context->errors) . " errors have been logged to $targetDir/errors.txt\n", Console::FG_RED, Console::BOLD);
}
} }
protected function findFiles($path, $except = ['vendor/', 'tests/']) protected function findFiles($path, $except = ['vendor/', 'tests/'])
......
...@@ -47,10 +47,13 @@ class GuideController extends BaseController ...@@ -47,10 +47,13 @@ class GuideController extends BaseController
if ($this->apiDocs !== null) { if ($this->apiDocs !== null) {
$renderer->apiUrl = $this->apiDocs; $renderer->apiUrl = $this->apiDocs;
$renderer->apiContext = $this->loadContext($this->apiDocs); $renderer->apiContext = $this->loadContext($this->apiDocs);
$this->updateContext($renderer->apiContext); // TODO autodetect API docs in same folder } elseif (file_exists($targetDir . '/cache/apidoc.data')) {
$renderer->apiUrl = './';
$renderer->apiContext = $this->loadContext($targetDir);
} else { } else {
$renderer->apiContext = new Context(); $renderer->apiContext = new Context();
} }
$this->updateContext($renderer->apiContext);
// search for files to process // search for files to process
$files = $this->searchFiles($sourceDirs); $files = $this->searchFiles($sourceDirs);
...@@ -93,4 +96,12 @@ class GuideController extends BaseController ...@@ -93,4 +96,12 @@ class GuideController extends BaseController
} }
return new $rendererClass(); return new $rendererClass();
} }
/**
* @inheritdoc
*/
public function globalOptions()
{
return array_merge(parent::globalOptions(), ['apiDocs']);
}
} }
\ No newline at end of file
...@@ -35,7 +35,7 @@ abstract class BaseController extends Controller ...@@ -35,7 +35,7 @@ abstract class BaseController extends Controller
{ {
$target = rtrim(Yii::getAlias($target), '\\/'); $target = rtrim(Yii::getAlias($target), '\\/');
if (file_exists($target)) { if (file_exists($target)) {
if (is_dir($target) && !$this->confirm('TargetDirectory already exists. Overwrite?')) { if (is_dir($target) && !$this->confirm('TargetDirectory already exists. Overwrite?', true)) {
$this->stderr('User aborted.' . PHP_EOL); $this->stderr('User aborted.' . PHP_EOL);
return false; return false;
} }
......
...@@ -27,29 +27,11 @@ use yii\helpers\Html; ...@@ -27,29 +27,11 @@ use yii\helpers\Html;
*/ */
class ApiRenderer extends \yii\apidoc\templates\html\ApiRenderer class ApiRenderer extends \yii\apidoc\templates\html\ApiRenderer
{ {
use RendererTrait;
public $layout = '@yii/apidoc/templates/bootstrap/layouts/api.php'; public $layout = '@yii/apidoc/templates/bootstrap/layouts/api.php';
public $indexView = '@yii/apidoc/templates/bootstrap/views/index.php'; public $indexView = '@yii/apidoc/templates/bootstrap/views/index.php';
public $extensions = [
'apidoc',
'authclient',
'bootstrap',
'codeception',
'composer',
'debug',
'elasticsearch',
'faker',
'gii',
'imagine',
'jui',
'mongodb',
'redis',
'smarty',
'sphinx',
'swiftmailer',
'twig',
];
/** /**
* @inheritdoc * @inheritdoc
*/ */
...@@ -106,56 +88,4 @@ class ApiRenderer extends \yii\apidoc\templates\html\ApiRenderer ...@@ -106,56 +88,4 @@ class ApiRenderer extends \yii\apidoc\templates\html\ApiRenderer
$this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN); $this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
} }
} }
public function getNavTypes($type, $types)
{
if ($type === null) {
return $types;
}
$extensions = $this->extensions;
$navClasses = 'app';
if (isset($type)) {
if ($type->name == 'Yii') {
$navClasses = 'yii';
} elseif (strncmp($type->name, 'yii\\', 4) == 0) {
$subName = substr($type->name, 4);
if (($pos = strpos($subName, '\\')) !== false) {
$subNamespace = substr($subName, 0, $pos);
if (in_array($subNamespace, $extensions)) {
$navClasses = $subNamespace;
} else {
$navClasses = 'yii';
}
}
}
}
return $this->filterTypes($types, $navClasses);
}
protected function filterTypes($types, $navClasses)
{
switch ($navClasses)
{
case 'app':
$types = array_filter($types, function($val) {
return strncmp($val->name, 'yii\\', 4) !== 0;
});
break;
case 'yii':
$self = $this;
$types = array_filter($types, function($val) use ($self) {
if (strlen($val->name) < 5) {
return false;
}
$subName = substr($val->name, 4, strpos($val->name, '\\', 5) - 4);
return strncmp($val->name, 'yii\\', 4) === 0 && !in_array($subName, $self->extensions);
});
break;
default:
$types = array_filter($types, function($val) use ($navClasses) {
return strncmp($val->name, "yii\\$navClasses\\", strlen("yii\\$navClasses\\")) === 0;
});
}
return $types;
}
} }
\ No newline at end of file
...@@ -27,79 +27,27 @@ use yii\helpers\Html; ...@@ -27,79 +27,27 @@ use yii\helpers\Html;
*/ */
class GuideRenderer extends \yii\apidoc\templates\html\GuideRenderer class GuideRenderer extends \yii\apidoc\templates\html\GuideRenderer
{ {
use RendererTrait;
public $layout = '@yii/apidoc/templates/bootstrap/layouts/guide.php'; public $layout = '@yii/apidoc/templates/bootstrap/layouts/guide.php';
public $extensions = [ /**
'apidoc', * @inheritDoc
'authclient', */
'bootstrap', public function render($files, $targetDir)
'codeception', {
'composer', $types = array_merge($this->apiContext->classes, $this->apiContext->interfaces, $this->apiContext->traits);
'debug',
'elasticsearch', $extTypes = [];
'faker', foreach($this->extensions as $k => $ext) {
'gii', $extType = $this->filterTypes($types, $ext);
'imagine', if (empty($extType)) {
'jui', unset($this->extensions[$k]);
'mongodb', continue;
'redis', }
'smarty', $extTypes[$ext] = $extType;
'sphinx', }
'swiftmailer',
'twig',
];
//
// /**
// * Renders a given [[Context]].
// *
// * @param Controller $controller the apidoc controller instance. Can be used to control output.
// */
// public function renderMarkdownFiles($controller)
// {
// $files = $this->markDownFiles;
// $dir = Yii::getAlias($this->targetDir);
// if (!is_dir($dir)) {
// mkdir($dir, 0777, true);
// }
//
// ApiMarkdown::$renderer = $this;
//
// $fileCount = count($files) + 1;
// Console::startProgress(0, $fileCount, 'Rendering markdown files: ', false);
// $done = 0;
// $fileData = [];
// $headlines = [];
// foreach($files as $file) {
// $fileData[$file] = file_get_contents($file);
// if (basename($file) == 'index.md') {
// continue; // to not add index file to nav
// }
// if (preg_match("/^(.*)\n=+/", $fileData[$file], $matches)) {
// $headlines[$file] = $matches[1];
// } else {
// $headlines[$file] = basename($file);
// }
// }
//
// foreach($fileData as $file => $content) {
// $output = ApiMarkdown::process($content); // TODO generate links to yiiframework.com by default
// $output = $this->fixMarkdownLinks($output);
// if ($this->guideLayout !== false) {
// $params = [
// 'headlines' => $headlines,
// 'currentFile' => $file,
// 'content' => $output,
// ];
// $output = $this->getView()->renderFile($this->guideLayout, $params, $this);
// }
// $fileName = $this->generateGuideFileName($file);
// file_put_contents($dir . '/' . $fileName, $output);
// Console::updateProgress(++$done, $fileCount);
// }
// Console::updateProgress(++$done, $fileCount);
// Console::endProgress(true);
// $controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
// }
//
parent::render($files, $targetDir);
}
} }
\ No newline at end of file
<?php
/**
* @author Carsten Brandt <mail@cebe.cc>
*/
namespace yii\apidoc\templates\bootstrap;
trait RendererTrait
{
public $extensions = [
'apidoc',
'authclient',
'bootstrap',
'codeception',
'composer',
'debug',
'elasticsearch',
'faker',
'gii',
'imagine',
'jui',
'mongodb',
'redis',
'smarty',
'sphinx',
'swiftmailer',
'twig',
];
public function getNavTypes($type, $types)
{
if ($type === null) {
return $types;
}
$extensions = $this->extensions;
$navClasses = 'app';
if (isset($type)) {
if ($type->name == 'Yii') {
$navClasses = 'yii';
} elseif (strncmp($type->name, 'yii\\', 4) == 0) {
$navClasses = 'yii';
$subName = substr($type->name, 4);
if (($pos = strpos($subName, '\\')) !== false) {
$subNamespace = substr($subName, 0, $pos);
if (in_array($subNamespace, $extensions)) {
$navClasses = $subNamespace;
}
}
}
}
return $this->filterTypes($types, $navClasses);
}
protected function filterTypes($types, $navClasses)
{
switch ($navClasses)
{
case 'app':
$types = array_filter($types, function($val) {
return strncmp($val->name, 'yii\\', 4) !== 0;
});
break;
case 'yii':
$self = $this;
$types = array_filter($types, function($val) use ($self) {
if ($val->name == 'Yii') {
return true;
}
if (strlen($val->name) < 5) {
return false;
}
$subName = substr($val->name, 4, strpos($val->name, '\\', 5) - 4);
return strncmp($val->name, 'yii\\', 4) === 0 && !in_array($subName, $self->extensions);
});
break;
default:
$types = array_filter($types, function($val) use ($navClasses) {
return strncmp($val->name, "yii\\$navClasses\\", strlen("yii\\$navClasses\\")) === 0;
});
}
return $types;
}
}
\ No newline at end of file
...@@ -109,7 +109,7 @@ class SideNavWidget extends \yii\bootstrap\Widget ...@@ -109,7 +109,7 @@ class SideNavWidget extends \yii\bootstrap\Widget
unset($items[$i]); unset($items[$i]);
continue; continue;
} }
$items[] = $this->renderItem($item); $items[] = $this->renderItem($item, count($this->items) !== 1);
} }
return Html::tag('div', implode("\n", $items), $this->options); return Html::tag('div', implode("\n", $items), $this->options);
...@@ -118,10 +118,12 @@ class SideNavWidget extends \yii\bootstrap\Widget ...@@ -118,10 +118,12 @@ class SideNavWidget extends \yii\bootstrap\Widget
/** /**
* Renders a widget's item. * Renders a widget's item.
* @param string|array $item the item to render. * @param string|array $item the item to render.
* @param bool $collapsed whether to collapse item if not active
* @throws \yii\base\InvalidConfigException
* @return string the rendering result. * @return string the rendering result.
* @throws InvalidConfigException * @throws InvalidConfigException if label is not defined
*/ */
public function renderItem($item) public function renderItem($item, $collapsed = true)
{ {
if (is_string($item)) { if (is_string($item)) {
return $item; return $item;
...@@ -163,7 +165,7 @@ class SideNavWidget extends \yii\bootstrap\Widget ...@@ -163,7 +165,7 @@ class SideNavWidget extends \yii\bootstrap\Widget
'encodeLabels' => $this->encodeLabels, 'encodeLabels' => $this->encodeLabels,
'view' => $this->getView(), 'view' => $this->getView(),
'options' => [ 'options' => [
'class' => "submenu panel-collapse collapse" . ($active ? ' in' : '') 'class' => "submenu panel-collapse collapse" . ($active || !$collapsed ? ' in' : '')
] ]
]); ]);
} }
......
...@@ -57,7 +57,7 @@ $this->beginPage(); ...@@ -57,7 +57,7 @@ $this->beginPage();
foreach($this->context->extensions as $ext) { foreach($this->context->extensions as $ext) {
$extItems[] = [ $extItems[] = [
'label' => $ext, 'label' => $ext,
'url' => "./ext_{$ext}_index.html", 'url' => "./ext-{$ext}-index.html",
]; ];
} }
$nav[] = ['label' => 'Extensions', 'items' => $extItems]; $nav[] = ['label' => 'Extensions', 'items' => $extItems];
......
...@@ -155,8 +155,6 @@ abstract class GuideRenderer extends BaseGuideRenderer ...@@ -155,8 +155,6 @@ abstract class GuideRenderer extends BaseGuideRenderer
*/ */
public function generateApiUrl($typeName) public function generateApiUrl($typeName)
{ {
// TODO: Implement generateApiUrl() method. return rtrim($this->apiUrl, '/') . '/' . strtolower(str_replace('\\', '-', $typeName)) . '.html';
return $this->apiUrl . '...';
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment