MadelineProto/src/danog/MadelineProto/PhpDoc/GenericDoc.php

149 lines
4.1 KiB
PHP
Raw Normal View History

2020-10-08 21:35:32 +02:00
<?php
namespace danog\MadelineProto\PhpDoc;
2020-10-13 10:03:35 +02:00
use danog\MadelineProto\PhpDocBuilder;
2020-10-08 21:35:32 +02:00
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\Author;
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
2020-10-13 10:03:35 +02:00
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Fqsen;
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Url;
2020-10-08 21:35:32 +02:00
use phpDocumentor\Reflection\DocBlock\Tags\See;
abstract class GenericDoc
{
2020-10-13 10:03:35 +02:00
/**
* Builder instance.
*/
protected PhpDocBuilder $builder;
/**
* Name.
*/
protected string $name;
2020-10-08 21:35:32 +02:00
/**
* Title.
*/
2020-10-13 10:03:35 +02:00
protected string $title;
2020-10-08 21:35:32 +02:00
/**
* Description.
*/
2020-10-13 10:03:35 +02:00
protected Description $description;
2020-10-08 21:35:32 +02:00
/**
* See also array.
*
2020-10-13 10:03:35 +02:00
* @var array<string, See>
2020-10-08 21:35:32 +02:00
*/
2020-10-13 10:03:35 +02:00
protected array $seeAlso = [];
2020-10-08 21:35:32 +02:00
/**
2020-10-13 21:54:25 +02:00
* Authors.
*
* @var Author[]
2020-10-08 21:35:32 +02:00
*/
2020-10-13 21:54:25 +02:00
protected array $authors;
2020-10-08 21:35:32 +02:00
/**
* Ignore this class.
*/
protected bool $ignore = false;
2020-10-13 10:03:35 +02:00
/**
* Class name.
*/
protected string $className;
/**
* Class namespace.
*/
protected string $namespace;
public function __construct(DocBlock $doc, $reflectionClass)
2020-10-08 21:35:32 +02:00
{
2020-10-13 10:03:35 +02:00
$this->className = $reflectionClass->getName();
$this->namespace = \str_replace('/', '\\', \dirname(\str_replace('\\', '/', $this->className)));
2020-10-08 21:35:32 +02:00
$this->title = $doc->getSummary();
$this->description = $doc->getDescription();
$tags = $doc->getTags();
2020-10-13 21:54:25 +02:00
$this->authors = $this->builder->getAuthors();
2020-10-08 21:35:32 +02:00
foreach ($tags as $tag) {
if ($tag instanceof Author) {
2020-10-13 21:54:25 +02:00
$this->authors []= $tag;
2020-10-08 21:35:32 +02:00
}
if ($tag instanceof Deprecated) {
$this->ignore = true;
break;
}
if ($tag instanceof Generic && $tag->getName() === 'internal') {
$this->ignore = true;
break;
}
if ($tag instanceof See) {
2020-10-13 10:03:35 +02:00
$this->seeAlso[$tag->getReference()->__toString()] = $tag;
2020-10-08 21:35:32 +02:00
}
}
}
2020-10-13 10:03:35 +02:00
public function seeAlso(): string
{
$seeAlso = '';
foreach ($this->seeAlso as $see) {
$ref = $see->getReference();
if ($ref instanceof Fqsen) {
$ref = (string) $ref;
$ref = $this->builder->resolveTypeAlias($this->className, $ref);
$refExpl = \explode("\\", $ref);
$name = \array_pop($refExpl);
$namespace = \explode('/', $this->namespace);
$count = \count($refExpl);
foreach ($namespace as $k => $name) {
if (isset($refExpl[$k]) && $refExpl[$k] === $name) {
$count--;
} else {
break;
}
}
$ref = \str_repeat('../', $count)
.\implode('/', \array_slice($refExpl, $count))
."/$name.md";
$desc = $see->getDescription() ?: $ref;
$seeAlso .= "* [$desc]($ref)\n";
}
if ($ref instanceof Url) {
$desc = $see->getDescription() ?: $ref;
$seeAlso .= "* [$desc]($ref)\n";
}
}
if ($seeAlso) {
$seeAlso = "\n#### See also: \n$seeAlso\n\n";
}
return $seeAlso;
}
2020-10-08 21:35:32 +02:00
public function format(): string
{
2020-10-13 21:54:25 +02:00
$authors = '';
foreach ($this->authors as $author) {
$authors .= "> Author: $author \n";
}
2020-10-13 10:03:35 +02:00
$seeAlso = $this->seeAlso();
return <<<EOF
---
title: $this->name: $this->title
description: $this->description
image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png
---
# `$this->name`: $this->title
[Back to API index](index.md)
2020-10-13 21:54:25 +02:00
$authors
2020-10-13 10:03:35 +02:00
$this->description
$seeAlso
EOF;
2020-10-08 21:35:32 +02:00
}
public function shouldIgnore(): bool
{
return $this->ignore;
}
}