yii\apidoc\renderers\BaseRenderer::createTypeLink PHP Method

    public function createTypeLink($types, $context = null, $title = null, $options = [])
    {
        if (!is_array($types)) {
            $types = [$types];
        }
        if (count($types) > 1) {
            $title = null;
        }
        $links = [];
        foreach ($types as $type) {
            $postfix = '';
            if (is_string($type)) {
                if (!empty($type) && substr_compare($type, '[]', -2, 2) === 0) {
                    $postfix = '[]';
                    $type = substr($type, 0, -2);
                }
                if ($type === '$this' && $context instanceof TypeDoc) {
                    $title = '$this';
                    $type = $context;
                } elseif (($t = $this->apiContext->getType(ltrim($type, '\\'))) !== null) {
                    $type = $t;
                } elseif (!empty($type) && $type[0] !== '\\' && ($t = $this->apiContext->getType($this->resolveNamespace($context) . '\\' . ltrim($type, '\\'))) !== null) {
                    $type = $t;
                } else {
                    ltrim($type, '\\');
                }
            }
            if (is_string($type)) {
                $linkText = ltrim($type, '\\');
                if ($title !== null) {
                    $linkText = $title;
                    $title = null;
                }
                $phpTypes = ['callable', 'array', 'string', 'boolean', 'bool', 'integer', 'int', 'float', 'object', 'resource', 'null', 'false', 'true'];
                $phpTypeAliases = ['true' => 'boolean', 'false' => 'boolean', 'bool' => 'boolean', 'int' => 'integer'];
                $phpTypeDisplayAliases = ['bool' => 'boolean', 'int' => 'integer'];
                // check if it is PHP internal class
                if ((class_exists($type, false) || interface_exists($type, false) || trait_exists($type, false)) && ($reflection = new \ReflectionClass($type)) && $reflection->isInternal()) {
                    $links[] = $this->generateLink($linkText, 'http://www.php.net/class.' . strtolower(ltrim($type, '\\')), $options) . $postfix;
                } elseif (in_array($type, $phpTypes)) {
                    if (isset($phpTypeDisplayAliases[$type])) {
                        $linkText = $phpTypeDisplayAliases[$type];
                    }
                    if (isset($phpTypeAliases[$type])) {
                        $type = $phpTypeAliases[$type];
                    }
                    $links[] = $this->generateLink($linkText, 'http://www.php.net/language.types.' . strtolower(ltrim($type, '\\')), $options) . $postfix;
                } else {
                    $links[] = $type . $postfix;
                }
            } elseif ($type instanceof BaseDoc) {
                $linkText = $type->name;
                if ($title !== null) {
                    $linkText = $title;
                    $title = null;
                }
                $links[] = $this->generateLink($linkText, $this->generateApiUrl($type->name), $options) . $postfix;
            }
        }
        return implode('|', $links);
    }

Usage Example

Esempio n. 1
0
 protected function parseApiLinks($text)
 {
     $context = $this->context;
     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();
             }
             $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 ['<span style="background: #f00;">' . $typeName . '::' . $subjectName . '</span>', $offset];
             } else {
                 if (($subject = $type->findSubject($subjectName)) !== null) {
                     if ($title === null) {
                         $title = $type->name . '::' . $subject->name;
                         if ($subject instanceof MethodDoc) {
                             $title .= '()';
                         }
                     }
                     return [static::$renderer->createSubjectLink($subject, $title), $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 ['<span style="background: #ff0;">' . $type->name . '</span><span style="background: #f00;">::' . $subjectName . '</span>', $offset];
                 }
             }
         } elseif ($context !== null && ($subject = $context->findSubject($object)) !== null) {
             return [static::$renderer->createSubjectLink($subject, $title), $offset];
         }
         if ($context !== null) {
             // Collection resolves relative types
             $object = (new Collection([$object], $context->phpDocContext))->__toString();
         }
         if (($type = static::$renderer->apiContext->getType($object)) !== null) {
             return [static::$renderer->createTypeLink($type, null, $title), $offset];
         } elseif (strpos($typeLink = static::$renderer->createTypeLink($object, null, $title), '<a href') !== false) {
             return [$typeLink, $offset];
         }
         static::$renderer->apiContext->errors[] = ['file' => $context !== null ? $context->sourceFile : null, 'message' => 'broken link to ' . $object . ($context !== null ? ' in ' . $context->name : '')];
         return ['<span style="background: #f00;">' . $object . '</span>', $offset];
     }
     return ['[[', 2];
 }