mirror of
https://github.com/Sysbot-org/tgscraper.git
synced 2025-01-07 09:25:48 +01:00
Improved code readability, added abstract classes support
This commit is contained in:
parent
cad00a6155
commit
5b38b081c3
@ -4,6 +4,7 @@
|
|||||||
namespace TgScraper\Common;
|
namespace TgScraper\Common;
|
||||||
|
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
use PHPHtmlParser\Dom;
|
use PHPHtmlParser\Dom;
|
||||||
use PHPHtmlParser\Exceptions\ChildNotFoundException;
|
use PHPHtmlParser\Exceptions\ChildNotFoundException;
|
||||||
use PHPHtmlParser\Exceptions\CircularException;
|
use PHPHtmlParser\Exceptions\CircularException;
|
||||||
@ -41,6 +42,40 @@ class SchemaExtractor
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ParentNotFoundException
|
||||||
|
* @throws ChildNotFoundException
|
||||||
|
*/
|
||||||
|
#[ArrayShape(['description' => "string", 'table' => "mixed", 'extended_by' => "array"])]
|
||||||
|
private function parseNode(Dom\Node\AbstractNode $node): ?array
|
||||||
|
{
|
||||||
|
$description = '';
|
||||||
|
$table = null;
|
||||||
|
$extendedBy = [];
|
||||||
|
$tag = '';
|
||||||
|
$sibling = $node;
|
||||||
|
while (!str_starts_with($tag, 'h')) {
|
||||||
|
$sibling = $sibling->nextSibling();
|
||||||
|
$tag = $sibling?->tag?->name();
|
||||||
|
if (empty($node->text()) or empty($tag) or $tag == 'text') {
|
||||||
|
continue;
|
||||||
|
} elseif ($tag == 'p') {
|
||||||
|
$description .= PHP_EOL . $sibling->innerHtml();
|
||||||
|
} elseif ($tag == 'ul') {
|
||||||
|
$items = $sibling->find('li');
|
||||||
|
/* @var Dom\Node\AbstractNode $item */
|
||||||
|
foreach ($items as $item) {
|
||||||
|
$extendedBy[] = $item->innerText;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
} elseif ($tag == 'table') {
|
||||||
|
$table = $sibling->find('tbody')->find('tr');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ['description' => $description, 'table' => $table, 'extended_by' => $extendedBy];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
* @throws ChildNotFoundException
|
* @throws ChildNotFoundException
|
||||||
@ -74,32 +109,12 @@ class SchemaExtractor
|
|||||||
if (!str_contains($name = $element->text, ' ')) {
|
if (!str_contains($name = $element->text, ' ')) {
|
||||||
$isMethod = lcfirst($name) == $name;
|
$isMethod = lcfirst($name) == $name;
|
||||||
$path = $isMethod ? 'methods' : 'types';
|
$path = $isMethod ? 'methods' : 'types';
|
||||||
$temp = $element;
|
['description' => $description, 'table' => $table, 'extended_by' => $extendedBy] = self::parseNode($element);
|
||||||
$description = '';
|
|
||||||
$table = null;
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
$element = $element->nextSibling();
|
|
||||||
} catch (ChildNotFoundException) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$tag = $element->tag->name() ?? null;
|
|
||||||
if (empty($temp->text()) or empty($tag) or $tag == 'text') {
|
|
||||||
continue;
|
|
||||||
} elseif (str_starts_with($tag, 'h')) {
|
|
||||||
break;
|
|
||||||
} elseif ($tag == 'p') {
|
|
||||||
$description .= PHP_EOL . $element->innerHtml();
|
|
||||||
} elseif ($tag == 'table') {
|
|
||||||
$table = $element->find('tbody')->find('tr');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* @var Dom\Node\AbstractNode $element */
|
|
||||||
$data[$path][] = self::generateElement(
|
$data[$path][] = self::generateElement(
|
||||||
$name,
|
$name,
|
||||||
trim($description),
|
trim($description),
|
||||||
$table,
|
$table,
|
||||||
|
$extendedBy,
|
||||||
$isMethod
|
$isMethod
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -111,6 +126,7 @@ class SchemaExtractor
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $description
|
* @param string $description
|
||||||
* @param Dom\Node\Collection|null $unparsedFields
|
* @param Dom\Node\Collection|null $unparsedFields
|
||||||
|
* @param array $extendedBy
|
||||||
* @param bool $isMethod
|
* @param bool $isMethod
|
||||||
* @return array
|
* @return array
|
||||||
* @throws ChildNotFoundException
|
* @throws ChildNotFoundException
|
||||||
@ -124,13 +140,15 @@ class SchemaExtractor
|
|||||||
string $name,
|
string $name,
|
||||||
string $description,
|
string $description,
|
||||||
?Dom\Node\Collection $unparsedFields,
|
?Dom\Node\Collection $unparsedFields,
|
||||||
|
array $extendedBy,
|
||||||
bool $isMethod
|
bool $isMethod
|
||||||
): array {
|
): array {
|
||||||
$fields = self::parseFields($unparsedFields, $isMethod);
|
$fields = self::parseFields($unparsedFields, $isMethod);
|
||||||
$result = [
|
$result = [
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
'description' => htmlspecialchars_decode(strip_tags($description), ENT_QUOTES),
|
'description' => htmlspecialchars_decode(strip_tags($description), ENT_QUOTES),
|
||||||
'fields' => $fields
|
'fields' => $fields,
|
||||||
|
'extended_by' => $extendedBy
|
||||||
];
|
];
|
||||||
if ($isMethod) {
|
if ($isMethod) {
|
||||||
$returnTypes = self::parseReturnTypes($description);
|
$returnTypes = self::parseReturnTypes($description);
|
||||||
|
@ -19,6 +19,7 @@ use Nette\PhpGenerator\Type;
|
|||||||
class StubCreator
|
class StubCreator
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
@ -115,13 +116,19 @@ class StubCreator
|
|||||||
* @param string $namespace
|
* @param string $namespace
|
||||||
* @return PhpFile[]
|
* @return PhpFile[]
|
||||||
*/
|
*/
|
||||||
#[ArrayShape(['Response' => "\Nette\PhpGenerator\PhpFile"])]
|
#[ArrayShape([
|
||||||
|
'Response' => "\Nette\PhpGenerator\PhpFile",
|
||||||
|
'TypeInterface' => "\Nette\PhpGenerator\ClassType"
|
||||||
|
])]
|
||||||
private function generateDefaultTypes(
|
private function generateDefaultTypes(
|
||||||
string $namespace
|
string $namespace
|
||||||
): array {
|
): array {
|
||||||
$file = new PhpFile;
|
$interfaceFile = new PhpFile;
|
||||||
$phpNamespace = $file->addNamespace($namespace);
|
$interfaceNamespace = $interfaceFile->addNamespace($namespace);
|
||||||
$response = $phpNamespace->addClass('Response')
|
$interface = $interfaceNamespace->addInterface('TypeInterface');
|
||||||
|
$responseFile = new PhpFile;
|
||||||
|
$responseNamespace = $responseFile->addNamespace($namespace);
|
||||||
|
$response = $responseNamespace->addClass('Response')
|
||||||
->setType('class');
|
->setType('class');
|
||||||
$response->addProperty('ok')
|
$response->addProperty('ok')
|
||||||
->setPublic()
|
->setPublic()
|
||||||
@ -141,8 +148,10 @@ class StubCreator
|
|||||||
->setType(Type::STRING)
|
->setType(Type::STRING)
|
||||||
->setNullable(true)
|
->setNullable(true)
|
||||||
->setValue(null);
|
->setValue(null);
|
||||||
|
$response->addImplement($namespace . '\\TypeInterface');
|
||||||
return [
|
return [
|
||||||
'Response' => $file
|
'Response' => $responseFile,
|
||||||
|
'TypeInterface' => $interfaceFile
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,6 +167,7 @@ class StubCreator
|
|||||||
$phpNamespace = $file->addNamespace($namespace);
|
$phpNamespace = $file->addNamespace($namespace);
|
||||||
$typeClass = $phpNamespace->addClass($type['name'])
|
$typeClass = $phpNamespace->addClass($type['name'])
|
||||||
->setType('class');
|
->setType('class');
|
||||||
|
$typeClass->addImplement($namespace . '\\TypeInterface');
|
||||||
foreach ($type['fields'] as $field) {
|
foreach ($type['fields'] as $field) {
|
||||||
['types' => $fieldTypes, 'comments' => $fieldComments] = $this->parseFieldTypes(
|
['types' => $fieldTypes, 'comments' => $fieldComments] = $this->parseFieldTypes(
|
||||||
$field['types'],
|
$field['types'],
|
||||||
|
Loading…
Reference in New Issue
Block a user