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

8
namespace yii\apidoc\templates\html;
9

Carsten Brandt committed
10
use yii\apidoc\helpers\ApiMarkdown;
11 12
use yii\apidoc\models\MethodDoc;
use yii\apidoc\models\PropertyDoc;
13 14
use yii\apidoc\models\ClassDoc;
use yii\apidoc\models\Context;
15
use yii\apidoc\renderers\ApiRenderer as BaseApiRenderer;
16 17 18
use yii\base\ViewContextInterface;
use yii\helpers\Console;
use yii\helpers\Html;
19 20
use yii\web\AssetManager;
use yii\web\View;
21
use Yii;
22

23 24 25
/**
 * The base class for HTML API documentation renderers.
 *
26 27
 * @property View $view The view instance. This property is read-only.
 *
28 29 30
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
31
class ApiRenderer extends BaseApiRenderer implements ViewContextInterface
32
{
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    /**
     * @var string string to use as the title of the generated page.
     */
    public $pageTitle;
    /**
     * @var string path or alias of the layout file to use.
     */
    public $layout;
    /**
     * @var string path or alias of the view file to use for rendering types (classes, interfaces, traits).
     */
    public $typeView = '@yii/apidoc/templates/html/views/type.php';
    /**
     * @var string path or alias of the view file to use for rendering the index page.
     */
    public $indexView = '@yii/apidoc/templates/html/views/index.php';
49

50 51 52 53 54 55
    /**
     * @var View
     */
    private $_view;
    private $_targetDir;

