Use getLogger in APILoop

This commit is contained in:
Daniil Gentili 2020-10-13 21:54:25 +02:00
parent def6199437
commit 37d3f94af2
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
9 changed files with 253 additions and 57 deletions

View File

@ -83,4 +83,14 @@ abstract class EventHandler extends InternalDoc
{
return [];
}
/**
* Get API instance.
*
* @return MTProto
*/
public function getAPI(): MTProto
{
return $this->API;
}
}

View File

@ -18,6 +18,7 @@
namespace danog\MadelineProto\Loop;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\InternalDoc;
/**
@ -41,6 +42,6 @@ trait APILoop
public function __construct(InternalDoc $API)
{
$this->API = $API;
$this->setLogger($API->getLogger());
$this->setLogger($API instanceof EventHandler ? $API->getAPI()->getLogger() : $API->logger);
}
}

View File

@ -173,7 +173,6 @@ trait Files
$start = \microtime(true);
while ($part_num < $part_total_num) {
$resa = $callable($part_num);
\var_dump($resa);
$writePromise = Tools::call($this->methodCallAsyncWrite($method, $resa, ['heavy' => true, 'file' => true, 'datacenter' => &$datacenter]));
if (!$seekable) {
yield $writePromise;

View File

@ -15,7 +15,7 @@ class ClassDoc extends GenericDoc
/**
* Properties.
*
* @var array<string, PropertyDoc>
* @var array<string, array>
*/
private array $properties = [];
/**
@ -24,6 +24,10 @@ class ClassDoc extends GenericDoc
* @var array<string, MethodDoc>
*/
private array $methods = [];
/**
* Constants.
*/
private array $constants = [];
public function __construct(PhpDocBuilder $builder, ReflectionClass $reflectionClass)
{
$this->builder = $builder;
@ -41,12 +45,10 @@ class ClassDoc extends GenericDoc
$tags = $doc->getTags();
foreach ($tags as $tag) {
if ($tag instanceof Property) {
$this->properties[$tag->getVariableName()] = new PropertyDoc(
$this->builder,
$tag->getName(),
$this->properties[$tag->getVariableName()] = [
$tag->getType(),
$tag->getDescription()
);
];
}
if ($tag instanceof InvalidTag && $tag->getName() === 'property') {
[$type, $description] = \explode(" $", $tag->render(), 2);
@ -54,15 +56,12 @@ class ClassDoc extends GenericDoc
[$varName, $description] = \explode(" ", $description, 2);
$type = \str_replace('@property ', '', $type);
$description ??= '';
$this->properties[$varName] = new PropertyDoc(
$this->builder,
$varName,
$this->properties[$varName] = [
$type,
$description
);
];
}
}
$constants = [];
foreach ($reflectionClass->getConstants() as $key => $value) {
$refl = new ReflectionClassConstant($reflectionClass->getName(), $key);
if (!$refl->isPublic()) {
@ -84,7 +83,7 @@ class ClassDoc extends GenericDoc
}
}
$description = \trim($description);
$constants[$key] = [
$this->constants[$key] = [
$value,
$description
];
@ -99,32 +98,39 @@ class ClassDoc extends GenericDoc
}
$this->methods = \array_filter($this->methods, fn (MethodDoc $doc): bool => !$doc->shouldIgnore());
//$this->properties = \array_filter($this->properties, fn (PropertyDoc $doc): bool => !$doc->shouldIgnore());
}
public function format(): string
{
$init = parent::format();
$methods = '';
$properties = '';
if ($this->constants) {
$init .= "\n";
$init .= "## Constants\n";
foreach ($this->constants as $name => [, $description]) {
$description = \trim($description);
$description = \str_replace("\n", "\n ", $description);
$init .= "* {$this->className}::$name: $description\n";
$init .= "\n";
}
}
if ($this->methods) {
$init .= "\n";
$init .= "## Method list:\n";
foreach ($this->methods as $method) {
$init .= "* ".$method->getSignature()."\n";
$init .= "* `".$method->getSignature()."`\n";
}
$init .= "\n";
$init .= "## Methods:\n$methods\n";
$init .= "## Methods:\n";
foreach ($this->methods as $method) {
$init .= $method->format();
$init .= "\n";
}
}
if ($properties) {
$init .= "## Properties:\n$properties\n";
/*foreach ($this->properties as $property) {
$init .= $property->format();
$init .= "\n";
}*/
if ($this->properties) {
$init .= "## Properties\n";
foreach ($this->properties as $name => [$type, $description]) {
$init .= "* `\$$name`: `$type` $description";
}
}
return $init;
}

View File

@ -37,9 +37,11 @@ abstract class GenericDoc
*/
protected array $seeAlso = [];
/**
* Author.
* Authors.
*
* @var Author[]
*/
protected Author $author;
protected array $authors;
/**
* Ignore this class.
*/
@ -60,10 +62,10 @@ abstract class GenericDoc
$this->description = $doc->getDescription();
$tags = $doc->getTags();
$this->author = new Author("Daniil Gentili", "daniil@daniil.it");
$this->authors = $this->builder->getAuthors();
foreach ($tags as $tag) {
if ($tag instanceof Author) {
$this->author = $tag;
$this->authors []= $tag;
}
if ($tag instanceof Deprecated) {
$this->ignore = true;
@ -118,6 +120,10 @@ abstract class GenericDoc
}
public function format(): string
{
$authors = '';
foreach ($this->authors as $author) {
$authors .= "> Author: $author \n";
}
$seeAlso = $this->seeAlso();
return <<<EOF
---
@ -128,7 +134,7 @@ abstract class GenericDoc
# `$this->name`: $this->title
[Back to API index](index.md)
> Author: $this->author
$authors
$this->description
$seeAlso

View File

@ -13,8 +13,10 @@ use ReflectionMethod;
class MethodDoc extends GenericDoc
{
private $return;
private Return_ $return;
private string $psalmReturn;
private array $params = [];
private array $psalmParams = [];
public function __construct(PhpDocBuilder $phpDocBuilder, ReflectionFunctionAbstract $method)
{
$this->builder = $phpDocBuilder;
@ -39,10 +41,10 @@ class MethodDoc extends GenericDoc
$tag->getType(),
$tag->getDescription()
];
} elseif ($tag instanceof Return_ && !$this->return) {
} elseif ($tag instanceof Return_ && !isset($this->return)) {
$this->return = $tag;
} elseif ($tag instanceof Generic && $tag->getName() === 'psalm-return') {
$this->return = $tag;
$this->psalmReturn = $tag;
} elseif ($tag instanceof Generic && $tag->getName() === 'psalm-param') {
[$type, $description] = \explode(" $", $tag->getDescription(), 2);
$description .= ' ';
@ -52,7 +54,7 @@ class MethodDoc extends GenericDoc
} else {
$description = new Description($description);
}
$this->params[$varName] = [
$this->psalmParams[$varName] = [
$type,
$description
];
@ -71,23 +73,45 @@ class MethodDoc extends GenericDoc
}
$sig = \trim($sig, ', ');
$sig .= ')';
if ($this->return) {
if (isset($this->return)) {
$sig .= ': ';
$sig .= $this->return;
$sig .= $this->builder->resolveTypeAlias($this->className, $this->return);
}
return $sig;
}
public function format(): string
{
$sig = '### '.$this->getSignature();
$sig = '### `'.$this->getSignature()."`";
$sig .= "\n\n";
$sig .= $this->title;
$sig .= "\n";
$sig .= $this->description;
$sig .= "\n";
if ($this->return && $this->return->getDescription() && $this->return->getDescription()->render()) {
if ($this->psalmParams || $this->params) {
$sig .= "\nParameters:\n";
foreach ($this->params as $name => [$type, $description]) {
if ($type) {
$type = $this->builder->resolveTypeAlias($this->className, $type);
}
$sig .= "* `\$$name`: `$type` $description \n";
if (isset($this->psalmParams[$name])) {
[$psalmType] = $this->psalmParams[$name];
$psalmType = \trim(\str_replace("\n", "\n ", $psalmType));
$sig .= " Full type:\n";
$sig .= " ```\n";
$sig .= " $psalmType\n";
$sig .= " ```\n";
}
}
$sig .= "\n";
}
if (isset($this->return) && $this->return->getDescription() && $this->return->getDescription()->render()) {
$sig .= "\nReturn value: ".$this->return->getDescription()."\n";
}
if (isset($this->psalmReturn)) {
$sig .= "\nFully typed return value:\n```\n".$this->psalmReturn."\n```";
}
$sig .= $this->seeAlso();
$sig .= "\n";
return $sig;

View File

@ -1,16 +0,0 @@
<?php
namespace danog\MadelineProto\PhpDoc;
use danog\MadelineProto\PhpDocBuilder;
class PropertyDoc
{
public function __construct(PhpDocBuilder $phpDocBuilder, string $name, $type, $description)
{
}
public function __toString()
{
}
}

View File

@ -21,6 +21,7 @@ namespace danog\MadelineProto;
use danog\ClassFinder\ClassFinder;
use danog\MadelineProto\PhpDoc\ClassDoc;
use danog\MadelineProto\PhpDoc\FunctionDoc;
use phpDocumentor\Reflection\DocBlock\Tags\Author;
use phpDocumentor\Reflection\DocBlockFactory;
use ReflectionClass;
use ReflectionFunction;
@ -39,6 +40,10 @@ class PhpDocBuilder
* Docblock factory.
*/
private DocBlockFactory $factory;
/**
* Authors.
*/
private array $authors = [];
/**
* Classes/interfaces/traits to ignore.
*
@ -59,12 +64,12 @@ class PhpDocBuilder
/**
* Create docblock builder.
*
* @param string $namespace Namespace
* @param string $namespace Namespace (defaults to package namespace)
* @param int $mode Finder mode, an OR-selection of ClassFinder::ALLOW_*
*
* @return self
*/
public static function fromNamespace(string $namespace, int $mode = ClassFinder::ALLOW_ALL): self
public static function fromNamespace(string $namespace = '', int $mode = ClassFinder::ALLOW_ALL): self
{
return new self($namespace, $mode);
}
@ -79,6 +84,26 @@ class PhpDocBuilder
$this->factory = DocBlockFactory::createInstance();
$this->namespace = $namespace;
$this->mode = $mode;
$appRoot = new \danog\ClassFinder\AppConfig;
$appRoot = $appRoot->getAppRoot();
$appRoot .= "/composer.json";
$json = \json_decode(\file_get_contents($appRoot), true);
$authors = $json['authors'] ?? [];
foreach ($authors as $author) {
$this->authors []= new Author($author['name'], $author['email']);
}
if (!$this->namespace) {
$namespaces = array_keys($json['autoload']['psr-4']);
$this->namespace = $namespaces[0];
foreach ($namespaces as $namespace) {
if (strlen($namespace) && strlen($namespace) < strlen($this->namespace)) {
$this->namespace = $namespace;
}
}
}
}
/**
* Set filter to ignore certain classes.
@ -120,7 +145,7 @@ class PhpDocBuilder
$this->addTypeAliases($class);
}
foreach ($classes as $class) {
if ($this->ignore && ($this->ignore)($class)) {
if ($this->ignore && $this->shouldIgnore($class)) {
continue;
}
$class = \function_exists($class)
@ -233,6 +258,30 @@ class PhpDocBuilder
*/
public function shouldIgnore(string $class): bool
{
return ($this->ignore)($class);
return !($this->ignore)($class);
}
/**
* Get authors.
*
* @return Author[]
*/
public function getAuthors(): array
{
return $this->authors;
}
/**
* Set authors.
*
* @param Author[] $authors Authors
*
* @return self
*/
public function setAuthors(array $authors): self
{
$this->authors = $authors;
return $this;
}
}

117
tools/phpdoc.php Normal file
View File

@ -0,0 +1,117 @@
<?php
use Amp\Sync\Internal\MutexStorage;
use danog\MadelineProto\AbstractAPIFactory;
use danog\MadelineProto\AnnotationsBuilder;
use danog\MadelineProto\APIFactory;
use danog\MadelineProto\APIWrapper;
use danog\MadelineProto\Async\AsyncConstruct;
use danog\MadelineProto\Bug74586Exception;
use danog\MadelineProto\Connection;
use danog\MadelineProto\DataCenter;
use danog\MadelineProto\DataCenterConnection;
use danog\MadelineProto\Db\DbPropertiesTrait;
use danog\MadelineProto\DocsBuilder;
use danog\MadelineProto\DoHConnector;
use danog\MadelineProto\InternalDoc;
use danog\MadelineProto\Lang;
use danog\MadelineProto\LightState;
use danog\MadelineProto\Magic;
use danog\MadelineProto\MTProtoTools\Crypt;
use danog\MadelineProto\MTProtoTools\GarbageCollector;
use danog\MadelineProto\MTProtoTools\MinDatabase;
use danog\MadelineProto\MTProtoTools\PasswordCalculator;
use danog\MadelineProto\MTProtoTools\ReferenceDatabase;
use danog\MadelineProto\MTProtoTools\UpdatesState;
use danog\MadelineProto\NothingInTheSocketException;
use danog\MadelineProto\PhpDocBuilder;
use danog\MadelineProto\RSA;
use danog\MadelineProto\Serialization;
use danog\MadelineProto\SessionPaths;
use danog\MadelineProto\SettingsAbstract;
use danog\MadelineProto\SettingsEmpty;
use danog\MadelineProto\Snitch;
use danog\MadelineProto\TL\TL;
use danog\MadelineProto\TL\TLCallback;
use danog\MadelineProto\TL\TLConstructors;
use danog\MadelineProto\TL\TLMethods;
use danog\MadelineProto\TON\ADNLConnection;
use danog\MadelineProto\TON\APIFactory as TONAPIFactory;
use danog\MadelineProto\TON\InternalDoc as TONInternalDoc;
use danog\MadelineProto\TON\Lite;
use danog\MadelineProto\VoIP;
require 'vendor/autoload.php';
$ignore = [ // Disallow list
AnnotationsBuilder::class,
APIFactory::class,
APIWrapper::class,
AbstractAPIFactory::class,
Bug74586Exception::class,
Connection::class,
ContextConnector::class,
DataCenter::class,
DataCenterConnection::class,
DoHConnector::class,
DocsBuilder::class,
InternalDoc::class,
Lang::class,
LightState::class,
Magic::class,
PhpDocBuilder::class,
RSA::class,
Serialization::class,
SessionPaths::class,
SettingsEmpty::class,
SettingsAbstract::class,
Snitch::class,
AsyncConstruct::class,
VoIP::class,
Crypt::class,
NothingInTheSocketException::class,
GarbageCollector::class,
MinDatabase::class,
PasswordCalculator::class,
ReferenceDatabase::class,
UpdatesState::class,
TL::class,
TLConstructors::class,
TLMethods::class,
TLCallback::class,
ADNLConnection::class,
TONAPIFactory::class,
TONInternalDoc::class,
Lite::class,
\ArrayIterator::class,
];
$filter = function (string $class) use ($ignore): bool {
if (\in_array($class, $ignore)) {
return false;
}
if (str_starts_with($class, 'danog\\MadelineProto\\Ipc')
|| str_starts_with($class, 'danog\\MadelineProto\\Loop\\Update')
|| str_starts_with($class, 'danog\\MadelineProto\\Loop\\Connection')
|| str_starts_with($class, 'danog\\MadelineProto\\MTProto\\')
|| str_starts_with($class, 'danog\\MadelineProto\\MTProtoSession\\')
|| str_starts_with($class, 'danog\\MadelineProto\\PhpDoc\\')
|| str_starts_with($class, 'danog\\MadelineProto\\Db\\NullCache')) {
return false;
}
if ($class === DbPropertiesTrait::class) {
return true;
}
$class = new ReflectionClass($class);
return !$class->isTrait();
};
PhpDocBuilder::fromNamespace()
->setFilter($filter)
->setOutput(__DIR__.'/../docs/docs/PHP/')
->run();