ApiRenderer.php 8.83 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;
Carsten Brandt committed
19
use yii\helpers\StringHelper;
20 21
use yii\web\AssetManager;
use yii\web\View;
22
use Yii;
23

24 25 26
/**
 * The base class for HTML API documentation renderers.
 *
27 28
 * @property View $view The view instance. This property is read-only.
 *
29 30 31
 * @author Carsten Brandt <mail@cebe.cc>
 * @since 2.0
 */
32
class ApiRenderer extends BaseApiRenderer implements ViewContextInterface
33
{
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    /**
     * @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';
50

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

57

58 59 60 61 62
    public function init()
    {
        parent::init();

        if ($this->pageTitle === null) {
Carsten Brandt committed
63
            $this->pageTitle = 'Yii Framework 2.0 API Documentation';
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
        }
    }

    /**
     * @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);
        }
    }

131 132 133 134 135 136
    /**
     * Renders file applying layout
     * @param string $viewFile the view name
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
     * @return string
     */
137 138 139 140 141 142 143 144 145 146 147 148 149
    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;
        }
    }

    /**
150
     * @param ClassDoc $class
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
     * @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);
    }

    /**
171
     * @param array $names
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
     * @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);
    }

    /**
190
     * @param array $names
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
     * @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);
    }

    /**
209
     * @param array $names
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
     * @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);
    }

    /**
228
     * @param PropertyDoc $property
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
     * @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');
    }

    /**
250
     * @param MethodDoc $method
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
     * @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');
    }

269 270 271
    /**
     * @inheritdoc
     */
272 273 274 275 276
    public function generateApiUrl($typeName)
    {
        return $this->generateFileName($typeName);
    }

277 278 279 280 281
    /**
     * Generates file name for API page for a given type
     * @param string $typeName
     * @return string
     */
282 283 284 285 286 287
    protected function generateFileName($typeName)
    {
        return strtolower(str_replace('\\', '-', $typeName)) . '.html';
    }

    /**
288
     * @inheritdoc
289
     */
290
    public function getViewPath()
291
    {
292
        return Yii::getAlias('@yii/apidoc/templates/html/views');
293 294 295 296 297 298 299 300 301 302 303 304
    }

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

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

305 306 307 308
    /**
     * @inheritdoc
     */
    public function getSourceUrl($type, $line = null)
309 310 311
    {
        return null;
    }
Luciano Baraglia committed
312
}