56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    public function init()
    {
        parent::init();

        if ($this->pageTitle === null) {
            $this->pageTitle = 'Yii Framework 2.0 API Documentation'; // TODO guess page title
        }
    }

    /**
     * @return View the view instance
     */
    public function getView()
    {
        if ($this->_view === null) {
            $this->_view = new View();
            $assetPath = Yii::getAlias($this->_targetDir) . '/assets';
            if (!is_dir($assetPath)) {
                mkdir($assetPath);
            }
            $this->_view->assetManager = new AssetManager([
                'basePath' => $assetPath,
                'baseUrl' => './assets',
            ]);
        }

        return $this->_view;
    }

    /**
     * Renders a given [[Context]].
     *
     * @param Context $context the api documentation context to render.
     * @param $targetDir
     */
    public function render($context, $targetDir)
    {
        $this->apiContext = $context;
        $this->_targetDir = $targetDir;

        $types = array_merge($context->classes, $context->interfaces, $context->traits);
        $typeCount = count($types) + 1;

        if ($this->controller !== null) {
            Console::startProgress(0, $typeCount, 'Rendering files: ', false);
        }
        $done = 0;
        foreach ($types as $type) {
            $fileContent = $this->renderWithLayout($this->typeView, [
                'type' => $type,
                'apiContext' => $context,
                'types' => $types,
            ]);
            file_put_contents($targetDir . '/' . $this->generateFileName($type->name), $fileContent);

            if ($this->controller !== null) {
                Console::updateProgress(++$done, $typeCount);
            }
        }

        $indexFileContent = $this->renderWithLayout($this->indexView, [
            'apiContext' => $context,
            'types' => $types,
        ]);
        file_put_contents($targetDir . '/index.html', $indexFileContent);

        if ($this->controller !== null) {
            Console::updateProgress(++$done, $typeCount);
            Console::endProgress(true);
            $this->controller->stdout('done.' . PHP_EOL, Console::FG_GREEN);
        }
    }

    protected function renderWithLayout($viewFile, $params)
    {
        $output = $this->getView()->render($viewFile, $params, $this);
        if ($this->layout !== false) {
            $params['content'] = $output;

            return $this->getView()->renderFile($this->layout, $params, $this);
        } else {
            return $output;
        }
    }

    /**
143
     * @param ClassDoc $class
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
     * @return string
     */
    public function renderInheritance($class)
    {
        $parents = [];
        $parents[] = $this->createTypeLink($class);
        while ($class->parentClass !== null) {
            if (isset($this->apiContext->classes[$class->parentClass])) {
                $class = $this->apiContext->classes[$class->parentClass];
                $parents[] = $this->createTypeLink($class);
            } else {
                $parents[] = $this->createTypeLink($class->parentClass);
                break;
            }
        }

        return implode(" &raquo;\n", $parents);
    }

    /**
164
     * @param array $names
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
     * @return string
     */
    public function renderInterfaces($names)
    {
        $interfaces = [];
        sort($names, SORT_STRING);
        foreach ($names as $interface) {
            if (isset($this->apiContext->interfaces[$interface])) {
                $interfaces[] = $this->createTypeLink($this->apiContext->interfaces[$interface]);
            } else {
                $interfaces[] = $this->createTypeLink($interface);
            }
        }

        return implode(', ', $interfaces);
    }

    /**
183
     * @param array $names
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
     * @return string
     */
    public function renderTraits($names)
    {
        $traits = [];
        sort($names, SORT_STRING);
        foreach ($names as $trait) {
            if (isset($this->apiContext->traits[$trait])) {
                $traits[] = $this->createTypeLink($this->apiContext->traits[$trait]);
            } else {
                $traits[] = $this->createTypeLink($trait);
            }
        }

        return implode(', ', $traits);
    }

    /**
202
     * @param array $names
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
     * @return string
     */
    public function renderClasses($names)
    {
        $classes = [];
        sort($names, SORT_STRING);
        foreach ($names as $class) {
            if (isset($this->apiContext->classes[$class])) {
                $classes[] = $this->createTypeLink($this->apiContext->classes[$class]);
            } else {
                $classes[] = $this->createTypeLink($class);
            }
        }

        return implode(', ', $classes);
    }

    /**
221
     * @param PropertyDoc $property
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
     * @return string
     */
    public function renderPropertySignature($property)
    {
        if ($property->getter !== null || $property->setter !== null) {
            $sig = [];
            if ($property->getter !== null) {
                $sig[] = $this->renderMethodSignature($property->getter);
            }
            if ($property->setter !== null) {
                $sig[] = $this->renderMethodSignature($property->setter);
            }

            return implode('<br />', $sig);
        }

        return $this->createTypeLink($property->types) . ' ' . $this->createSubjectLink($property, $property->name) . ' '
                . ApiMarkdown::highlight('= ' . ($property->defaultValue === null ? 'null' : $property->defaultValue), 'php');
    }

    /**
243
     * @param MethodDoc $method
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
     * @return string
     */
    public function renderMethodSignature($method)
    {
        $params = [];
        foreach ($method->params as $param) {
            $params[] = (empty($param->typeHint) ? '' : $param->typeHint . ' ')
                . ($param->isPassedByReference ? '<b>&</b>' : '')
                . $param->name
                . ($param->isOptional ? ' = ' . $param->defaultValue : '');
        }

        return ($method->isReturnByReference ? '<b>&</b>' : '')
            . ($method->returnType === null ? 'void' : $this->createTypeLink($method->returnTypes))
            . ' <strong>' . $this->createSubjectLink($method, $method->name) . '</strong>'
            . ApiMarkdown::highlight(str_replace('  ', ' ', '( ' . implode(', ', $params) . ' )'), 'php');
    }

    public function generateApiUrl($typeName)
    {
        return $this->generateFileName($typeName);
    }

    protected function generateFileName($typeName)
    {
        return strtolower(str_replace('\\', '-', $typeName)) . '.html';
    }

    /**
273
     * @inheritdoc
274
     */
275
    public function getViewPath()
276
    {
277
        return Yii::getAlias('@yii/apidoc/templates/html/views');
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    }

    /**
     * @inheritdoc
     */
    protected function generateLink($text, $href, $options = [])
    {
        $options['href'] = $href;

        return Html::a($text, null, $options);
    }

    public function getSourceUrl($type)
    {
        return null;
    }
Luciano Baraglia committed
294
}