ApiRenderer.php 3.98 KB
Newer Older
1 2 3 4 5 6 7 8
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\apidoc\templates\bootstrap;
Carsten Brandt committed
9

10
use Yii;
11
use yii\apidoc\helpers\ApiIndexer;
12
use yii\helpers\Console;
13
use yii\helpers\FileHelper;
14 15 16 17 18 19 20 21

/**
 *
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
class ApiRenderer extends \yii\apidoc\templates\html\ApiRenderer
{
22
    use RendererTrait;
23

24 25
    public $layout = '@yii/apidoc/templates/bootstrap/layouts/api.php';
    public $indexView = '@yii/apidoc/templates/bootstrap/views/index.php';
26

27 28 29 30 31 32
    /**
     * @inheritdoc
     */
    public function render($context, $targetDir)
    {
        $types = array_merge($context->classes, $context->interfaces, $context->traits);
33

34 35 36 37 38 39 40 41 42
        $extTypes = [];
        foreach ($this->extensions as $k => $ext) {
            $extType = $this->filterTypes($types, $ext);
            if (empty($extType)) {
                unset($this->extensions[$k]);
                continue;
            }
            $extTypes[$ext] = $extType;
        }
43

44 45
        // render view files
        parent::render($context, $targetDir);
46

47 48 49
        if ($this->controller !== null) {
            $this->controller->stdout('generating extension index files...');
        }
50

51 52 53 54 55 56 57 58 59
        foreach ($extTypes as $ext => $extType) {
            $readme = @file_get_contents("https://raw.github.com/yiisoft/yii2-$ext/master/README.md");
            $indexFileContent = $this->renderWithLayout($this->indexView, [
                'docContext' => $context,
                'types' => $extType,
                'readme' => $readme ?: null,
            ]);
            file_put_contents($targetDir . "/ext-{$ext}-index.html", $indexFileContent);
        }
60

61 62
        $yiiTypes = $this->filterTypes($types, 'yii');
        if (empty($yiiTypes)) {
63
//			$readme = @file_get_contents("https://raw.github.com/yiisoft/yii2-framework/master/README.md");
64 65 66 67 68 69 70 71 72 73 74 75 76 77
            $indexFileContent = $this->renderWithLayout($this->indexView, [
                'docContext' => $context,
                'types' => $this->filterTypes($types, 'app'),
                'readme' => null,
            ]);
        } else {
            $readme = @file_get_contents("https://raw.github.com/yiisoft/yii2-framework/master/README.md");
            $indexFileContent = $this->renderWithLayout($this->indexView, [
                'docContext' => $context,
                'types' => $yiiTypes,
                'readme' => $readme ?: null,
            ]);
        }
        file_put_contents($targetDir . '/index.html', $indexFileContent);
78

79 80 81 82 83 84 85 86 87 88
        if ($this->controller !== null) {
            $this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
            $this->controller->stdout('generating search index...');
        }

        $indexer = new ApiIndexer();
        $indexer->indexFiles(FileHelper::findFiles($targetDir, ['only' => ['*.html']]), $targetDir);
        $js = $indexer->exportJs();
        file_put_contents($targetDir . '/jssearch.index.js', $js);

89 90 91 92
        if ($this->controller !== null) {
            $this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
        }
    }
93

94 95 96 97 98
    public function getSourceUrl($type, $line = null)
    {
        if (is_string($type)) {
            $type = $this->apiContext->getType($type);
        }
99

100 101 102
        $baseUrl = 'https://github.com/yiisoft/yii2/blob/master';
        switch ($this->getTypeCategory($type)) {
            case 'yii':
Carsten Brandt committed
103 104 105 106 107
                if ($type->name == 'Yii') {
                    $url = '/framework/Yii.php';
                } else {
                    $url = '/framework/' . str_replace('\\', '/', substr($type->name, 4)) . '.php';
                }
108 109 110 111 112 113 114
                break;
            case 'app':
                return null;
            default:
                $url = '/extensions/' . str_replace('\\', '/', substr($type->name, 4)) . '.php';
                break;
        }
115

116 117 118 119 120
        if ($line === null)
            return $baseUrl . $url;
        else
            return $baseUrl . $url . '#L' . $line;
    }
Luciano Baraglia committed
121
}