ApiMarkdownTrait.php 4.64 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 9 10 11 12 13 14 15 16
 */

namespace yii\apidoc\helpers;

use phpDocumentor\Reflection\DocBlock\Type\Collection;
use yii\apidoc\models\MethodDoc;
use yii\apidoc\models\TypeDoc;

/**
 * Class ApiMarkdownTrait
 *
17
 * @property TypeDoc $renderingContext
18 19 20
 */
trait ApiMarkdownTrait
{
21 22 23
    /**
     * @marker [[
     */
24 25
    protected function parseApiLinks($text)
    {
26
        $context = $this->renderingContext;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

        if (preg_match('/^\[\[([\w\d\\\\\(\):$]+)(\|[^\]]*)?\]\]/', $text, $matches)) {

            $offset = strlen($matches[0]);

            $object = $matches[1];
            $title = (empty($matches[2]) || $matches[2] == '|') ? null : substr($matches[2], 1);

            if (($pos = strpos($object, '::')) !== false) {
                $typeName = substr($object, 0, $pos);
                $subjectName = substr($object, $pos + 2);
                if ($context !== null) {
                    // Collection resolves relative types
                    $typeName = (new Collection([$typeName], $context->phpDocContext))->__toString();
                }
42
                /** @var $type TypeDoc */
43 44 45 46 47 48 49 50
                $type = static::$renderer->apiContext->getType($typeName);
                if ($type === null) {
                    static::$renderer->apiContext->errors[] = [
                        'file' => ($context !== null) ? $context->sourceFile : null,
                        'message' => 'broken link to ' . $typeName . '::' . $subjectName . (($context !== null) ? ' in ' . $context->name : ''),
                    ];

                    return [
51
                        ['brokenApiLink', '<span style="background: #f00;">' . $typeName . '::' . $subjectName . '</span>'],
52 53 54 55 56 57 58 59 60 61 62 63
                        $offset
                    ];
                } else {
                    if (($subject = $type->findSubject($subjectName)) !== null) {
                        if ($title === null) {
                            $title = $type->name . '::' . $subject->name;
                            if ($subject instanceof MethodDoc) {
                                $title .= '()';
                            }
                        }

                        return [
64
                            ['apiLink', static::$renderer->createSubjectLink($subject, $title)],
65 66 67 68 69 70 71 72 73
                            $offset
                        ];
                    } else {
                        static::$renderer->apiContext->errors[] = [
                            'file' => ($context !== null) ? $context->sourceFile : null,
                            'message' => 'broken link to ' . $type->name . '::' . $subjectName . (($context !== null) ? ' in ' . $context->name : ''),
                        ];

                        return [
74
                            ['brokenApiLink', '<span style="background: #ff0;">' . $type->name . '</span><span style="background: #f00;">::' . $subjectName . '</span>'],
75 76 77 78 79 80
                            $offset
                        ];
                    }
                }
            } elseif ($context !== null && ($subject = $context->findSubject($object)) !== null) {
                return [
81
                    ['apiLink', static::$renderer->createSubjectLink($subject, $title)],
82 83 84 85 86 87 88 89 90 91
                    $offset
                ];
            }

            if ($context !== null) {
                // Collection resolves relative types
                $object = (new Collection([$object], $context->phpDocContext))->__toString();
            }
            if (($type = static::$renderer->apiContext->getType($object)) !== null) {
                return [
92
                    ['apiLink', static::$renderer->createTypeLink($type, null, $title)],
93 94 95 96
                    $offset
                ];
            } elseif (strpos($typeLink = static::$renderer->createTypeLink($object, null, $title), '<a href') !== false) {
                return [
97
                    ['apiLink', $typeLink],
98 99 100 101 102 103 104 105 106
                    $offset
                ];
            }
            static::$renderer->apiContext->errors[] = [
                'file' => ($context !== null) ? $context->sourceFile : null,
                'message' => 'broken link to ' . $object . (($context !== null) ? ' in ' . $context->name : ''),
            ];

            return [
107
                ['brokenApiLink', '<span style="background: #f00;">' . $object . '</span>'],
108 109 110 111
                $offset
            ];
        }

112 113 114 115 116 117
        return [['text', '[['], 2];
    }

    protected function renderApiLink($block)
    {
        return $block[1];
118
    }
119 120 121 122 123

    protected function renderBrokenApiLink($block)
    {
        return $block[1];
    }
124
}